rs-result 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 85d0bd21d2e9cdae59b4b8824bbacbfef2780792b8b3398d3a5e962efab3078d
4
+ data.tar.gz: dff2d34b2a2877d1ce5360a2ab979bb4dfde8b595c0459d67926c3f29238bd58
5
+ SHA512:
6
+ metadata.gz: 761f77d7b69bbfceccfdd5a8ebb3a1b5e79ed72e405bc4325326230c3f94ccd9dc66486d25cfc44eb7ecd389325c318f167fa4ca8a0e91a0008f3b62f250bfe2
7
+ data.tar.gz: d5af5f8d085a919590858d332b974ed1e79daa46e5b1e0ad41fad448209c6668aa4b66c34cbbcb23e18da91a04664eaf70a8500ad11e49e2f51ebd91e76eabbf
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ inherit_gem:
2
+ rubocop-config-crystal: .rubocop.yml
3
+
4
+ Metrics/BlockLength:
5
+ Enabled: false
6
+
7
+ Style/MethodCallWithArgsParentheses:
8
+ Enabled: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-11-06
4
+
5
+ - Initial release
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Rs::Result
2
+
3
+ Bring the Rust [Result Option] type to Ruby
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add rs-result
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install rs-result
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require "rs/result"
19
+
20
+ x = Some.new(2)
21
+ x = None.new
22
+
23
+ x = Ok.new(-3)
24
+ x = Err.new("Some error message")
25
+ ```
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rs-result.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: [:spec, :rubocop]
data/lib/rs/option.rb ADDED
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "result/version"
4
+ module Rs
5
+ # https://doc.rust-lang.org/std/option/index.html
6
+ # https://doc.rust-lang.org/std/option/enum.Option.html
7
+ # https://doc.rust-lang.org/src/core/option.rs.html
8
+ module Option
9
+ class ArgumentError < StandardError; end
10
+ class UnwrapNone < StandardError; end
11
+
12
+ def some?
13
+ @some
14
+ end
15
+
16
+ def some_and
17
+ if !@some
18
+ false
19
+ elsif block_given?
20
+ yield(@value)
21
+ else
22
+ @some
23
+ end
24
+ end
25
+
26
+ def none?
27
+ !@some
28
+ end
29
+
30
+ def none_or
31
+ if !@some
32
+ true
33
+ elsif block_given?
34
+ yield(@value)
35
+ else
36
+ !@some
37
+ end
38
+ end
39
+
40
+ def expect(msg)
41
+ @some ? @value : raise(UnwrapNone, msg)
42
+ end
43
+
44
+ def unwrap
45
+ @some ? @value : raise(UnwrapNone)
46
+ end
47
+
48
+ def unwrap_or(default = nil)
49
+ if @some
50
+ @value
51
+ elsif block_given?
52
+ yield
53
+ else
54
+ default
55
+ end
56
+ end
57
+
58
+ def unwrap_or_else(&block)
59
+ @some ? @value : block.call
60
+ end
61
+ end
62
+ end
63
+
64
+ class Some
65
+ include Rs::Option
66
+
67
+ def initialize(value)
68
+ if value == nil
69
+ raise(Rs::Option::ArgumentError, "Some value cannot be nil")
70
+ end
71
+
72
+ @value = value
73
+ @some = true
74
+ end
75
+ end
76
+
77
+ class None
78
+ include Rs::Option
79
+
80
+ def initialize
81
+ @some = false
82
+ end
83
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rs
4
+ module Result
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/rs/result.rb ADDED
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "option"
4
+ require_relative "result/version"
5
+
6
+ module Rs
7
+ # https://doc.rust-lang.org/std/result/index.html
8
+ # https://doc.rust-lang.org/std/result/enum.Result.html
9
+ # https://doc.rust-lang.org/src/core/result.rs.html
10
+ module Result
11
+ class ArgumentError < StandardError; end
12
+ class UnwrapOnErr < StandardError; end
13
+ class UnwrapErrOnOk < StandardError; end
14
+
15
+ def ok?
16
+ @ok
17
+ end
18
+
19
+ def ok_and
20
+ if !@ok
21
+ false
22
+ elsif block_given?
23
+ yield(@value)
24
+ else
25
+ @ok
26
+ end
27
+ end
28
+
29
+ def err?
30
+ !@ok
31
+ end
32
+
33
+ def err_and
34
+ if @ok
35
+ false
36
+ elsif block_given?
37
+ yield(@value)
38
+ else
39
+ !@ok
40
+ end
41
+ end
42
+
43
+ def ok
44
+ if @ok
45
+ Some.new(@value)
46
+ else
47
+ None.new
48
+ end
49
+ end
50
+
51
+ def err
52
+ if !@ok
53
+ Some.new(@value)
54
+ else
55
+ None.new
56
+ end
57
+ end
58
+
59
+ def expect(msg)
60
+ @ok ? @value : raise(UnwrapOnErr, "#{msg}: #{@value}")
61
+ end
62
+
63
+ def unwrap
64
+ @ok ? @value : raise(UnwrapOnErr, "called `Result::unwrap()` on an `Err` value: #{@value}")
65
+ end
66
+
67
+ def unwrap_or(default = nil)
68
+ if @ok
69
+ @value
70
+ elsif block_given?
71
+ yield
72
+ else
73
+ default
74
+ end
75
+ end
76
+
77
+ def unwrap_or_else(&block)
78
+ @ok ? @value : block.call(@value)
79
+ end
80
+
81
+ def expect_err(msg)
82
+ @ok ? raise(UnwrapErrOnOk, "#{msg}: #{@value}") : @value
83
+ end
84
+
85
+ def unwrap_err
86
+ @ok ? raise(UnwrapErrOnOk, "called `Result::unwrap_err()` on an `Ok` value: #{@value}") : @value
87
+ end
88
+ end
89
+ end
90
+
91
+ class Ok
92
+ include Rs::Result
93
+ def initialize(value)
94
+ if value == nil
95
+ raise(Rs::Result::ArgumentError, "Ok value cannot be nil")
96
+ end
97
+
98
+ @value = value
99
+ @ok = true
100
+ end
101
+ end
102
+
103
+ class Err
104
+ include Rs::Result
105
+ def initialize(error)
106
+ @value = error
107
+ @ok = false
108
+ end
109
+ end
data/sig/rs/result.rbs ADDED
@@ -0,0 +1,6 @@
1
+ module Rs
2
+ module Result
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rs-result
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - initdc
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-11-06 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Bring the Rust [Result Option] type to Ruby
13
+ email:
14
+ - initd@outlook.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".rspec"
20
+ - ".rubocop.yml"
21
+ - CHANGELOG.md
22
+ - README.md
23
+ - Rakefile
24
+ - lib/rs/option.rb
25
+ - lib/rs/result.rb
26
+ - lib/rs/result/version.rb
27
+ - sig/rs/result.rbs
28
+ homepage: https://rubygems.org/gems/rs-result
29
+ licenses: []
30
+ metadata:
31
+ allowed_push_host: https://rubygems.org
32
+ homepage_uri: https://rubygems.org/gems/rs-result
33
+ source_code_uri: https://github.com/initdc/rs-result
34
+ changelog_uri: https://github.com/initdc/rs-result/blob/master/CHANGELOG.md
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.7.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.6.2
50
+ specification_version: 4
51
+ summary: Bring the Rust [Result Option] type to Ruby
52
+ test_files: []