klenod-build 0.0.29 → 0.0.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9dbc1f6baef3ea6249edc7d68f1d8725234384d45c3f00dcf1b4ebba50eb8cbe
4
- data.tar.gz: ee110cebfd420f402a67c922b2dcb1fa8cb9f8dffe768e4d5ced389ae6110f49
3
+ metadata.gz: 13ffde9084826c2af713cef2b267b547b57ec88b1f23dd7e32bb5da3681d929e
4
+ data.tar.gz: 46585426c377c8368573a901f7062fc7f862e8fed2dbf4b0b96b1032fd0e36dd
5
5
  SHA512:
6
- metadata.gz: cae3766524cc6f31a74420abed1f8eaec845204f2465dfc1b48b9ff0f6ad7695dbc648a7394e58940338d1f8ce052beee3cb6cef7fc7c97ec18510f6badd85f6
7
- data.tar.gz: 6a4230dcc4503ff87c4974f9cbd8065cc8348f66ca54f47a6f44e8419367b0901a0562e1dc0bd8bc633cf7d86fae621b4171b63e07b716f887d93b71e3d44e5a
6
+ metadata.gz: e4abd68c6f308b9fbadd8e099a034e741220a22d72f93207880d97400a7f2987a27dbcb3fd9aaaae2a833dfd4586cc7c23703ba6a5fe0f0691226ad378f4febf
7
+ data.tar.gz: f8c131821ecdbe4ed750418c0262be97602116f64f6b8a05cce9b099c3072a7bc73e5323c00ac9348cc1a68ce2256a1ad9a80611dea5805ee7f559aa650ee918
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "date"
3
4
  require "json"
4
5
  require "toml-rb"
5
6
  require "yaml"
@@ -63,9 +64,42 @@ module Klenod
63
64
 
64
65
  def module_source(data)
65
66
  <<~RUBY
66
- Default = #{data.inspect}
67
+ Default = #{ruby_literal(data)}
67
68
  RUBY
68
69
  end
70
+
71
+ # `inspect` is not Ruby source for every parsed value: dates, times,
72
+ # and non-finite floats need constructor calls instead.
73
+ def ruby_literal(value)
74
+ case value
75
+ when Hash
76
+ entries = value.map { |key, entry| "#{ruby_literal(key)} => #{ruby_literal(entry)}" }
77
+ "{#{entries.join(", ")}}"
78
+ when Array
79
+ "[#{value.map { ruby_literal(it) }.join(", ")}]"
80
+ when Float
81
+ float_literal(value)
82
+ when Time
83
+ zone = value.utc? ? "UTC" : value.utc_offset
84
+ "::Time.at(#{value.to_i}, #{value.nsec}, :nsec, in: #{zone.inspect})"
85
+ when Date
86
+ "::Date.new(#{value.year}, #{value.month}, #{value.day})"
87
+ when nil, true, false, String, Symbol, Integer
88
+ value.inspect
89
+ else
90
+ raise TypeError, "Cannot write #{value.class} as data module source"
91
+ end
92
+ end
93
+
94
+ def float_literal(value)
95
+ if value.nan?
96
+ "::Float::NAN"
97
+ elsif value.infinite?
98
+ value.positive? ? "::Float::INFINITY" : "-::Float::INFINITY"
99
+ else
100
+ value.inspect
101
+ end
102
+ end
69
103
  end
70
104
  end
71
105
 
@@ -155,8 +189,8 @@ module Klenod
155
189
  end
156
190
 
157
191
  class ParseError < Klenod::Build::SourceError
158
- # "Failed to parse input on line 2 at offset 9\ninvalid =\n\n ^"
159
- LOCATION = /\AFailed to parse input on line (?<line>\d+) at offset (?<column>\d+)/
192
+ # "Unexpected \"\\n\" at line 2, column 10: expected a value"
193
+ LOCATION = / at line (?<line>\d+), column (?<column>\d+)/
160
194
 
161
195
  def kind
162
196
  "TOML parse error"
@@ -165,16 +199,16 @@ module Klenod
165
199
  private
166
200
 
167
201
  def location(error)
202
+ # Invalid values such as "Invalid date: 1979-13-01" carry no location.
168
203
  found = LOCATION.match(error.message)
169
204
  return nil unless found
170
205
 
171
- # toml-rb embeds its own caret diagram in the message; we render our
172
- # own from the line and column instead.
206
+ # One-based. The location is rendered as the title and the caret, so
207
+ # drop the copy the parser puts in its message.
173
208
  Location.new(
174
209
  line: found[:line].to_i,
175
- # citrus reports a zero-based offset into the line.
176
- column: found[:column].to_i + 1,
177
- detail: "Could not parse TOML"
210
+ column: found[:column].to_i,
211
+ detail: error.message.sub(LOCATION, "")
178
212
  )
179
213
  end
180
214
  end
@@ -186,7 +220,26 @@ module Klenod
186
220
  private
187
221
 
188
222
  def parse(code)
189
- TomlRB.parse(code)
223
+ plain_values(TomlRB.parse(code))
224
+ end
225
+
226
+ # toml-rb returns its own Time subclasses for local values. Exports
227
+ # and bundles should not depend on toml-rb, so a local date becomes a
228
+ # Date, as in YAML, and a local datetime or time becomes a UTC Time
229
+ # with the same clock time. A local time falls on 1970-01-01.
230
+ def plain_values(value)
231
+ case value
232
+ when Hash
233
+ value.transform_values { plain_values(it) }
234
+ when Array
235
+ value.map { plain_values(it) }
236
+ when TomlRB::LocalDate
237
+ Date.new(value.year, value.month, value.day)
238
+ when TomlRB::LocalDateTime, TomlRB::LocalTime
239
+ Time.at(value, in: "UTC")
240
+ else
241
+ value
242
+ end
190
243
  end
191
244
  end
192
245
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Klenod
4
4
  module Build
5
- VERSION = "0.0.29"
5
+ VERSION = "0.0.30"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klenod-build
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.29
4
+ version: 0.0.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrés Alin
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.0.29
18
+ version: 0.0.30
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.0.29
25
+ version: 0.0.30
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: async
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -217,14 +217,14 @@ dependencies:
217
217
  requirements:
218
218
  - - "~>"
219
219
  - !ruby/object:Gem::Version
220
- version: '4.2'
220
+ version: '6.0'
221
221
  type: :runtime
222
222
  prerelease: false
223
223
  version_requirements: !ruby/object:Gem::Requirement
224
224
  requirements:
225
225
  - - "~>"
226
226
  - !ruby/object:Gem::Version
227
- version: '4.2'
227
+ version: '6.0'
228
228
  - !ruby/object:Gem::Dependency
229
229
  name: tsort
230
230
  requirement: !ruby/object:Gem::Requirement