iron_oxide 0.0.1

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: 689ead17e28d9c0bf0b7b70d8f607ad8cfb838937159568b13b8acc0c832de5b
4
+ data.tar.gz: 243c1f6494cf34f30a251792501f3cddb89512a290ca1a4a9db1d8cd096446cc
5
+ SHA512:
6
+ metadata.gz: 5f931fa4c52470ac1b74d473337940e5a19c13fe496f061d62bb54fa26949a6dc36b25577c1ce6e497cba4b84355f89be2351e50ebf2e3ac675035e7a8e51065
7
+ data.tar.gz: 21a8a63cc77150ad9b812a2add7e81ff4aad80c069951bc8c326bea3c28565fe761cec3c035a8f98089194dd0f1051524ba96e5e9deacf3c55fd6d0c9162035a
@@ -0,0 +1,4 @@
1
+ # You can read more about CODEOWNERS at
2
+ # https://help.github.com/github/creating-cloning-and-archiving-repositories/about-code-owners
3
+
4
+ * @fnando
@@ -0,0 +1,4 @@
1
+ # These are supported funding model platforms
2
+ ---
3
+ github: [fnando]
4
+ custom: ["https://paypal.me/[USER]/๐Ÿ•"]
@@ -0,0 +1,41 @@
1
+ ---
2
+ name: "๐Ÿ› Bug Report"
3
+ about: Report a reproducible bug or regression.
4
+ title: 'Bug: '
5
+ labels: 'Status: Unconfirmed'
6
+
7
+ ---
8
+
9
+ <!--
10
+ - Please provide a clear and concise description of what the bug is.
11
+ - If possible, add an example reproducing your issue.
12
+ - Please test using the latest version of iron_oxide
13
+ to make sure your issue has not already been fixed.
14
+ -->
15
+
16
+ ## Description
17
+
18
+ [Add bug description here]
19
+
20
+ ## How to reproduce
21
+
22
+ [Add steps on how to reproduce this issue]
23
+
24
+ ## What do you expect
25
+
26
+ [Describe what do you expect to happen]
27
+
28
+ ## What happened instead
29
+
30
+ [Describe the actual results]
31
+
32
+ ## Software:
33
+
34
+ - Gem version: [Add gem version here]
35
+ - Ruby version: [Add version here]
36
+
37
+ ## Full backtrace
38
+
39
+ ```text
40
+ [Paste full backtrace here]
41
+ ```
@@ -0,0 +1,5 @@
1
+ ---
2
+ contact_links:
3
+ - name: "๐Ÿคจ Q&A"
4
+ url: https://github.com/fnando/iron_oxide/discussions/new?category=q-a
5
+ about: Have a question? Ask it away here!
@@ -0,0 +1,23 @@
1
+ ---
2
+ name: "๐Ÿ’ก Feature request"
3
+ about: Have an idea that may be useful? Make a suggestion!
4
+ title: 'Feature Request: '
5
+ labels: 'Feature request'
6
+
7
+ ---
8
+
9
+ ## Description
10
+
11
+ _A clear and concise description of what the problem is._
12
+
13
+ ## Describe the solution
14
+
15
+ _A clear and concise description of what you want to happen._
16
+
17
+ ## Alternatives you considered
18
+
19
+ _A clear and concise description of any alternative solutions or features you've considered._
20
+
21
+ ## Additional context
22
+
23
+ _Add any other context, screenshots, links, etc about the feature request here._
@@ -0,0 +1,38 @@
1
+ <!--
2
+ If you're making a doc PR or something tiny where the below is irrelevant,
3
+ delete this template and use a short description, but in your description aim to
4
+ include both what the change is, and why it is being made, with enough context
5
+ for anyone to understand.
6
+ -->
7
+
8
+ <details>
9
+ <summary>PR Checklist</summary>
10
+
11
+ ### PR Structure
12
+
13
+ - [ ] This PR has reasonably narrow scope (if not, break it down into smaller
14
+ PRs).
15
+ - [ ] This PR avoids mixing refactoring changes with feature changes (split into
16
+ two PRs otherwise).
17
+ - [ ] This PR's title starts is concise and descriptive.
18
+
19
+ ### Thoroughness
20
+
21
+ - [ ] This PR adds tests for the most critical parts of the new functionality or
22
+ fixes.
23
+ - [ ] I've updated any docs, `.md` files, etcโ€ฆ affected by this change.
24
+
25
+ </details>
26
+
27
+ ### What
28
+
29
+ [TODO: Short statement about what is changing.]
30
+
31
+ ### Why
32
+
33
+ [TODO: Why this change is being made. Include any context required to understand
34
+ the why.]
35
+
36
+ ### Known limitations
37
+
38
+ [TODO or N/A]
@@ -0,0 +1,15 @@
1
+ ---
2
+ # Documentation:
3
+ # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
4
+
5
+ version: 2
6
+ updates:
7
+ - package-ecosystem: "github-actions"
8
+ directory: "/"
9
+ schedule:
10
+ interval: "daily"
11
+
12
+ - package-ecosystem: bundler
13
+ directory: "/"
14
+ schedule:
15
+ interval: "daily"
@@ -0,0 +1,52 @@
1
+ ---
2
+ name: ruby-tests
3
+
4
+ on:
5
+ pull_request_target:
6
+ push:
7
+ branches:
8
+ - main
9
+ workflow_dispatch:
10
+ inputs: {}
11
+
12
+ jobs:
13
+ build:
14
+ name: Tests with Ruby ${{ matrix.ruby }} and ${{ matrix.gemfile }}
15
+ runs-on: "ubuntu-latest"
16
+ if: |
17
+ github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target' ||
18
+ github.actor != 'dependabot[bot]'
19
+ strategy:
20
+ fail-fast: false
21
+ matrix:
22
+ ruby: ["3.3"]
23
+ gemfile:
24
+ - Gemfile
25
+
26
+ steps:
27
+ - uses: actions/checkout@v1
28
+
29
+ - uses: actions/cache@v2
30
+ with:
31
+ path: vendor/bundle
32
+ key: >
33
+ ${{ runner.os }}-${{ matrix.ruby }}-gems-${{ hashFiles(matrix.gemfile) }}
34
+
35
+ - name: Set up Ruby
36
+ uses: ruby/setup-ruby@v1
37
+ with:
38
+ ruby-version: ${{ matrix.ruby }}
39
+
40
+ - name: Install gem dependencies
41
+ env:
42
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
43
+ run: |
44
+ gem install bundler
45
+ bundle config path vendor/bundle
46
+ bundle update --jobs 4 --retry 3
47
+
48
+ - name: Run Tests
49
+ env:
50
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
51
+ run: |
52
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+ *.log
11
+ *.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ ---
2
+ inherit_gem:
3
+ rubocop-fnando: .rubocop.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.3
7
+ NewCops: enable
8
+ Exclude:
9
+ - vendor/**/*
10
+ - gemfiles/**/*
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ <!--
4
+ Prefix your message with one of the following:
5
+
6
+ - [Added] for new features.
7
+ - [Changed] for changes in existing functionality.
8
+ - [Deprecated] for soon-to-be removed features.
9
+ - [Removed] for now removed features.
10
+ - [Fixed] for any bug fixes.
11
+ - [Security] in case of vulnerabilities.
12
+ -->
13
+
14
+ ## 0.0.1
15
+
16
+ - Initial release.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at me@fnando.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,79 @@
1
+ # Contributing to iron_oxide
2
+
3
+ ๐Ÿ‘๐ŸŽ‰ First off, thanks for taking the time to contribute! ๐ŸŽ‰๐Ÿ‘
4
+
5
+ The following is a set of guidelines for contributing to this project. These are
6
+ mostly guidelines, not rules. Use your best judgment, and feel free to propose
7
+ changes to this document in a pull request.
8
+
9
+ ## Code of Conduct
10
+
11
+ Everyone interacting in this project's codebases, issue trackers, chat rooms and
12
+ mailing lists is expected to follow the [code of conduct](https://github.com/fnando/iron_oxide/blob/main/CODE_OF_CONDUCT.md).
13
+
14
+ ## Reporting bugs
15
+
16
+ This section guides you through submitting a bug report. Following these
17
+ guidelines helps maintainers and the community understand your report, reproduce
18
+ the behavior, and find related reports.
19
+
20
+ - Before creating bug reports, please check the open issues; somebody may
21
+ already have submitted something similar, and you may not need to create a new
22
+ one.
23
+ - When you are creating a bug report, please include as many details as
24
+ possible, with an example reproducing the issue.
25
+
26
+ ## Contributing with code
27
+
28
+ Before making any radicals changes, please make sure you discuss your intention
29
+ by [opening an issue on Github](https://github.com/fnando/iron_oxide/issues).
30
+
31
+ When you're ready to make your pull request, follow checklist below to make sure
32
+ your contribution is according to how this project works.
33
+
34
+ 1. [Fork](https://help.github.com/forking/) iron_oxide
35
+ 2. Create a topic branch - `git checkout -b my_branch`
36
+ 3. Make your changes using [descriptive commit messages](#commit-messages)
37
+ 4. Update CHANGELOG.md describing your changes by adding an entry to the
38
+ "Unreleased" section. If this section is not available, create one right
39
+ before the last version.
40
+ 5. Push to your branch - `git push origin my_branch`
41
+ 6. [Create a pull request](https://help.github.com/articles/creating-a-pull-request)
42
+ 7. That's it!
43
+
44
+ ## Styleguides
45
+
46
+ ### Commit messages
47
+
48
+ - Use the present tense ("Add feature" not "Added feature")
49
+ - Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
50
+ - Limit the first line to 72 characters or less
51
+ - Reference issues and pull requests liberally after the first line
52
+
53
+ ### Changelog
54
+
55
+ - Add a message describing your changes to the "Unreleased" section. The
56
+ changelog message should follow the same style as the commit message.
57
+ - Prefix your message with one of the following:
58
+ - `[Added]` for new features.
59
+ - `[Changed]` for changes in existing functionality.
60
+ - `[Deprecated]` for soon-to-be removed features.
61
+ - `[Removed]` for now removed features.
62
+ - `[Fixed]` for any bug fixes.
63
+ - `[Security]` in case of vulnerabilities.
64
+
65
+ ### Ruby code
66
+
67
+ - This project uses [Rubocop](https://rubocop.org) to enforce code style. Before
68
+ submitting your changes, make sure your tests are passing and code conforms to
69
+ the expected style by running `rake`.
70
+ - Do not change the library version. This will be done by the maintainer
71
+ whenever a new version is about to be released.
72
+
73
+ ### JavaScript code
74
+
75
+ - This project uses [ESLint](https://eslint.org) to enforce code style. Before
76
+ submitting your changes, make sure your tests are passing and code conforms to
77
+ the expected style by running `yarn test:ci`.
78
+ - Do not change the library version. This will be done by the maintainer
79
+ whenever a new version is about to be released.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ # The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Nando Vieira
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # iron_oxide
2
+
3
+ [![Tests](https://github.com/fnando/iron_oxide/workflows/ruby-tests/badge.svg)](https://github.com/fnando/iron_oxide)
4
+ [![Gem](https://img.shields.io/gem/v/iron_oxide.svg)](https://rubygems.org/gems/iron_oxide)
5
+ [![Gem](https://img.shields.io/gem/dt/iron_oxide.svg)](https://rubygems.org/gems/iron_oxide)
6
+ [![MIT License](https://img.shields.io/:License-MIT-blue.svg)](https://tldrlegal.com/license/mit-license)
7
+
8
+ An experiment that brings most of Rust's `Result` and `Option` patterns to Ruby.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ gem install iron_oxide
14
+ ```
15
+
16
+ Or add the following line to your project's Gemfile:
17
+
18
+ ```ruby
19
+ gem "iron_oxide"
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require "iron_oxide"
26
+
27
+ include IronOxide::Aliases
28
+
29
+ Some(1).some?
30
+ None.some?
31
+ None.none?
32
+
33
+ Ok(1).ok?
34
+ Err("oh noes!").ok?
35
+ ```
36
+
37
+ Most of `Result` and `Option` has been ported to Ruby. You can
38
+ [check tests](https://github.com/fnando/iron_oxide/tree/main/test/iron_oxide)
39
+ for examples.
40
+
41
+ ## Maintainer
42
+
43
+ - [Nando Vieira](https://github.com/fnando)
44
+
45
+ ## Contributors
46
+
47
+ - https://github.com/fnando/iron_oxide/contributors
48
+
49
+ ## Contributing
50
+
51
+ For more details about how to contribute, please read
52
+ https://github.com/fnando/iron_oxide/blob/main/CONTRIBUTING.md.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the
57
+ [MIT License](https://opensource.org/licenses/MIT). A copy of the license can be
58
+ found at https://github.com/fnando/iron_oxide/blob/main/LICENSE.md.
59
+
60
+ ## Code of Conduct
61
+
62
+ Everyone interacting in the iron_oxide project's codebases, issue trackers, chat
63
+ rooms and mailing lists is expected to follow the
64
+ [code of conduct](https://github.com/fnando/iron_oxide/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "rubocop/rake_task"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["test/**/*_test.rb"]
11
+ end
12
+
13
+ RuboCop::RakeTask.new
14
+
15
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "iron_oxide"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ begin
11
+ require "pry"
12
+ Pry.start
13
+ rescue LoadError
14
+ require "irb"
15
+ IRB.start(__FILE__)
16
+ end
data/bin/setup ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+
5
+ IFS=$'\n\t'
6
+ set -vx
7
+
8
+ bundle install
9
+
10
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/iron_oxide/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "iron_oxide"
7
+ spec.version = IronOxide::VERSION
8
+ spec.authors = ["Nando Vieira"]
9
+ spec.email = ["me@fnando.com"]
10
+ spec.metadata = {"rubygems_mfa_required" => "true"}
11
+
12
+ spec.summary = "An experiment that brings some Rust patterns to Ruby."
13
+ spec.description = spec.summary
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.3.0")
16
+
17
+ github_url = "https://github.com/fnando/iron_oxide"
18
+ github_tree_url = "#{github_url}/tree/v#{spec.version}"
19
+
20
+ spec.homepage = github_url
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["bug_tracker_uri"] = "#{github_url}/issues"
23
+ spec.metadata["source_code_uri"] = github_tree_url
24
+ spec.metadata["changelog_uri"] = "#{github_tree_url}/CHANGELOG.md"
25
+ spec.metadata["documentation_uri"] = "#{github_tree_url}/README.md"
26
+ spec.metadata["license_uri"] = "#{github_tree_url}/LICENSE.md"
27
+
28
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
29
+ `git ls-files -z`
30
+ .split("\x0")
31
+ .reject {|f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "minitest"
39
+ spec.add_development_dependency "minitest-utils"
40
+ spec.add_development_dependency "pry-meta"
41
+ spec.add_development_dependency "rake"
42
+ spec.add_development_dependency "rubocop"
43
+ spec.add_development_dependency "rubocop-fnando"
44
+ spec.add_development_dependency "simplecov"
45
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronOxide
4
+ module Aliases
5
+ def Some(value) # rubocop:disable Naming/MethodName
6
+ Option::Some(value)
7
+ end
8
+
9
+ None = Option::None
10
+
11
+ def Ok(value) # rubocop:disable Naming/MethodName
12
+ Result::Ok(value)
13
+ end
14
+
15
+ def Err(error) # rubocop:disable Naming/MethodName
16
+ Result::Err(error)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,214 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronOxide
4
+ module Option
5
+ class Base
6
+ attr_reader :value
7
+
8
+ # Checks if the instance represents a `Some` value.
9
+ def some?
10
+ instance_of?(SomeClass)
11
+ end
12
+
13
+ # Checks if the instance represents a `None` value.
14
+ def none?
15
+ instance_of?(NoneClass)
16
+ end
17
+
18
+ # Evaluates a block for a `Some` value and checks its result.
19
+ def some_and?
20
+ some? && yield(value)
21
+ end
22
+
23
+ # Evaluates a block for a `None` value and checks its result.
24
+ def none_or?
25
+ none? || yield(value)
26
+ end
27
+
28
+ # Converts a `Some` value into an array or returns an empty array
29
+ # for `None`.
30
+ def as_slice
31
+ some? ? [value] : []
32
+ end
33
+
34
+ # Retrieves the contained value or raises an error with a custom message.
35
+ def expect(message)
36
+ some? ? value : raise(ExpectError, message)
37
+ end
38
+
39
+ # Retrieves the contained value or raises an error for a `None` value.
40
+ def unwrap
41
+ some? ? value : raise(ExpectError, "error unwrapping None")
42
+ end
43
+
44
+ # Retrieves the contained value or returns a provided default value.
45
+ def unwrap_or(default)
46
+ some? ? value : default
47
+ end
48
+
49
+ # Retrieves the contained value or computes a default value using a block.
50
+ def unwrap_or_else
51
+ some? ? value : yield
52
+ end
53
+
54
+ # Applies a transformation to a `Some` value using a block.
55
+ def map
56
+ some? ? Option::Some(yield(value)) : None
57
+ end
58
+
59
+ # Checks equality with another `Option` value.
60
+ def ==(other)
61
+ Option.option?(other) &&
62
+ ((none? && other.none?) ||
63
+ (some? && other.some? && value == other.value))
64
+ end
65
+
66
+ # Applies a transformation to a `Some` value or returns a default value
67
+ # for `None`.
68
+ def map_or(default)
69
+ some? ? yield(value) : default
70
+ end
71
+
72
+ # Converts a `Some` value into an `Ok` result or returns an `Err` result
73
+ # with a provided error.
74
+ def ok_or(error)
75
+ some? ? Result::Ok(value) : Result::Err(error)
76
+ end
77
+
78
+ # Converts a `Some` value into an `Ok` result or computes an `Err` result
79
+ # using a block.
80
+ def ok_or_else
81
+ some? ? Result::Ok(value) : Result::Err(yield)
82
+ end
83
+
84
+ # Combines the `Option` with another, returning `None` if the instance
85
+ # is `None`.
86
+ def and(other)
87
+ none? ? None : other
88
+ end
89
+
90
+ # Transforms the contained value of a `Some` instance using a block or
91
+ # returns `None`.
92
+ def and_then
93
+ return None if none?
94
+
95
+ yield(value).tap do |other|
96
+ next if Option.option?(other)
97
+
98
+ raise TypeError,
99
+ "expected block to return an Option; got #{other.class.name}"
100
+ end
101
+ end
102
+
103
+ # Filters a `Some` value based on a predicate block.
104
+ def filter
105
+ return None if none?
106
+
107
+ yield(value) ? self : None
108
+ end
109
+
110
+ # Combines the `Option` with another, returning the first `Some`
111
+ # encountered.
112
+ def or(other)
113
+ some? ? self : other
114
+ end
115
+
116
+ # Combines the `Option` with another, using a block to produce an
117
+ # alternative value.
118
+ def or_else
119
+ some? ? self : yield
120
+ end
121
+
122
+ # Computes the exclusive-or of two `Option` values.
123
+ def xor(other)
124
+ return Option::None if some? && other.some?
125
+ return Option::None if none? && other.none?
126
+ return self if some?
127
+
128
+ other
129
+ end
130
+
131
+ # Combines two `Option` values into a single `Option` containing a tuple
132
+ # of their values.
133
+ def zip(other)
134
+ some? && other.some? ? Option::Some([value, other.value]) : None
135
+ end
136
+
137
+ # Splits the contained value into two `Option` values if it is a pair.
138
+ def unzip
139
+ return None unless some?
140
+
141
+ unless value.respond_to?(:to_a)
142
+ raise ArgumentError,
143
+ "expected value to respond to #to_a"
144
+ end
145
+
146
+ items = value.to_a
147
+ size = items.size
148
+
149
+ unless size == 2
150
+ raise ArgumentError,
151
+ "expected value to have exactly 2 items; got #{size}"
152
+ end
153
+
154
+ items.map { Option::Some(_1) }
155
+ end
156
+
157
+ # Transposes a contained `Result` value to swap the `Option` and `Result`
158
+ # structure.
159
+ def transpose
160
+ return Result::Ok(Option::None) if none?
161
+
162
+ unless Result.result?(value)
163
+ raise TypeError,
164
+ "expected value to be a Result; got #{value.class.name}"
165
+ end
166
+
167
+ return Result::Ok(Option::Some(value.value)) if some? && value.ok?
168
+
169
+ Result::Err(value.value) if some? && value.err?
170
+ end
171
+
172
+ # Flattens nested `Option` values into a single `Option`.
173
+ def flatten
174
+ unless Option.option?(value)
175
+ raise TypeError,
176
+ "expected value to be an Option; got #{value.class.name}"
177
+ end
178
+
179
+ if none? || value.none?
180
+ None
181
+ else
182
+ Option::Some(value.value)
183
+ end
184
+ end
185
+ end
186
+
187
+ class SomeClass < Base
188
+ def initialize(value)
189
+ super()
190
+ @value = value
191
+ end
192
+ end
193
+
194
+ class NoneClass < Base
195
+ def initialize(*)
196
+ super()
197
+ @value = self
198
+ end
199
+ end
200
+
201
+ # Creates a new `Some` instance with a specified value.
202
+ def self.Some(value) # rubocop:disable Naming/MethodName
203
+ SomeClass.new(value)
204
+ end
205
+
206
+ # The `None` instance is a singleton.
207
+ None = NoneClass.new.freeze
208
+
209
+ # Checks if a given value is an `Option`.
210
+ def self.option?(value)
211
+ value.is_a?(Base)
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,195 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronOxide
4
+ module Result
5
+ class Base
6
+ attr_reader :value
7
+
8
+ def initialize(value)
9
+ @value = value
10
+ end
11
+
12
+ # Compares the current result with another result.
13
+ def ==(other)
14
+ Result.result?(other) &&
15
+ ((err? && other.err?) || (ok? && other.ok? && value == other.value))
16
+ end
17
+
18
+ # Checks if the result is of type `Ok`.
19
+ def ok?
20
+ instance_of?(OkClass)
21
+ end
22
+
23
+ # Checks if the result is of type `Err`.
24
+ def err?
25
+ instance_of?(ErrClass)
26
+ end
27
+
28
+ # Checks if the result is `Ok` and the provided block evaluates to `true`.
29
+ def ok_and?
30
+ ok? && yield(value)
31
+ end
32
+
33
+ # Checks if the result is `Err` and the provided block evaluates
34
+ # to `true`.
35
+ def err_and?
36
+ err? && yield(value)
37
+ end
38
+
39
+ # Converts an `Ok` result into an `Option::Some` or
40
+ # returns `Option::None`.
41
+ def ok
42
+ ok? ? Option::Some(value) : Option::None
43
+ end
44
+
45
+ # Converts an `Err` result into an `Option::Some` or
46
+ # returns `Option::None`.
47
+ def err
48
+ err? ? Option::Some(value) : Option::None
49
+ end
50
+
51
+ # Maps the value of an `Ok` result using the provided block; otherwise,
52
+ # returns itself.
53
+ def map
54
+ ok? ? Result::Ok(yield(value)) : self
55
+ end
56
+
57
+ # Maps the value of an `Ok` result or returns a default value.
58
+ def map_or(default)
59
+ ok? ? yield(value) : default
60
+ end
61
+
62
+ # Wraps the `Err` into another error `Err` using the provided block;
63
+ # otherwise, returns itself.
64
+ def map_err
65
+ err? ? Result::Err(yield(value)) : self
66
+ end
67
+
68
+ # Unwraps the value if the result is `Ok`, raising an error otherwise.
69
+ def expect(message)
70
+ ok? ? value : raise(ExpectError, message)
71
+ end
72
+
73
+ # Unwraps the value if the result is `Ok`, raising an error otherwise.
74
+ def unwrap
75
+ ok? ? value : raise(ExpectError, "error unwrapping Err")
76
+ end
77
+
78
+ # Unwraps the value if the result is `Ok`, or returns the default value
79
+ # otherwise.
80
+ def unwrap_or(default)
81
+ ok? ? value : default
82
+ end
83
+
84
+ # Unwraps the value if the result is `Ok`, or computes a value using the
85
+ # provided block.
86
+ def unwrap_or_else
87
+ ok? ? value : yield
88
+ end
89
+
90
+ # Unwraps the error value if the result is `Err`, raising an error
91
+ # otherwise.
92
+ def expect_err
93
+ raise(ExpectError, "expected Err; got Ok<#{value.inspect}>") unless err?
94
+
95
+ value
96
+ end
97
+ alias unwrap_err expect_err
98
+
99
+ # Combines this result with another, returning the second result if both
100
+ # are `Ok`.
101
+ def and(other)
102
+ unless other.is_a?(Result::Base)
103
+ raise TypeError,
104
+ "expected Result; got #{other.class.name}"
105
+ end
106
+
107
+ other_ok = other.instance_of?(Result::OkClass)
108
+
109
+ return other.value if ok? && other_ok
110
+ return self if err? && other_ok
111
+
112
+ other
113
+ end
114
+
115
+ # Combines this result with another, transforming the value of `Ok` using
116
+ # the provided block.
117
+ def and_then
118
+ return self if err?
119
+
120
+ Result::Ok(yield(value))
121
+ end
122
+
123
+ # Combines this result with another, returning the first `Ok` encountered.
124
+ def or(other)
125
+ unless other.is_a?(Result::Base)
126
+ raise TypeError, "expected Result; got #{other.class.name}"
127
+ end
128
+
129
+ return self if ok?
130
+ return other if other.ok?
131
+
132
+ err? ? self : other
133
+ end
134
+
135
+ # Combines this result with another, transforming the error value using
136
+ # the provided block.
137
+ def or_else
138
+ return self if ok?
139
+
140
+ yield(value).tap do |other|
141
+ unless Result.result?(other)
142
+ raise TypeError, "expected Result; got #{other.class.name}"
143
+ end
144
+ end
145
+ end
146
+
147
+ # Converts a result of an `Option` into an `Option` of a result.
148
+ def transpose
149
+ if ok? && !Option.option?(value)
150
+ raise TypeError,
151
+ "expected value to be Option; got #{value.class.name}"
152
+ end
153
+
154
+ return Option::Some(Result::Ok(value.value)) if ok? && value.some?
155
+ return Option::None if ok? && value.none?
156
+
157
+ Option::Some(self)
158
+ end
159
+
160
+ # Flattens a nested result if the value of `Ok` is another result.
161
+ def flatten
162
+ if ok? && !Result.result?(value)
163
+ raise TypeError,
164
+ "expected value to be Result; got #{value.class.name}"
165
+ end
166
+
167
+ return Result::Ok(value.value) if ok? && value.ok?
168
+ return value if ok? && value.err?
169
+
170
+ self
171
+ end
172
+ end
173
+
174
+ class OkClass < Base
175
+ end
176
+
177
+ class ErrClass < Base
178
+ end
179
+
180
+ # Factory method to create a new `Ok` result.
181
+ def self.Ok(value) # rubocop:disable Naming/MethodName
182
+ OkClass.new(value)
183
+ end
184
+
185
+ # Factory method to create a new `Err` result.
186
+ def self.Err(error) # rubocop:disable Naming/MethodName
187
+ ErrClass.new(error)
188
+ end
189
+
190
+ # Checks if a value is a result.
191
+ def self.result?(value)
192
+ value.is_a?(Base)
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronOxide
4
+ VERSION = "0.0.1"
5
+ end
data/lib/iron_oxide.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronOxide
4
+ ExpectError = Class.new(StandardError)
5
+
6
+ require "iron_oxide/version"
7
+ require "iron_oxide/option"
8
+ require "iron_oxide/result"
9
+ require "iron_oxide/aliases"
10
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iron_oxide
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nando Vieira
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-meta
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-fnando
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: An experiment that brings some Rust patterns to Ruby.
112
+ email:
113
+ - me@fnando.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".github/CODEOWNERS"
119
+ - ".github/FUNDING.yml"
120
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
121
+ - ".github/ISSUE_TEMPLATE/config.yml"
122
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
123
+ - ".github/PULL_REQUEST_TEMPLATE.md"
124
+ - ".github/dependabot.yml"
125
+ - ".github/workflows/ruby-tests.yml"
126
+ - ".gitignore"
127
+ - ".rubocop.yml"
128
+ - CHANGELOG.md
129
+ - CODE_OF_CONDUCT.md
130
+ - CONTRIBUTING.md
131
+ - Gemfile
132
+ - LICENSE.md
133
+ - README.md
134
+ - Rakefile
135
+ - bin/console
136
+ - bin/setup
137
+ - iron_oxide.gemspec
138
+ - lib/iron_oxide.rb
139
+ - lib/iron_oxide/aliases.rb
140
+ - lib/iron_oxide/option.rb
141
+ - lib/iron_oxide/result.rb
142
+ - lib/iron_oxide/version.rb
143
+ homepage: https://github.com/fnando/iron_oxide
144
+ licenses:
145
+ - MIT
146
+ metadata:
147
+ rubygems_mfa_required: 'true'
148
+ homepage_uri: https://github.com/fnando/iron_oxide
149
+ bug_tracker_uri: https://github.com/fnando/iron_oxide/issues
150
+ source_code_uri: https://github.com/fnando/iron_oxide/tree/v0.0.1
151
+ changelog_uri: https://github.com/fnando/iron_oxide/tree/v0.0.1/CHANGELOG.md
152
+ documentation_uri: https://github.com/fnando/iron_oxide/tree/v0.0.1/README.md
153
+ license_uri: https://github.com/fnando/iron_oxide/tree/v0.0.1/LICENSE.md
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 3.3.0
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubygems_version: 3.5.22
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: An experiment that brings some Rust patterns to Ruby.
173
+ test_files: []