~singpolyma/dhall-ruby

ref: 5f399393e6c7c3df810cd53793f9ddba5cbcfabd dhall-ruby/lib/dhall.rb -rw-r--r-- 1.0 KiB
5f399393Stephen Paul Weber Update to dhall-lang 7.0.0 3 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
# frozen_string_literal: true

require "dhall/as_dhall"
require "dhall/ast"
require "dhall/binary"
require "dhall/builtins"
require "dhall/coder"
require "dhall/normalize"
require "dhall/parser"
require "dhall/resolve"
require "dhall/typecheck"

module Dhall
	using Dhall::AsDhall

	def self.load(source, resolver: Resolvers::Default.new)
		Promise.resolve(nil).then {
			load_raw(source.to_s).resolve(resolver: resolver)
		}.then do |resolved|
			TypeChecker.for(resolved).annotate(TypeChecker::Context.new).normalize
		end
	end

	def self.load_raw(source)
		unless source.valid_encoding?
			raise ArgumentError, "invalid byte sequence in #{source.encoding}"
		end

		begin
			return from_binary(source) if source.encoding == Encoding::BINARY
		rescue Exception # rubocop:disable Lint/RescueException
			# Parsing CBOR failed, so guess this is source text in standard UTF-8
			return load_raw(source.force_encoding("UTF-8"))
		end

		Parser.parse(source.encode("UTF-8")).value
	end

	def self.dump(o)
		CBOR.encode(o.as_dhall)
	end
end