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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f462705ab2cc82507ed66b66274dd6f9167ade6b3bab3e8d12dc329f8f564fb
4
- data.tar.gz: 2a16abba19d6b9f8cb68d7db943e55627c439925b40090f96dc3c8b6dc27e65e
3
+ metadata.gz: b3b7725a14c0738d4860f46509eb8e2ce781f6298bb9253797c0dad84f769564
4
+ data.tar.gz: '07785a90e96fe8a56bcbb2986d0cdd61aca6e75541f084e1a5c7be5799261912'
5
5
  SHA512:
6
- metadata.gz: 918dae4e293d0585afd44aa092fd95a295d3ed5153fd9a5f4acf041a551fdc97a8ca14f030f038c7507f0b7de799b6cb6076dcbb3bac8b18f024e45ed05fd068
7
- data.tar.gz: 818550ca01578f9ba869bf54bb875f962ac22ee179f12c23e7565ab4923e733aaa629325561c73f5c1659c290857200b5dd1bb3b3d4eafad00e445c8ff291385
6
+ metadata.gz: 4c253b152f0f82f95fb3a5a77c84acf741686c776492418967b5a9ca11800f1296351af373126f9d1efaeba8dd2418d3bbbdbdc1ad88e77c314489442b9af1a6
7
+ data.tar.gz: ddba1656e7e7cec32423d0c1e6ada7494972b620666e1dc3daa70b7bf5c1210a0ad50911778b74e8c06d2f4befbe3f644c44bf38b05c8b97a266fdf3d0afd49f
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.2] - 2025-10-25
4
+
5
+ * Added `Result#unwrap_err`
6
+
7
+ ## [0.2.1] - 2025-10-25
8
+
9
+ * Fixed missed error not returning `Decoding::Failure`
10
+
3
11
  ## [0.2.0] - 2025-10-25
4
12
 
5
13
  * Added `decode_hash`, `regexp` and `original` decoders
@@ -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
@@ -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
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Decoding
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
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.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arjan van der Gaag