class Ok(t.Generic[KT, KE], Result[KT, KE]):
__value: KT
def __init__(self, value: KT):
self.__value = value
def __repr__(self):
return f"Result::Ok({self.__value})"
def __str__(self):
return str(self.__value)
def __eq__(self, other):
return isinstance(other, Ok) and self.__value == other.__value
def __instancecheck__(self, instance):
return isinstance(instance, Ok)
def is_ok(self) -> bool:
return True
def is_ok_and(self, func: t.Callable[[KT], bool]) -> bool:
return func(self.__value)
def is_err(self) -> bool:
return False
def is_err_and(self, func: t.Callable[[KE], bool]) -> bool:
return False
def ok(self) -> "Option[KT]":
return Option.some(self.__value)
def err(self) -> "Option[KE]":
return Option.none()
def map(self, func: t.Callable[[KT], U]) -> Result[U, KE]:
return Result.of_ok(func(self.__value))
def map_mut(self, func: t.Callable[[KT], None]) -> Result[KT, KE]:
func(self.__value)
return self
def map_err_mut(self, func: t.Callable[[KE], None]) -> Result[KT, KE]:
return self
def map_or(self, default: U, func: t.Callable[[KT], U]) -> U:
return func(self.__value)
def map_or_else(self, default: t.Callable[[KE], U], func: t.Callable[[KT], U]) -> U:
return func(self.__value)
def map_err(self, func: t.Callable[[KE], F]) -> Result[KT, F]:
return Result.of_ok(self.__value)
def inspect(self, func: t.Callable[[KT], None]) -> Result[KT, KE]:
func(self.__value)
return self
def inspect_err(self, func: t.Callable[[KE], None]) -> Result[KT, KE]:
return self
def to_array(self) -> t.List[KT]:
return [self.__value]
def expect(self, msg: str) -> KT:
return self.__value
def expect_err(self, msg: str) -> KE:
raise UnwrapException("Result", msg + f': {repr(self.__value)}')
def to_pattern(self) -> t.Tuple[bool, t.Union[KT, KE]]:
return True, self.__value
def to_either(self) -> "Left[KT, KE]":
return Left(self.__value)
def unwrap_unchecked(self) -> t.Union[KT, KE]:
return self.__value
def unwrap(self) -> KT:
return self.__value
def unwrap_or(self, default: KT) -> KT:
return self.__value
def unwrap_or_else(self, op: t.Callable[[KE], KT]) -> KT:
return self.__value
def unwrap_err(self) -> KE:
raise UnwrapException("Result", f"call `Result.unwrap_err` on an `Ok` value: {self.__value}")
def bool_and(self, res: Result[U, KE]) -> Result[U, KE]:
if res.is_ok():
return Result.of_ok(res.unwrap())
else:
return Result.of_err(res.unwrap_err())
def and_then(self, op: t.Callable[[KT], Result[U, KE]]) -> Result[U, KE]:
return op(self.__value)
def bool_or(self, res: Result[KT, F]) -> Result[KT, F]:
return Result.of_ok(self.__value)
def or_else(self, op: t.Callable[[KE], Result[KT, F]]) -> Result[KT, F]:
return Result.of_ok(self.__value)