decoding 0.2.4 → 0.2.6
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/CHANGELOG.md +8 -0
- data/lib/decoding/decoders/any.rb +12 -2
- data/lib/decoding/result.rb +12 -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: 5d613b53b493863ecae1fcbc778f5684acbbc628ad51f88fca0953889cd138d0
|
|
4
|
+
data.tar.gz: cfc4039a5bfcf182c174b0d1f037d2cbd3b610fa746904906a2209b680fb4cad
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cbe2df98d6a2b6b9588cc5ecfe5543cae21575488a09d99768f10791fdb66332f8cda35483b6e61045a8bbdb5949f900cdd69ab94cfb602e456fb05f5dbfa14e
|
|
7
|
+
data.tar.gz: e0eaddb3e027ba796f126aa500564507481847c196d139b16a51c390cb28990706be3d959bde8700857abbf2c18fc69765f79c03cd7cd16cbfd5e7a7be03be92
|
data/CHANGELOG.md
CHANGED
|
@@ -20,8 +20,18 @@ module Decoding
|
|
|
20
20
|
# @param value [Object]
|
|
21
21
|
# @return [Decoding::Result<Object>]
|
|
22
22
|
def call(value)
|
|
23
|
-
|
|
24
|
-
@decoders.
|
|
23
|
+
failures = []
|
|
24
|
+
@decoders.each do |decoder|
|
|
25
|
+
result = decoder.call(value)
|
|
26
|
+
|
|
27
|
+
# NOTE: we could've pattern matched here but that would create an
|
|
28
|
+
# unreachable `else` clause triggering code coverage issues. This
|
|
29
|
+
# explicit way ensures we know every code path is touched.
|
|
30
|
+
return result if result.ok?
|
|
31
|
+
|
|
32
|
+
failures << result.unwrap_err(nil)
|
|
33
|
+
end
|
|
34
|
+
err(failure("None of the decoders matched:\n#{failures.map { " - #{_1}" }.join("\n")}"))
|
|
25
35
|
end
|
|
26
36
|
end
|
|
27
37
|
end
|
data/lib/decoding/result.rb
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Decoding
|
|
4
|
+
# Raised when calling {Result#unwrap!} on an `Err` value.
|
|
5
|
+
class UnwrapError < StandardError; end
|
|
6
|
+
|
|
4
7
|
# A result represent the outcome of some computation that can succeed or fail.
|
|
5
8
|
# The results are represented with two subclasses of `Result`: `Ok` and `Err`.
|
|
6
9
|
# Each hold a single result value.
|
|
@@ -83,6 +86,14 @@ module Decoding
|
|
|
83
86
|
# @return [Object]
|
|
84
87
|
def unwrap(default_value) = default_value
|
|
85
88
|
|
|
89
|
+
# Extract the value out of a `Result` value. In case of an `Ok`, this
|
|
90
|
+
# returns the result's value. In case of an `Err`, a {UnwrapError} is
|
|
91
|
+
# raised with the error value as its message.
|
|
92
|
+
#
|
|
93
|
+
# @raise [Decoding::UnwrapError] if the result is an `Err` value.
|
|
94
|
+
# @return [Object]
|
|
95
|
+
def unwrap! = raise(UnwrapError, value.to_s)
|
|
96
|
+
|
|
86
97
|
# Extract the error value out of a `Result` value. In case of an `Err`, this
|
|
87
98
|
# returns the result's value. In case of an `Ok`, the given `default_value`
|
|
88
99
|
# is returned.
|
|
@@ -142,6 +153,7 @@ module Decoding
|
|
|
142
153
|
|
|
143
154
|
def ok? = true
|
|
144
155
|
def unwrap(_) = value
|
|
156
|
+
def unwrap! = value
|
|
145
157
|
def map = self.class.new(yield value)
|
|
146
158
|
|
|
147
159
|
def and(other)
|
data/lib/decoding/version.rb
CHANGED