blob: 9a5bed6eb369aa590f473b513dc54b2bce523ffa (
plain)
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
|
import { FFIError, ICU4XError } from "icu4x";
export type Result<T> =
| { ok: true; value: T }
| { ok: false; error: FFIError<ICU4XError> };
export function Ok<T>(value: T): Result<T> {
return { ok: true, value };
}
export function Err<T>(error: FFIError<ICU4XError>): Result<T> {
return { ok: false, error };
}
// Convert exceptions into a `Result`.
export function result<T>(fn: () => T): Result<T> {
try {
return Ok(fn());
} catch (e) {
return Err(e);
}
}
// Convert a `Result` into an exception.
export function unwrap<T>(result: Result<T>): T {
switch (result.ok) {
case true: return result.value;
case false: throw result.error;
}
}
|