1from __future__ import annotations
2
3from functools import partial
4from typing import TYPE_CHECKING, Callable, TypeVar, Union
5
6from stdlibx.result import Result, methods
7from typing_extensions import TypeVarTuple, Unpack
8
9if TYPE_CHECKING:
10 from stdlibx.result._types import Operation
11
12T = TypeVar("T")
13E = TypeVar("E")
14U = TypeVar("U")
15F = TypeVar("F")
16Ts = TypeVarTuple("Ts")
17_AnyException = TypeVar("_AnyException", bound=Exception)
18
19
[docs]
20def is_ok_and(func: Callable[[T], bool]) -> Operation[Result[T, E], bool]:
21 return partial(methods.is_ok_and, func=func)
22
23
[docs]
24def is_err_and(func: Callable[[E], bool]) -> Operation[Result[T, E], bool]:
25 return partial(methods.is_err_and, func=func)
26
27
[docs]
28def map_(func: Callable[[T], U]) -> Operation[Result[T, E], Result[U, E]]:
29 return partial(methods.map_, func=func)
30
31
[docs]
32def map_or(default: U, func: Callable[[T], U]) -> Operation[Result[T, E], U]:
33 return partial(methods.map_or, default=default, func=func)
34
35
[docs]
36def map_or_else(
37 default: Callable[[E], U], func: Callable[[T], U]
38) -> Operation[Result[T, E], U]:
39 return partial(methods.map_or_else, default=default, func=func)
40
41
[docs]
42def map_err(func: Callable[[E], F]) -> Operation[Result[T, E], Result[T, F]]:
43 return partial(methods.map_err, func=func)
44
45
[docs]
46def inspect(func: Callable[[T], None]) -> Operation[Result[T, E], Result[T, E]]:
47 return partial(methods.inspect, func=func)
48
49
[docs]
50def inspect_err(func: Callable[[E], None]) -> Operation[Result[T, E], Result[T, E]]:
51 return partial(methods.inspect_err, func=func)
52
53
[docs]
54def expect(msg: str) -> Operation[Result[T, E], T]:
55 return partial(methods.expect, msg=msg)
56
57
[docs]
58def unwrap() -> Operation[Result[T, E], T]:
59 return methods.unwrap
60
61
[docs]
62def expect_err(msg: str) -> Operation[Result[T, E], E]:
63 return partial(methods.expect_err, msg=msg)
64
65
[docs]
66def unwrap_err() -> Operation[Result[T, E], E]:
67 return methods.unwrap_err
68
69
[docs]
70def and_(other: Result[U, F]) -> Operation[Result[T, E], Result[U, Union[E, F]]]:
71 return partial(methods.and_, other=other)
72
73
[docs]
74def and_then(
75 func: Callable[[T], Result[U, F]],
76) -> Operation[Result[T, E], Result[U, Union[E, F]]]:
77 return partial(methods.and_then, func=func)
78
79
[docs]
80def or_(default: Result[T, F]) -> Operation[Result[T, E], Result[T, F]]:
81 return partial(methods.or_, default=default)
82
83
[docs]
84def or_else(
85 default: Callable[[E], Result[T, F]],
86) -> Operation[Result[T, E], Result[T, F]]:
87 return partial(methods.or_else, default=default)
88
89
[docs]
90def unwrap_or(default: T) -> Operation[Result[T, E], T]:
91 return partial(methods.unwrap_or, default=default)
92
93
[docs]
94def unwrap_or_else(default: Callable[[E], T]) -> Operation[Result[T, E], T]:
95 return partial(methods.unwrap_or_else, default=default)
96
97
[docs]
98def unwrap_or_raise() -> Operation[Result[T, _AnyException], T]:
99 return methods.unwrap_or_raise
100
101
[docs]
102def flatten() -> Operation[Result[Result[T, E], F], Result[T, Union[E, F]]]:
103 return methods.flatten
104
105
[docs]
106def zipped(
107 func: Callable[[Unpack[Ts]], Result[U, E]],
108) -> Operation[Result[tuple[Unpack[Ts]], E], Result[tuple[Unpack[Ts], U], E]]:
109 return partial(methods.zipped, func=func)