decoding 0.2.1 → 0.2.3

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: 05a711dfeb38945b0d8f57745f9555cd0a8a01b8e74a119aafddb9b74e3f4ccc
4
- data.tar.gz: 5faa5cc2848bb269bf1bf65766dfed1fd80f6f74e39b48c3cfcddc9b9ede1946
3
+ metadata.gz: f89de33cb9abd7e09600ac7622500813a93478e9e57fcadbe678688df0e2d1e2
4
+ data.tar.gz: 34dfd51a4642e580312863aa856d220562b9281db70023e5011396c5b3b897f5
5
5
  SHA512:
6
- metadata.gz: 6822f9f59e3da74225b435131991b9e903bfd77206c1edd0620849d74d89db509572550eb184ec49c1ec5ef4ef1729072a93f104b6bf05fa300ffe2853dfbdee
7
- data.tar.gz: 30d2caa4fa4ff49fab9f4327eada6daf088204f392e87765048decc5b6c3534ebc629850d8d0aff5a7b43228afc8f9bbe58fd121a6466e515e1dd8b4b69b96b7
6
+ metadata.gz: e45131fe22a31c0200a31ff0160648a74cb7ef5ee3121475a55bbb3be3933f1101127f258cdd49702c38c5587879864d00e7b1613f34f6ee65d52f2346271481
7
+ data.tar.gz: 76b3a9c08511bbf3f067e41cb7ef7a8d2216665ed56e96c5e72da3057f78ef2b95fe7b0cdcc0bcecd70e2fccbd9eb5041869531e61506514f8270715f0520cb6
data/.rubocop.yml CHANGED
@@ -19,6 +19,10 @@ Style/StringLiteralsInInterpolation:
19
19
  Layout/LineLength:
20
20
  Max: 140
21
21
 
22
+ Metrics/ModuleLength:
23
+ Exclude:
24
+ - spec/**/*_spec.rb
25
+
22
26
  Metrics/BlockLength:
23
27
  Exclude:
24
28
  - spec/**/*_spec.rb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.3]
4
+
5
+ * Implement `Decoding::Result#deconstruct` to support pattern matching on result values.
6
+
7
+ ## [0.2.2] - 2025-10-25
8
+
9
+ * Added `Result#unwrap_err`
10
+
3
11
  ## [0.2.1] - 2025-10-25
4
12
 
5
13
  * Fixed missed error not returning `Decoding::Failure`
@@ -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
@@ -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(re) = Decoders::Match.new(Regexp.new(re))
36
+ def regexp(regex) = Decoders::Match.new(Regexp.new(regex))
37
37
 
38
38
  # Decode any integer value.
39
39
  #
@@ -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
@@ -52,6 +52,10 @@ module Decoding
52
52
  freeze
53
53
  end
54
54
 
55
+ def deconstruct
56
+ [value]
57
+ end
58
+
55
59
  def eql?(other)
56
60
  other.is_a?(self.class) && value == other.value
57
61
  end
@@ -79,6 +83,14 @@ module Decoding
79
83
  # @return [Object]
80
84
  def unwrap(default_value) = default_value
81
85
 
86
+ # Extract the error value out of a `Result` value. In case of an `Err`, this
87
+ # returns the result's value. In case of an `Ok`, the given `default_value`
88
+ # is returned.
89
+ #
90
+ # @param default_value [Object]
91
+ # @return [Object]
92
+ def unwrap_err(default_value) = default_value
93
+
82
94
  # Create a new `Result` value for the result of the block applied to this
83
95
  # result's `value`. `Err` values are returned as-is.
84
96
  #
@@ -149,5 +161,6 @@ module Decoding
149
161
 
150
162
  def err? = true
151
163
  def map_err = self.class.new(yield value)
164
+ def unwrap_err(_) = value
152
165
  end
153
166
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Decoding
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decoding
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arjan van der Gaag