Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
errgonomic (0.5.1)
errgonomic (0.6.0)
concurrent-ruby (~> 1.0)

GEM
Expand Down Expand Up @@ -303,4 +303,4 @@ DEPENDENCIES
yard-doctest (~> 0.1)

BUNDLED WITH
2.6.9
2.6.6
2 changes: 1 addition & 1 deletion gemset.nix
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
path = ./.;
type = "path";
};
version = "0.5.1";
version = "0.6.0";
};
erubi = {
groups = ["default" "development"];
Expand Down
35 changes: 35 additions & 0 deletions lib/errgonomic/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ def map(&block)
Ok(block.call(value))
end

# Map the Err(e) to an Err(f), preserving the Ok
#
# @example
# Ok(:Alice).map_err { |_e| :Bob } # => Ok(:Alice)
# Err(:bob).map_err { |e| e.capitalize } # => Err(:Bob)
def map_err(&block)
return self if ok?

Err(block.call(value))
end

# Refuse to serialize an unwrapped Result as a String. Results must be
# correctly handled to access their inner value.
#
Expand All @@ -271,6 +282,30 @@ def to_s
def to_json(*_args)
raise Errgonomic::SerializeError, 'cannot serialize an unwrapped Result'
end

# @example simple pattern match with variable capture of the value
# result = Errgonomic::Result::Ok.new(1)
# case result
# in Errgonomic::Result::Ok, value
# "Measurement is #{value}"
# in Errgonomic::Result::Err, err
# "Measurement is not available"
# end # => "Measurement is 1"
#
# @example more advanced pattern match against the kind of value
# result = Errgonomic::Result::Err.new(StandardError.new("nope"))
# case result
# in Errgonomic::Result::Ok, value
# "Measurement is #{value}"
# in Errgonomic::Result::Err, String => msg
# "Measurement failed with a message: #{msg}"
# in Errgonomic::Result::Err, Exception => e
# "Measurement produced an exception -- #{e.class}: #{e}"
# end # => "Measurement produced an exception -- StandardError: nope"
def deconstruct
[self, value]
end

end

# The Ok variant.
Expand Down
2 changes: 1 addition & 1 deletion lib/errgonomic/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Errgonomic
VERSION = '0.5.1'
VERSION = '0.6.0'
end
Loading