ref: 9d7f8fd65eb064db799e07a8ea405a124a1195d1 dhall-ruby/lib/dhall/util.rb -rw-r--r-- 4.3 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# frozen_string_literal: true

require "promise"
require "timeout"

module Dhall
	module Util
		class LazyPromise < Promise
			def initialize(&block)
				super
				@block = block
			end

			def subscribe(*args)
				super

				begin
					fulfill(@block.call)
				rescue => e
					reject(e)
				end
			end
		end

		class AllOf
			def initialize(*validators)
				@validators = validators
			end

			def ===(other)
				@validators.all? { |v| v === other }
			end
		end

		class ArrayOf < ValueSemantics::ArrayOf
			def initialize(element_validator, min: 0, max: Float::INFINITY)
				@min = min
				@max = max
				super(element_validator)
			end

			def ===(other)
				super && other.length >= @min && other.length <= @max
			end
		end

		class HashOf
			def initialize(
				key_validator,
				element_validator,
				min: 0,
				max: Float::INFINITY
			)
				@min = min
				@max = max
				@key_validator = key_validator
				@element_validator = element_validator
			end

			def ===(other)
				Hash === other &&
					other.keys.all? { |x| @key_validator === x } &&
					other.values.all? { |x| @element_validator === x } &&
					other.size >= @min && other.size <= @max
			end
		end

		module ArrayAllTheSame
			def self.===(other)
				Array === other && other.all? { |x| x == other.first }
			end
		end

		class Not
			def initialize(validator)
				@validator = validator
			end

			def ===(other)
				!(@validator === other)
			end
		end

		class Deadline
			def self.for_timeout(timeout)
				if timeout.nil? || timeout.to_f.infinite?
					NoDeadline.new
				else
					new(Time.now + timeout)
				end
			end

			def initialize(deadline)
				@deadline = deadline
			end

			def exceeded?
				@deadline < Time.now
			end

			def timeout
				[0.000000000000001, @deadline - Time.now].max
			end

			def timeout_block(&block)
				Timeout.timeout(timeout, TimeoutException, &block)
			end
		end

		class NoDeadline
			def exceeded?
				false
			end

			def timeout
				nil
			end

			def timeout_block
				yield
			end
		end

		def self.match_results(xs=nil, ys=nil)
			Array(xs).each_with_index.map do |r, idx|
				yield r, ys[idx]
			end
		end

		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

		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

		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

		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

		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

		def self.utf8_if_possible(str)
			utf8 = str.dup.force_encoding(Encoding::UTF_8)
			utf8.valid_encoding? ? utf8 : str
		end

		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

		def self.longest_common_prefix(a, b)
			a.zip(b).take_while { |(x, y)| x == y }.map(&:first)
		end

		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

		def self.path_components_to_uri(*components)
			URI("/#{components.map(&method(:uri_escape)).join("/")}")
		end

		def self.uri_escape(s)
			::URI.encode_www_form_component(s).gsub("+", "%20")
		end

		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
	end
end