~singpolyma/dhall-ruby

ref: 134ece750abbc97f6c926b3171af40a41bfa13a2 dhall-ruby/lib/dhall/binary.rb -rw-r--r-- 4.8 KiB
134ece75Stephen Paul Weber Decoding Dhall from binary works 4 years ago
                                                                                
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
236
237
238
239
240
241
# frozen_string_literal: true

require "cbor"

module Dhall
	def self.from_binary(cbor_binary)
		data = CBOR.decode(cbor_binary)
		if data.is_a?(Array) && data[0] == "5.0.0"
			decode(data[1])
		else
			decode(data)
		end
	end

	def self.decode(expression)
		BINARY.each do |match, use|
			return use[expression] if expression.is_a?(match)
		end

		raise "Unknown expression: #{expression.inspect}"
	end

	BINARY = {
		::TrueClass => Bool.method(:decode),
		::FalseClass => Bool.method(:decode),
		::Float => Double.method(:decode),
		::String => ->(e) { Variable.decode(e, 0) },
		::Integer => ->(e) { Variable.decode("_", e) },
		::Array => lambda { |e|
			if e.length == 2 && e.first.is_a?(::String)
				Variable.decode(*expression)
			else
				tag, *body = expression
				BINARY_TAGS[tag]&.decode(*body) ||
					(raise "Unknown expression: #{expression.inspect}")
			end
		}
	}.freeze

	BINARY_TAGS = [
		Application,
		Function,
		Forall,
		Operator,
		List,
		Optional,
		Merge,
		RecordType,
		Record,
		RecordFieldAccess,
		RecordProjection,
		UnionType,
		Union,
		Constructors,
		If,
		Natural,
		Integer,
		nil,
		TextLiteral,
		nil,
		nil,
		nil,
		nil,
		nil,
		Import,
		LetBlock,
		TypeAnnotation
	].freeze

	class Expression
		def self.decode(*args)
			new(*args)
		end
	end

	class Application < Expression
		def self.decode(f, *args)
			new(Dhall.decode(f), *args.map(&Dhall.method(:decode)))
		end
	end

	class Function < Expression
		def self.decode(var_or_type, type_or_body, body_or_nil=nil)
			if body_or_nil.nil?
				new("_", Dhall.decode(var_or_type), Dhall.decode(type_or_body))
			else
				unless var_or_type.is_a?(String)
					raise TypeError, "Function var must be a String"
				end

				raise ArgumentError, "explicit var named _" if var_or_type == "_"

				new(var_or_type, Dhall.decode(type_or_body), Dhall.decode(body_or_nil))
			end
		end
	end

	class Operator < Expression
		OPCODES = [
			:'||', :'&&', :==, :!=, :+, :*, :'++', :'#', :, :, :, :'?'
		].freeze

		def self.decode(opcode, lhs, rhs)
			new(
				OPCODES[opcode] || (raise "Unknown opcode: #{opcode}"),
				Dhall.decode(lhs),
				Dhall.decode(rhs)
			)
		end
	end

	class List < Expression
		def self.decode(type, *els)
			if type.nil?
				List.new(*els.map(&Dhall.method(:decode)))
			else
				EmptyList.new(Dhall.decode(type))
			end
		end
	end

	class Optional < Expression
		def self.decode(type, value=nil)
			if value.nil?
				OptionalNone.new(Dhall.decode(type))
			else
				Optional.new(
					Dhall.decode(value),
					type.nil? ? type : Dhall.decode(type)
				)
			end
		end
	end

	class Merge < Expression
		def self.decode(record, input, type=nil)
			new(
				Dhall.decode(record),
				Dhall.decode(input),
				type.nil? ? nil : Dhall.decode(type)
			)
		end
	end

	class RecordType < Expression
		def self.decode(record)
			new(Hash[record.map { |k, v| [k, Dhall.decode(v)] }])
		end
	end

	class Record < Expression
		def self.decode(record)
			new(Hash[record.map { |k, v| [k, Dhall.decode(v)] }])
		end
	end

	class RecordFieldAccess < Expression
		def self.decode(record, field)
			new(Dhall.decode(record), field)
		end
	end

	class RecordProjection < Expression
		def self.decode(record, *fields)
			new(Dhall.decode(record), *fields)
		end
	end

	class UnionType < Expression
		def self.decode(record)
			new(Hash[record.map { |k, v| [k, Dhall.decode(v)] }])
		end
	end

	class Union < Expression
		def self.decode(tag, value, rest_of_type)
			new(
				tag,
				Dhall.decode(value),
				Hash[rest_of_type.map { |k, v| [k, Dhall.decode(v)] }]
			)
		end
	end

	class If < Expression
		def self.decode(cond, thn, els)
			new(Dhall.decode(cond), Dhall.decode(thn), Dhall.decode(els))
		end
	end

	class TextLiteral < Text
		def self.decode(*chunks)
			if chunks.length == 1 && chunks.is_a?(String)
				Text.new(chunks.first)
			else
				TextLiteral.new(*chunks.map do |chunk|
					chunk.is_a?(String) ? Text.new(chunk) : Dhall.decode(chunk)
				end)
			end
		end
	end

	class Import
		IMPORT_TYPES = [Expression, Text].freeze
		PATH_TYPES = [
			Http, Https,
			AbsolutePath, RelativePath, RelativeToParentPath, RelativeToHomePath,
			EnvironmentVariable, MissingImport
		].freeze

		def self.decode(integrity_check, import_type, path_type, *parts)
			parts[0] = Dhall.decode(parts[0]) if path_type < 2 && !parts[0].nil?
			new(
				integrity_check.nil? ? nil : IntegrityCheck.new(*integrity_check),
				IMPORT_TYPES[import_type],
				PATH_TYPES[path_type].new(*parts)
			)
		end
	end

	class LetBlock < Expression
		def self.decode(*parts)
			new(
				Dhall.decode(parts.pop),
				*parts.each_slice(3).map do |(var, type, assign)|
					Let.new(
						var,
						Dhall.decode(assign),
						type.nil? ? nil : Dhall.decode(type)
					)
				end
			)
		end
	end

	class TypeAnnotation < Expression
		def self.decode(value, type)
			new(Dhall.decode(value), Dhall.decode(type))
		end
	end
end