Source code for stdlibx.result.methods.option
1from __future__ import annotations
2
3from typing import TYPE_CHECKING, TypeVar
4
5from stdlibx import option
6from stdlibx.result._result import error, is_err, is_ok, ok
7
8if TYPE_CHECKING:
9 from stdlibx.option.types import Option
10 from stdlibx.result.types import Result
11
12T = TypeVar("T")
13E = TypeVar("E")
14U = TypeVar("U")
15
16
[docs]
17def get_ok(result: Result[T, E]) -> Option[T]:
18 if is_ok(result):
19 return option.some(result.value)
20 return option.nothing()
21
22
[docs]
23def get_error(result: Result[T, E]) -> Option[E]:
24 if is_err(result):
25 return option.some(result.error)
26 return option.nothing()
27
28
[docs]
29def transpose(result: Result[Option[U], E]) -> Option[Result[U, E]]:
30 if is_ok(result) and option.is_some(result.value):
31 return option.some(ok(result.value.value))
32 elif is_err(result):
33 return option.some(error(result.error))
34 else:
35 return option.nothing()