| 1 |
/** |
| 2 |
* Result types to allow for more type-safe handling of errors. |
| 3 |
* |
| 4 |
* This is basically a partial reimplementation of the Result type in Rust. |
| 5 |
* https://doc.rust-lang.org/std/result/enum.Result.html |
| 6 |
*/ |
| 7 |
|
| 8 |
interface BaseResult<T, E> { |
| 9 |
/** Whether this is Ok */ |
| 10 |
is_ok: boolean; |
| 11 |
|
| 12 |
/** Whether this is Err */ |
| 13 |
is_err: boolean; |
| 14 |
|
| 15 |
/** Calls op if the result is Ok, otherwise returns the Err value of self. */ |
| 16 |
and_then<U>(op: (val: T) => Ok<U>): Result<U, E>; |
| 17 |
and_then<F>(op: (val: T) => Err<F>): Result<T, E | F>; |
| 18 |
and_then<U, F>(op: (val: T) => Result<U, F>): Result<U, E | F>; |
| 19 |
and_then<U, F>(op: (val: T) => Result<U, F>): Result<U, E | F>; |
| 20 |
|
| 21 |
/** Maps by applying a function to a contained Ok value, leaving Err untouched. */ |
| 22 |
map<U>(op: (v: T) => U): Result<U, E>; |
| 23 |
|
| 24 |
/** Maps by applying a function to a contained Err value, leaving Ok untouched. */ |
| 25 |
map_err<F>(op: (v: E) => F): Result<T, F>; |
| 26 |
|
| 27 |
/** Calls op if the result is Err, otherwise returns the Ok value of self. */ |
| 28 |
or_else<F>(op: (v: E) => T): Result<T, F>; |
| 29 |
|
| 30 |
/** Returns the contained Ok value or throw an Error for Err. */ |
| 31 |
unwrap(): T; |
| 32 |
|
| 33 |
/** Returns the contained Err value or throw an Error for Ok. */ |
| 34 |
unwrap_err(): E; |
| 35 |
|
| 36 |
/** Returns the contained Ok value or a provided default. */ |
| 37 |
unwrap_or<U>(d: U): T | U; |
| 38 |
} |
| 39 |
|
| 40 |
/** A successful result. */ |
| 41 |
export class Ok<T> implements BaseResult<T, never> { |
| 42 |
readonly is_ok = true; |
| 43 |
|
| 44 |
readonly is_err = false; |
| 45 |
|
| 46 |
constructor(readonly value: T) {} |
| 47 |
|
| 48 |
and_then<T2>(op: (val: T) => Ok<T2>): Ok<T2>; |
| 49 |
and_then<E2>(op: (val: T) => Err<E2>): Result<T, E2>; |
| 50 |
and_then<T2, E2>(op: (val: T) => Result<T2, E2>): Result<T2, E2>; |
| 51 |
and_then<T2, E2>(op: (val: T) => Result<T2, E2>): Result<T2, E2> { |
| 52 |
return op(this.value); |
| 53 |
} |
| 54 |
|
| 55 |
map<U>(op: (v: T) => U): Ok<U> { |
| 56 |
return new Ok(op(this.value)); |
| 57 |
} |
| 58 |
|
| 59 |
map_err(): this { |
| 60 |
return this; |
| 61 |
} |
| 62 |
|
| 63 |
or_else(): this { |
| 64 |
return this; |
| 65 |
} |
| 66 |
|
| 67 |
unwrap(): T { |
| 68 |
return this.value; |
| 69 |
} |
| 70 |
|
| 71 |
unwrap_err(): never { |
| 72 |
throw new Error("unwrap_err() called on Ok()."); |
| 73 |
} |
| 74 |
|
| 75 |
unwrap_or(): T { |
| 76 |
return this.value; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** An error result. */ |
| 81 |
export class Err<E> implements BaseResult<never, E> { |
| 82 |
readonly is_ok = false; |
| 83 |
|
| 84 |
readonly is_err = true; |
| 85 |
|
| 86 |
constructor(readonly error: E) {} |
| 87 |
|
| 88 |
and_then(): this { |
| 89 |
return this; |
| 90 |
} |
| 91 |
|
| 92 |
map(): this { |
| 93 |
return this; |
| 94 |
} |
| 95 |
|
| 96 |
or_else<F, U>(op: (v: E) => Result<U, F>): Result<U, F> { |
| 97 |
return op(this.error); |
| 98 |
} |
| 99 |
|
| 100 |
map_err<F>(op: (v: E) => F): Result<never, F> { |
| 101 |
return new Err(op(this.error)); |
| 102 |
} |
| 103 |
|
| 104 |
unwrap(): never { |
| 105 |
throw new Error("unwrap() called on error."); |
| 106 |
} |
| 107 |
|
| 108 |
unwrap_err(): E { |
| 109 |
return this.error; |
| 110 |
} |
| 111 |
|
| 112 |
unwrap_or<U>(val: U): U { |
| 113 |
return val; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** Result of an operation that might fail */ |
| 118 |
export type Result<T, E> = Ok<T> | Err<E>; |
| 119 |
|
| 120 |
/** Wrap the value in a successful result. */ |
| 121 |
export function ok<T>(value: T): Ok<T> { |
| 122 |
return new Ok(value); |
| 123 |
} |
| 124 |
|
| 125 |
/** Wrap the value in an error result. */ |
| 126 |
export function err<E>(error: E): Err<E> { |
| 127 |
return new Err(error); |
| 128 |
} |
| 129 |
|
| 130 |
/** Collect an array of results into a single result. */ |
| 131 |
export function collect<T, E>(items: Result<T, E>[]): Result<T[], E> { |
| 132 |
const ok_values: T[] = []; |
| 133 |
for (const r of items) { |
| 134 |
if (r.is_ok) { |
| 135 |
ok_values.push(r.value); |
| 136 |
} else { |
| 137 |
return r; |
| 138 |
} |
| 139 |
} |
| 140 |
return ok(ok_values); |
| 141 |
} |