dhall 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dhall/util.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "timeout"
4
+
3
5
  module Dhall
4
6
  module Util
5
7
  class AllOf
@@ -61,45 +63,43 @@ module Dhall
61
63
  end
62
64
  end
63
65
 
64
- module BuiltinName
65
- NAMES = [
66
- "Natural/build",
67
- "Natural/fold",
68
- "Natural/isZero",
69
- "Natural/even",
70
- "Natural/odd",
71
- "Natural/toInteger",
72
- "Natural/show",
73
- "Integer/toDouble",
74
- "Integer/show",
75
- "Double/show",
76
- "List/build",
77
- "List/fold",
78
- "List/length",
79
- "List/head",
80
- "List/last",
81
- "List/indexed",
82
- "List/reverse",
83
- "Optional/fold",
84
- "Optional/build",
85
- "Text/show",
86
- "Bool",
87
- "Optional",
88
- "Natural",
89
- "Integer",
90
- "Double",
91
- "Text",
92
- "List",
93
- "True",
94
- "False",
95
- "None",
96
- "Type",
97
- "Kind",
98
- "Sort"
99
- ].freeze
66
+ class Deadline
67
+ def self.for_timeout(timeout)
68
+ if timeout.nil? || timeout.to_f.infinite?
69
+ NoDeadline.new
70
+ else
71
+ new(Time.now + timeout)
72
+ end
73
+ end
100
74
 
101
- def self.===(other)
102
- NAMES.include?(other)
75
+ def initialize(deadline)
76
+ @deadline = deadline
77
+ end
78
+
79
+ def exceeded?
80
+ @deadline < Time.now
81
+ end
82
+
83
+ def timeout
84
+ [0.000000000000001, @deadline - Time.now].max
85
+ end
86
+
87
+ def timeout_block(&block)
88
+ Timeout.timeout(timeout, TimeoutException, &block)
89
+ end
90
+ end
91
+
92
+ class NoDeadline
93
+ def exceeded?
94
+ false
95
+ end
96
+
97
+ def timeout
98
+ nil
99
+ end
100
+
101
+ def timeout_block
102
+ yield
103
103
  end
104
104
  end
105
105
 
@@ -126,5 +126,56 @@ module Dhall
126
126
  end]
127
127
  end
128
128
  end
129
+
130
+ def self.psych_coder_for(tag, v)
131
+ c = Psych::Coder.new(tag)
132
+ case v
133
+ when Hash
134
+ c.map = v
135
+ when Array
136
+ c.seq = v
137
+ else
138
+ c.scalar = v
139
+ end
140
+ end
141
+
142
+ def self.psych_coder_from(tag, o)
143
+ coder = Psych::Coder.new(tag)
144
+
145
+ if o.respond_to?(:encode_with)
146
+ o.encode_with(coder)
147
+ else
148
+ o.instance_variables.each do |ivar|
149
+ coder[ivar.to_s[1..-1]] = o.instance_variable_get(ivar)
150
+ end
151
+ end
152
+
153
+ coder
154
+ end
155
+
156
+ def self.transform_keys(hash_or_not)
157
+ return hash_or_not unless hash_or_not.is_a?(Hash)
158
+
159
+ Hash[hash_or_not.map { |k, v| [(yield k), v] }]
160
+ end
161
+
162
+ def self.utf8_if_possible(str)
163
+ utf8 = str.dup.force_encoding(Encoding::UTF_8)
164
+ utf8.valid_encoding? ? utf8 : str
165
+ end
166
+
167
+ def self.text_or_binary(str)
168
+ unless str.valid_encoding?
169
+ raise ArgumentError, "invalid byte sequence in #{str.encoding}"
170
+ end
171
+
172
+ if str.encoding == Encoding::BINARY
173
+ return str if str =~ /(?!\s)[[:cntrl:]]/
174
+
175
+ utf8_if_possible(str)
176
+ else
177
+ str.encode(Encoding::UTF_8)
178
+ end
179
+ end
129
180
  end
130
181
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dhall
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Paul Weber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-10 00:00:00.000000000 Z
11
+ date: 2019-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cbor
@@ -129,6 +129,7 @@ files:
129
129
  - lib/dhall/ast.rb
130
130
  - lib/dhall/binary.rb
131
131
  - lib/dhall/builtins.rb
132
+ - lib/dhall/coder.rb
132
133
  - lib/dhall/normalize.rb
133
134
  - lib/dhall/parser.citrus
134
135
  - lib/dhall/parser.rb