Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Typed::Result sealed #58

Merged
merged 2 commits into from
Jan 29, 2024
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
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
sorbet-result (1.0.0)
sorbet-runtime (~> 0.5)
zeitwerk (~> 2.6)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -107,7 +106,6 @@ GEM
yard-sorbet (0.8.1)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
zeitwerk (2.6.12)

PLATFORMS
arm64-darwin-22
Expand Down
6 changes: 1 addition & 5 deletions lib/sorbet-result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
# frozen_string_literal: true

require "sorbet-runtime"
require "zeitwerk"
require_relative "typed/result"

# Sorbet-aware namespace to super-charge your projects
module Typed; end

loader = Zeitwerk::Loader.new
loader.push_dir("#{__dir__}/typed", namespace: Typed)
loader.setup
81 changes: 0 additions & 81 deletions lib/typed/failure.rb

This file was deleted.

155 changes: 155 additions & 0 deletions lib/typed/result.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# typed: strict
# frozen_string_literal: true

require_relative "no_error_on_success_error"
require_relative "no_payload_on_failure_error"

module Typed
# A monad representing either a success or a failure. Contains payload and error information as well.
class Result
Expand All @@ -9,6 +12,7 @@ class Result
extend T::Generic

abstract!
sealed!

Payload = type_member(:out)
Error = type_member(:out)
Expand Down Expand Up @@ -55,4 +59,155 @@ def on_error(&block)
def payload_or(value)
end
end

class Success < Result
extend T::Sig
extend T::Generic

Payload = type_member
Error = type_member { {fixed: T.noreturn} }

sig { override.returns(Payload) }
attr_reader :payload

sig do
type_parameters(:T)
.params(payload: T.type_parameter(:T))
.returns(Typed::Success[T.type_parameter(:T)])
end
def self.new(payload)
super(payload)
end

sig { returns(Typed::Success[NilClass]) }
def self.blank
new(nil)
end

sig { params(payload: Payload).void }
def initialize(payload)
@payload = payload
super()
end

sig { override.returns(T::Boolean) }
def success?
true
end

sig { override.returns(T::Boolean) }
def failure?
false
end

sig { override.returns(T.noreturn) }
def error
raise NoErrorOnSuccessError
end

sig do
override
.type_parameters(:U, :T)
.params(block: T.proc.params(arg0: Payload).returns(Result[T.type_parameter(:U), T.type_parameter(:T)]))
.returns(Result[T.type_parameter(:U), T.type_parameter(:T)])
end
def and_then(&block)
block.call(payload)
end

sig do
override
.params(_block: T.proc.params(arg0: Error).void)
.returns(T.self_type)
end
def on_error(&_block)
self
end

sig do
override
.type_parameters(:Fallback)
.params(_value: T.type_parameter(:Fallback))
.returns(T.any(Payload, T.type_parameter(:Fallback)))
end
def payload_or(_value)
payload
end
end

class Failure < Result
extend T::Sig
extend T::Generic

Payload = type_member { {fixed: T.noreturn} }
Error = type_member

sig { override.returns(Error) }
attr_reader :error

sig do
type_parameters(:T)
.params(error: T.type_parameter(:T))
.returns(Typed::Failure[T.type_parameter(:T)])
end
def self.new(error)
super(error)
end

sig { returns(Typed::Failure[NilClass]) }
def self.blank
new(nil)
end

sig { params(error: Error).void }
def initialize(error)
@error = error
super()
end

sig { override.returns(T::Boolean) }
def success?
false
end

sig { override.returns(T::Boolean) }
def failure?
true
end

sig { override.returns(T.noreturn) }
def payload
raise NoPayloadOnFailureError
end

sig do
override
.type_parameters(:U, :T)
.params(_block: T.proc.params(arg0: Payload).returns(Result[T.type_parameter(:U), T.type_parameter(:T)]))
.returns(Result[T.type_parameter(:U), Error])
end
def and_then(&_block)
self
end

sig do
override
.params(block: T.proc.params(arg0: Error).void)
.returns(T.self_type)
end
def on_error(&block)
block.call(error)
self
end

sig do
override
.type_parameters(:Fallback)
.params(value: T.type_parameter(:Fallback))
.returns(T.any(Payload, T.type_parameter(:Fallback)))
end
def payload_or(value)
value
end
end
end
80 changes: 0 additions & 80 deletions lib/typed/success.rb

This file was deleted.

1 change: 0 additions & 1 deletion sorbet-result.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_runtime_dependency "sorbet-runtime", "~> 0.5"
spec.add_runtime_dependency "zeitwerk", "~> 2.6"
end
Loading
Loading