File: result.rb

package info (click to toggle)
ruby-dry-types 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 504 kB
  • sloc: ruby: 3,059; makefile: 4
file content (84 lines) | stat: -rw-r--r-- 1,479 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

require 'dry/equalizer'

module Dry
  module Types
    # Result class used by {Type#try}
    #
    # @api public
    class Result
      include Dry::Equalizer(:input, inspect: false, immutable: true)

      # @return [Object]
      attr_reader :input

      # @param [Object] input
      #
      # @api private
      def initialize(input)
        @input = input
      end

      # Success result
      #
      # @api public
      class Success < Result
        # @return [true]
        #
        # @api public
        def success?
          true
        end

        # @return [false]
        #
        # @api public
        def failure?
          false
        end
      end

      # Failure result
      #
      # @api public
      class Failure < Result
        include Dry::Equalizer(:input, :error, inspect: false, immutable: true)

        # @return [#to_s]
        attr_reader :error

        # @param [Object] input
        #
        # @param [#to_s] error
        #
        # @api private
        def initialize(input, error)
          super(input)
          @error = error
        end

        # @return [String]
        #
        # @api private
        def to_s
          error.to_s
        end

        # @return [false]
        #
        # @api public
        def success?
          false
        end

        # @return [true]
        #
        # @api public
        def failure?
          true
        end
      end
    end
  end
end