~singpolyma/dhall-ruby

ref: 0.2.0 dhall-ruby/lib/dhall.rb -rw-r--r-- 1.0 KiB View raw
a49a8991Stephen Paul Weber Customizable cache 1 year, 26 days 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
# 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,
		timeout: 10
	)
		deadline = Util::Deadline.for_timeout(timeout)
		Promise.resolve(nil).then {
			load_raw(source.to_s, timeout: timeout).resolve(
				resolver: resolver.with_deadline(deadline)
			)
		}.then do |resolved|
			deadline.timeout_block do
				TypeChecker.for(resolved).annotate(TypeChecker::Context.new).normalize
			end
		end
	end

	def self.load_raw(source, timeout: 10)
		source = Util.text_or_binary(source)

		Util::Deadline.for_timeout(timeout).timeout_block do
			if source.encoding == Encoding::BINARY
				from_binary(source)
			else
				Parser.parse(source).value
			end
		end
	end

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

	class TimeoutException < StandardError; end
end