Source code for stdlibx.result.methods.option

 1from __future__ import annotations
 2
 3from typing import TypeVar
 4
 5from stdlibx.option import Nothing, Option, Some, is_some
 6from stdlibx.result._result import Error, Ok, Result, is_err, is_ok
 7
 8T = TypeVar("T")
 9E = TypeVar("E")
10U = TypeVar("U")
11
12
[docs] 13def ok(result: Result[T, E]) -> Option[T]: 14 if is_ok(result): 15 return Some(result.value) 16 return Nothing()
17 18
[docs] 19def err(result: Result[T, E]) -> Option[E]: 20 if is_err(result): 21 return Some(result.error) 22 return Nothing()
23 24
[docs] 25def transpose(result: Result[Option[U], E]) -> Option[Result[U, E]]: 26 if is_ok(result) and is_some(result.value): 27 return Some(Ok(result.value.value)) 28 elif is_err(result): 29 return Some(Error(result.error)) 30 else: 31 return Nothing()