forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResult.ts
236 lines (199 loc) · 6.13 KB
/
Result.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// Direct translation of Rust's Result type, although some ownership related methods are omitted.
export interface Result<T, E> {
/*
* Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value,
* leaving an `Err` value untouched.
*
* This function can be used to compose the results of two functions.
*/
map<U>(fn: (val: T) => U): Result<U, E>;
/*
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value,
* leaving an `Ok` value untouched.
*
* This function can be used to pass through a successful result while handling an error.
*/
mapErr<F>(fn: (val: E) => F): Result<T, F>;
/*
* Returns the provided default (if `Err`), or applies a function to the contained value
* (if `Ok`).
*
* Arguments passed to {@link mapOr} are eagerly evaluated; if you are passing the result of a
* function call, it is recommended to use {@link mapOrElse}, which is lazily evaluated.
*/
mapOr<U>(fallback: U, fn: (val: T) => U): U;
/*
* Maps a `Result<T, E>` to `U` by applying fallback function default to a contained `Err` value,
* or function `fn` to a contained `Ok` value.
*
* This function can be used to unpack a successful result while handling an error.
*/
mapOrElse<U>(fallback: () => U, fn: (val: T) => U): U;
/*
* Calls `fn` if the result is `Ok`, otherwise returns the `Err` value of self.
*
* This function can be used for control flow based on Result values.
*/
andThen<U>(fn: (val: T) => Result<U, E>): Result<U, E>;
/*
* Returns res if the result is `Ok`, otherwise returns the `Err` value of self.
*
* Arguments passed to {@link and} are eagerly evaluated; if you are passing the result of a
* function call, it is recommended to use {@link andThen}, which is lazily evaluated.
*/
and<U>(res: Result<U, E>): Result<U, E>;
/*
* Returns `res` if the result is `Err`, otherwise returns the `Ok` value of self.
*
* Arguments passed to {@link or} are eagerly evaluated; if you are passing the result of a
* function call, it is recommended to use {@link orElse}, which is lazily evaluated.
*/
or(res: Result<T, E>): Result<T, E>;
/*
* Calls `fn` if the result is `Err`, otherwise returns the `Ok` value of self.
*
* This function can be used for control flow based on result values.
*/
orElse<F>(fn: (val: E) => Result<T, F>): Result<T, F>;
// Returns `true` if the result is `Ok`.
isOk(): this is OkImpl<T>;
// Returns `true` if the result is `Err`.
isErr(): this is ErrImpl<E>;
// Returns the contained `Ok` value or throws.
expect(msg: string): T;
// Returns the contained `Err` value or throws.
expectErr(msg: string): E;
// Returns the contained `Ok` value.
unwrap(): T;
/*
* Returns the contained `Ok` value or a provided default.
*
* Arguments passed to {@link unwrapOr} are eagerly evaluated; if you are passing the result of a
* function call, it is recommended to use {@link unwrapOrElse}, which is lazily evaluated.
*/
unwrapOr(fallback: T): T;
// Returns the contained `Ok` value or computes it from a closure.
unwrapOrElse(fallback: (val: E) => T): T;
// Returns the contained `Err` value or throws.
unwrapErr(): E;
}
export function Ok<T>(val: T): OkImpl<T> {
return new OkImpl(val);
}
class OkImpl<T> implements Result<T, never> {
constructor(private val: T) {}
map<U>(fn: (val: T) => U): Result<U, never> {
return new OkImpl(fn(this.val));
}
mapErr<F>(_fn: (val: never) => F): Result<T, F> {
return this;
}
mapOr<U>(_fallback: U, fn: (val: T) => U): U {
return fn(this.val);
}
mapOrElse<U>(_fallback: () => U, fn: (val: T) => U): U {
return fn(this.val);
}
andThen<U>(fn: (val: T) => Result<U, never>): Result<U, never> {
return fn(this.val);
}
and<U>(res: Result<U, never>): Result<U, never> {
return res;
}
or(_res: Result<T, never>): Result<T, never> {
return this;
}
orElse<F>(_fn: (val: never) => Result<T, F>): Result<T, F> {
return this;
}
isOk(): this is OkImpl<T> {
return true;
}
isErr(): this is ErrImpl<never> {
return false;
}
expect(_msg: string): T {
return this.val;
}
expectErr(msg: string): never {
throw new Error(`${msg}: ${this.val}`);
}
unwrap(): T {
return this.val;
}
unwrapOr(_fallback: T): T {
return this.val;
}
unwrapOrElse(_fallback: (val: never) => T): T {
return this.val;
}
unwrapErr(): never {
if (this.val instanceof Error) {
throw this.val;
}
throw new Error(`Can't unwrap \`Ok\` to \`Err\`: ${this.val}`);
}
}
export function Err<E>(val: E): ErrImpl<E> {
return new ErrImpl(val);
}
class ErrImpl<E> implements Result<never, E> {
constructor(private val: E) {}
map<U>(_fn: (val: never) => U): Result<U, E> {
return this;
}
mapErr<F>(fn: (val: E) => F): Result<never, F> {
return new ErrImpl(fn(this.val));
}
mapOr<U>(fallback: U, _fn: (val: never) => U): U {
return fallback;
}
mapOrElse<U>(fallback: () => U, _fn: (val: never) => U): U {
return fallback();
}
andThen<U>(_fn: (val: never) => Result<U, E>): Result<U, E> {
return this;
}
and<U>(_res: Result<U, E>): Result<U, E> {
return this;
}
or(res: Result<never, E>): Result<never, E> {
return res;
}
orElse<F>(fn: (val: E) => ErrImpl<F>): Result<never, F> {
return fn(this.val);
}
isOk(): this is OkImpl<never> {
return false;
}
isErr(): this is ErrImpl<E> {
return true;
}
expect(msg: string): never {
throw new Error(`${msg}: ${this.val}`);
}
expectErr(_msg: string): E {
return this.val;
}
unwrap(): never {
if (this.val instanceof Error) {
throw this.val;
}
throw new Error(`Can't unwrap \`Err\` to \`Ok\`: ${this.val}`);
}
unwrapOr<T>(fallback: T): T {
return fallback;
}
unwrapOrElse<T>(fallback: (val: E) => T): T {
return fallback(this.val);
}
unwrapErr(): E {
return this.val;
}
}