# frozen_string_literal: true module Wayfarer module Config module Strconv module_function def parse(str, type = nil) return primitive(str) unless type case type.name when "Hash" then hash(str) when "Array" then array(str) when "Symbol" then str.to_sym when "Integer" then Integer(str) else str end end def hash(str) array(str).reduce({}) do |acc, pair| k, v = pair.split(":", 2) next acc unless k && v acc.merge({ parse(k, Symbol) => primitive(v) }) end end def array(str) str.split(",").map(&:strip) end def primitive(str) return true if str == "true" return false if str == "false" begin parse(str, Integer) rescue StandardError str end end end end end