-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
258 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Generic, Literal, TypeAlias, TypeGuard, TypeVar | ||
|
||
E = TypeVar("E") | ||
T = TypeVar("T") | ||
|
||
|
||
class Ok(Generic[T]): | ||
def __init__(self, value: T): | ||
self._value = value | ||
|
||
@property | ||
def value(self) -> T: | ||
return self._value | ||
|
||
def is_ok(self) -> Literal[True]: | ||
return True | ||
|
||
def is_err(self) -> Literal[False]: | ||
return False | ||
|
||
|
||
class Err(Generic[E]): | ||
def __init__(self, value: E): | ||
self._value = value | ||
|
||
@property | ||
def value(self) -> E: | ||
return self._value | ||
|
||
def is_err(self) -> bool: | ||
return True | ||
|
||
def is_ok(self) -> bool: | ||
return False | ||
|
||
|
||
Result: TypeAlias = Ok[T] | Err[E] | ||
|
||
|
||
def is_err(result: Result[T, E]) -> TypeGuard[Err[E]]: | ||
return result.is_err() | ||
|
||
|
||
def is_ok(result: Result[T, E]) -> TypeGuard[Ok[T]]: | ||
return result.is_ok() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.