-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.dart
50 lines (39 loc) · 1.39 KB
/
result.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
sealed class Result<T, E> {}
final class Ok<T, E> implements Result<T, E> {
const Ok(this.value);
final T value;
}
final class Err<T, E> implements Result<T, E> {
const Err(this.error);
final E error;
}
extension ResultExtensions<T, E> on Result<T, E> {
Result<NewT, E> mapOk<NewT>(NewT Function(T) mapping) => switch (this) {
Ok(:final value) => Ok(mapping(value)),
Err(:final error) => Err(error),
};
Result<NewT, E> flatMapOk<NewT>(Result<NewT, E> Function(T) mapping) => switch (this) {
Ok(:final value) => mapping(value),
Err(:final error) => Err(error),
};
Result<T, NewE> mapErr<NewE>(NewE Function(E) mapping) => switch (this) {
Ok(:final value) => Ok(value),
Err(:final error) => Err(mapping(error)),
};
Result<T, NewE> flatMapErr<NewE>(Result<T, NewE> Function(E) mapping) => switch (this) {
Ok(:final value) => Ok(value),
Err(:final error) => mapping(error),
};
T orElse(T Function(E) mapping) => switch (this) {
Ok(:final value) => value,
Err(:final error) => mapping(error),
};
bool isOkWith(T expectedValue) => switch (this) {
Ok(:final value) => value == expectedValue,
Err(error: _) => false,
};
bool isErrWith(E expectedError) => switch (this) {
Ok(value: _) => false,
Err(:final error) => error == expectedError,
};
}