Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nz committed Mar 4, 2025
1 parent 36d5f0f commit 53555cf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ task default: %i[spec standard]

YARD::Doctest::RakeTask.new do |task|
task.doctest_opts = %w[-v]
task.pattern = 'lib/**/*.rb'
task.pattern = "lib/**/*.rb"
end
56 changes: 39 additions & 17 deletions lib/errgonomic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ class ResultRequiredError < Error; end

module Result
class Any

# Equality comparison for Result objects is based on value not reference.
#
# @example
# Ok("foo") == Ok("foo") # => true
# Ok("foo") == Err("foo") # => false
# Ok("foo").object_id != Ok("foo").object_id # => true
def ==(other)
self.class == other.class && self.value == other.value
self.class == other.class && value == other.value
end

# Indicate that this is some kind of result object. Contrast to
Expand Down Expand Up @@ -342,10 +341,9 @@ def match

module Option
class Any

def ==(other)
return true if self.none? && other.none?
return true if self.some? && other.some? && self.value == other.value
return true if none? && other.none?
return true if some? && other.some? && value == other.value
false
end

Expand All @@ -357,7 +355,8 @@ def ==(other)
# None().some_and { |x| x > 0 } # => false
def some_and(&block)
return false if none?
!! block.call(self.value)

!!block.call(value)
end

# return true if the contained value is None or the block returns truthy
Expand All @@ -368,7 +367,8 @@ def some_and(&block)
# Some(1).none_or { |x| x < 0 } # => false
def none_or(&block)
return true if none?
!! block.call(self.value)

!!block.call(value)
end

# return an Array with the contained value, if any
Expand All @@ -385,7 +385,8 @@ def to_a
# Some(1).unwrap! # => 1
# None().unwrap! # => raise Errgonomic::UnwrapError, "cannot unwrap None"
def unwrap!
raise Errgonomic::UnwrapError, "cannot unwrap None" if none?
raise Errgonomic::UnwrapError, 'cannot unwrap None' if none?

value
end

Expand All @@ -396,6 +397,7 @@ def unwrap!
# None().expect!("msg") # => raise Errgonomic::ExpectError, "msg"
def expect!(msg)
raise Errgonomic::ExpectError, msg if none?

value
end

Expand All @@ -405,6 +407,7 @@ def expect!(msg)
# None().unwrap_or(2) # => 2
def unwrap_or(default)
return default if none?

value
end

Expand All @@ -415,6 +418,7 @@ def unwrap_or(default)
# None().unwrap_or_else { 2 } # => 2
def unwrap_or_else(&block)
return block.call if none?

value
end

Expand Down Expand Up @@ -445,10 +449,12 @@ def tap_some(&block)
# Some(1).map { :foo } # => raise Errgonomic::ArgumentError, "block must return an Option"
def map(&block)
return self if none?

res = block.call(value)
unless res.is_a?(Errgonomic::Option::Any) || Errgonomic.give_me_ambiguous_downstream_errors?
raise ArgumentError, "block must return an Option"
raise ArgumentError, 'block must return an Option'
end

res
end

Expand All @@ -462,6 +468,7 @@ def map(&block)
# Some("foo").map_or(0) { |str| str.length } # => 3
def map_or(default, &block)
return default if none?

block.call(value)
end

Expand All @@ -473,6 +480,7 @@ def map_or(default, &block)
# Some("str").map_or_else(-> { 100 }) { |str| str.length } # => 3
def map_or_else(proc, &block)
return proc.call if none?

block.call(value)
end

Expand All @@ -482,6 +490,7 @@ def map_or_else(proc, &block)
# Some(1).ok # => Ok(1)
def ok
return Errgonomic::Result::Ok.new(value) if some?

Errgonomic::Result::Err.new
end

Expand All @@ -492,6 +501,7 @@ def ok
# Some(1).ok_or("such err") # => Ok(1)
def ok_or(err)
return Errgonomic::Result::Ok.new(value) if some?

Errgonomic::Result::Err.new(err)
end

Expand All @@ -500,9 +510,10 @@ def ok_or(err)
#
# @example
# None().ok_or_else { "wow" } # => Err("wow")
# Some(1).ok_or_else { "such err" } # => Ok(1)
# Some("foo").ok_or_else { "such err" } # => Ok("foo")
def ok_or_else(&block)
return Errgonomic::Result::Ok.new(value) if some?

Errgonomic::Result::Err.new(block.call)
end

Expand All @@ -521,20 +532,31 @@ def ok_or_else(&block)
# replace
# zip
# zip_with

end

class Some < Any
attr_accessor :value
def initialize(value)
@value = value
end
def some?; true; end
def none?; false; end

def some?
true
end

def none?
false
end
end

class None < Any
def some?; false; end
def none?; true; end
def some?
false
end

def none?
true
end
end
end

Expand Down Expand Up @@ -565,8 +587,8 @@ def Some(value)
Errgonomic::Option::Some.new(value)
end

def None()
Errgonomic::Option::None.new()
def None
Errgonomic::Option::None.new
end

class Object
Expand Down
5 changes: 0 additions & 5 deletions spec/errgonomic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,5 @@

describe "zip"
describe "zip_with"





end
end

0 comments on commit 53555cf

Please sign in to comment.