module Dhall::Util

Public Class Methods

indent_size(str) click to toggle source
# File lib/dhall/util.rb, line 203
def self.indent_size(str)
        if str.end_with?("\n")
                0
        else
                str
                        .scan(/^[ \t]*(?=[^ \t\n]|\Z)/)
                        .map(&:chars)
                        .reduce(&method(:longest_common_prefix))&.length.to_i
        end
end
longest_common_prefix(a, b) click to toggle source
# File lib/dhall/util.rb, line 199
def self.longest_common_prefix(a, b)
        a.zip(b).take_while { |(x, y)| x == y }.map(&:first)
end
match_result_promises(xs=nil, ys=nil) { |Array(xs)| ... } click to toggle source
# File lib/dhall/util.rb, line 130
def self.match_result_promises(xs=nil, ys=nil)
        match_results(yield(Array(xs)), ys) do |promise, promises|
                promises.each { |p| p.fulfill(promise) }
        end
end
match_results(xs=nil, ys=nil) { |r, ys| ... } click to toggle source
# File lib/dhall/util.rb, line 124
def self.match_results(xs=nil, ys=nil)
        Array(xs).each_with_index.map do |r, idx|
                yield r, ys[idx]
        end
end
net_http_req_with_timeout(uri, req, timeout:) click to toggle source
# File lib/dhall/util.rb, line 222
def self.net_http_req_with_timeout(uri, req, timeout:)
        Net::HTTP.start(
                uri.hostname,
                uri.port,
                use_ssl:       uri.scheme == "https",
                open_timeout:  timeout,
                ssl_timeout:   timeout,
                read_timeout:  timeout,
                write_timeout: timeout
        ) { |http| http.request(req) }
end
path_components_to_uri(*components) click to toggle source
# File lib/dhall/util.rb, line 214
def self.path_components_to_uri(*components)
        URI("/#{components.map(&method(:uri_escape)).join("/")}")
end
promise_all_hash(hash) click to toggle source
# File lib/dhall/util.rb, line 136
def self.promise_all_hash(hash)
        keys, promises = hash.to_a.transpose

        return Promise.resolve(hash) unless keys

        Promise.all(promises).then do |values|
                Hash[Util.match_results(keys, values) do |k, v|
                        [k, v]
                end]
        end
end
psych_coder_for(tag, v) click to toggle source
# File lib/dhall/util.rb, line 148
def self.psych_coder_for(tag, v)
        c = Psych::Coder.new(tag)
        case v
        when Hash
                c.map = v
        when Array
                c.seq = v
        else
                c.scalar = v
        end
end
psych_coder_from(tag, o) click to toggle source
# File lib/dhall/util.rb, line 160
def self.psych_coder_from(tag, o)
        coder = Psych::Coder.new(tag)

        if o.respond_to?(:encode_with)
                o.encode_with(coder)
        else
                o.instance_variables.each do |ivar|
                        coder[ivar.to_s[1..-1]] = o.instance_variable_get(ivar)
                end
        end

        coder
end
text_or_binary(str) click to toggle source
# File lib/dhall/util.rb, line 185
def self.text_or_binary(str)
        unless str.valid_encoding?
                raise ArgumentError, "invalid byte sequence in #{str.encoding}"
        end

        if str.encoding == Encoding::BINARY
                return str if str =~ /(?!\s)[[:cntrl:]]/

                utf8_if_possible(str)
        else
                str.encode(Encoding::UTF_8)
        end
end
transform_keys(hash_or_not) click to toggle source
# File lib/dhall/util.rb, line 174
def self.transform_keys(hash_or_not)
        return hash_or_not unless hash_or_not.is_a?(Hash)

        Hash[hash_or_not.map { |k, v| [(yield k), v] }]
end
uri_escape(s) click to toggle source
# File lib/dhall/util.rb, line 218
def self.uri_escape(s)
        ::URI.encode_www_form_component(s).gsub("+", "%20")
end
utf8_if_possible(str) click to toggle source
# File lib/dhall/util.rb, line 180
def self.utf8_if_possible(str)
        utf8 = str.dup.force_encoding(Encoding::UTF_8)
        utf8.valid_encoding? ? utf8 : str
end