decoding 0.2.0 → 0.2.2
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 +4 -4
- data/.rubocop.yml +4 -0
- data/CHANGELOG.md +8 -0
- data/lib/decoding/decoders/field.rb +2 -2
- data/lib/decoding/decoders/pass.rb +4 -0
- data/lib/decoding/decoders.rb +1 -1
- data/lib/decoding/failure.rb +11 -8
- data/lib/decoding/result.rb +9 -0
- data/lib/decoding/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b3b7725a14c0738d4860f46509eb8e2ce781f6298bb9253797c0dad84f769564
|
|
4
|
+
data.tar.gz: '07785a90e96fe8a56bcbb2986d0cdd61aca6e75541f084e1a5c7be5799261912'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4c253b152f0f82f95fb3a5a77c84acf741686c776492418967b5a9ca11800f1296351af373126f9d1efaeba8dd2418d3bbbdbdc1ad88e77c314489442b9af1a6
|
|
7
|
+
data.tar.gz: ddba1656e7e7cec32423d0c1e6ada7494972b620666e1dc3daa70b7bf5c1210a0ad50911778b74e8c06d2f4befbe3f644c44bf38b05c8b97a266fdf3d0afd49f
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -23,10 +23,10 @@ module Decoding
|
|
|
23
23
|
if value.key?(@key)
|
|
24
24
|
@decoder.call(value.fetch(@key)).map_err { _1.push(@key) }
|
|
25
25
|
else
|
|
26
|
-
err("expected a Hash with key #{@key}")
|
|
26
|
+
err(failure("expected a Hash with key #{@key}"))
|
|
27
27
|
end
|
|
28
28
|
else
|
|
29
|
-
err("expected a Hash, got: #{value.inspect}")
|
|
29
|
+
err(failure("expected a Hash, got: #{value.inspect}"))
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
end
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
module Decoding
|
|
4
4
|
module Decoders
|
|
5
|
+
# Decoder that returns the original value as-is. You are not likely to need
|
|
6
|
+
# this that often, as it kind of defeats the point of decoding -- but it
|
|
7
|
+
# might be useful to inspect the original input value for logging or
|
|
8
|
+
# debguging purposes.
|
|
5
9
|
class Pass < Decoder
|
|
6
10
|
def call(value) = ok(value)
|
|
7
11
|
end
|
data/lib/decoding/decoders.rb
CHANGED
|
@@ -33,7 +33,7 @@ module Decoding
|
|
|
33
33
|
# @param re [Regexp, String]
|
|
34
34
|
# @return [Decoding::Decoder<String>]
|
|
35
35
|
# @see Decoding::Decoders::Match
|
|
36
|
-
def regexp(
|
|
36
|
+
def regexp(regex) = Decoders::Match.new(Regexp.new(regex))
|
|
37
37
|
|
|
38
38
|
# Decode any integer value.
|
|
39
39
|
#
|
data/lib/decoding/failure.rb
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Decoding
|
|
4
|
+
# A failure is an error message, much like a string, but with an added stack
|
|
5
|
+
# of earlier messages.
|
|
6
|
+
#
|
|
7
|
+
# This is useful to create clearer error messages when using compound
|
|
8
|
+
# decoders, such as `array(string)`. If the `string` decoder fails with an
|
|
9
|
+
# error, the `array` decoder can push `3` to the stack to indicate that
|
|
10
|
+
# happened at index 3 in its input value.
|
|
4
11
|
class Failure
|
|
5
|
-
protected attr_reader :msg
|
|
6
|
-
protected attr_reader :path
|
|
7
|
-
|
|
8
12
|
# @paramn msg [String]
|
|
9
13
|
def initialize(msg)
|
|
10
14
|
@msg = msg
|
|
@@ -20,11 +24,6 @@ module Decoding
|
|
|
20
24
|
|
|
21
25
|
# Add segments to the stack of errors.
|
|
22
26
|
#
|
|
23
|
-
# This is useful to create clearer error messages when using compound
|
|
24
|
-
# decoders, such as `array(string)`. If the `string` decoder fails with an
|
|
25
|
-
# error, the `array` decoder can push `3` to the stack to indicate that
|
|
26
|
-
# happened at index 3 in its input value.
|
|
27
|
-
#
|
|
28
27
|
# @param segment [String]
|
|
29
28
|
# @return [Decoding::Failure]
|
|
30
29
|
def push(segment)
|
|
@@ -39,5 +38,9 @@ module Decoding
|
|
|
39
38
|
@msg
|
|
40
39
|
end
|
|
41
40
|
end
|
|
41
|
+
|
|
42
|
+
protected
|
|
43
|
+
|
|
44
|
+
attr_reader :msg, :path
|
|
42
45
|
end
|
|
43
46
|
end
|
data/lib/decoding/result.rb
CHANGED
|
@@ -79,6 +79,14 @@ module Decoding
|
|
|
79
79
|
# @return [Object]
|
|
80
80
|
def unwrap(default_value) = default_value
|
|
81
81
|
|
|
82
|
+
# Extract the error value out of a `Result` value. In case of an `Err`, this
|
|
83
|
+
# returns the result's value. In case of an `Ok`, the given `default_value`
|
|
84
|
+
# is returned.
|
|
85
|
+
#
|
|
86
|
+
# @param default_value [Object]
|
|
87
|
+
# @return [Object]
|
|
88
|
+
def unwrap_err(default_value) = default_value
|
|
89
|
+
|
|
82
90
|
# Create a new `Result` value for the result of the block applied to this
|
|
83
91
|
# result's `value`. `Err` values are returned as-is.
|
|
84
92
|
#
|
|
@@ -149,5 +157,6 @@ module Decoding
|
|
|
149
157
|
|
|
150
158
|
def err? = true
|
|
151
159
|
def map_err = self.class.new(yield value)
|
|
160
|
+
def unwrap_err(_) = value
|
|
152
161
|
end
|
|
153
162
|
end
|
data/lib/decoding/version.rb
CHANGED