perlica
Version:
<h1 align="center">Perlica</h1>
332 lines (331 loc) • 13.6 kB
TypeScript
/**
* |----------------------|------------------|
* | Rust methods | Perlica |
* |----------------------|------------------|
* | and | and |
* | and_then | andThen |
* | as_deref | |
* | as_deref_mut | |
* | as_mut | |
* | as_ref | |
* | cloned | |
* | copied | |
* | err | err |
* | expect | expect |
* | expect_err | expectErr |
* | flatten | flatten |
* | inspect | inspect |
* | inspect_err | inspectErr |
* | into_err | |
* | into_ok | |
* | is_err | isErr |
* | is_err_and | isErrAnd |
* | is_ok | isOk |
* | is_ok_and | isOkAnd |
* | iter | |
* | iter_mut | |
* | map | map |
* | map_err | mapErr |
* | map_or | mapOr |
* | map_or_else | mapOrElse |
* | ok | ok |
* | or | or |
* | or_else | orElse |
* | transpose | transpose |
* | unwrap | unwrap |
* | unwrap_err | unwrapErr |
* | unwrap_err_unchecked | |
* | unwrap_or | unwrapOr |
* | unwrap_or_default | |
* | unwrap_or_else | unwrapOrElse |
* | unwrap_unchecked | |
* |----------------------|------------------|
*/
import { OnceIterator } from "./Iterator";
import { type Option } from "./Option";
export type OkType<R> = R extends Result<infer T, any> ? T : any;
export type ErrType<R> = R extends Result<any, infer E> ? E : any;
export interface ResultTrait<T, E> {
/**
* Returns `res` if `this` is `Ok`, otherwise returns `Err`.
*
* # Examples
* ```ts
* import { err, ok } from "perlica/Result";
*
* const a = ok(4);
* const b = err("new error");
* expect(a.and(b)).toEqual(err("new error"));
*
* const a = err("old error");
* const b = ok(4);
* expect(a.and(b)).toEqual(err("old error"));
*
* const a = err("old error");
* const b = err("new error");
* expect(a.and(b)).toEqual(err("old error"));
*
* const a = ok(4);
* const b = ok(12);
* expect(a.and(b)).toEqual(ok(12));
* ```
* See also the **[Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and)**
*/
and<U, T, E>(this: Result<T, E>, res: Result<U, E>): Result<U, E>;
/**
* Returns `f` call if `this` is `Ok`, otherwise returns `Err`.
*
* # Examples
* ```ts
* import { err, ok } from "perlica/Result";
*
* const string_to_number = (num: string): Result<number, Error> => {
* const v = Number(num);
* return isNaN(v) ? err(Error("Not a Number")) : ok(v);
* };
*
* expect(ok("4").andThen(string_to_number)).toEqual(ok(4));
* expect(ok("hello").andThen(string_to_number)).toEqual(err(Error("Not a Number")));
* expect(err(Error("old error")).andThen(string_to_number)).toEqual(err(Error("old error")));
* ```
* See also the **[Result.and_then](https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then)**
*/
andThen<U, T, E>(this: Result<T, E>, f: (v: T) => Result<U, E>): Result<U, E>;
err<T, E>(this: Result<T, E>): Option<E>;
/**
* Returns the contained value `T` if `this` is `Ok`, otherwise throw exception `msg`.
*
* ```ts
* import { err, ok } from "perlica/Result";
*
* const a = ok(4);
* const b = err("error");
*
* expect(a.expect("my error message")).toEqual(4);
* expect(() => b.expect("my error message")).toThrowError("my error message");
* ```
* See also the **[Result.expect](https://doc.rust-lang.org/std/result/enum.Result.html#method.expect)**
*/
expect<T, E>(this: Result<T, E>, msg: string): T;
/**
* Returns the contained value `E` if `this` is `Err`, otherwise throw exception `msg`.
*
* ```ts
* import { err, ok } from "perlica/Result";
*
* const a = ok(4);
* const b = err("error");
*
* expect(() => a.expectErr("my error message")).toThrowError("my error message");
* expect(b.expectErr("my error message")).toEqual("error");
* ```
* See also the **[Result.expect_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err)**
*/
expectErr<T, E>(this: Result<T, E>, msg: string): E;
flatten<T, E>(this: Result<Result<T, E>, E>): Result<T, E>;
inspect: <T, E>(this: Result<T, E>, f: (v: T) => void) => Result<T, E>;
inspectErr<T, E>(this: Result<T, E>, f: (v: E) => void): Result<T, E>;
isErr<T, E>(this: Result<T, E>): boolean;
isErrAnd<T, E>(this: Result<T, E>, f: (v: E) => boolean): boolean;
isOk<T, E>(this: Result<T, E>): boolean;
isOkAnd<T, E>(this: Result<T, E>, f: (v: T) => boolean): boolean;
map<U, T, E>(this: Result<T, E>, f: (v: T) => U): Result<U, E>;
mapErr<U, T, E>(this: Result<T, E>, f: (v: E) => U): Result<T, U>;
mapOr<U, T, E>(this: Result<T, E>, def: U, f: (v: T) => U): U;
mapOrElse<U, T, E>(this: Result<T, E>, def: (v: E) => U, f: (v: T) => U): U;
ok<T, E>(this: Result<T, E>): Option<T>;
or<U, T, E>(this: Result<T, E>, f: (v: E) => U): Result<T, U>;
orElse<U, T, E>(this: Result<T, E>, f: (v: E) => Result<T, U>): Result<T, U>;
transpose<T, E>(this: Result<Option<T>, E>): Option<Result<T, E>>;
/**
* Returns the contained value `Ok`, otherwise throw `Error` exception.
*
* ```ts
* import { err, ok } from "perlica/Result";
*
* expect(ok(4).unwrap()).toEqual(4);
* expect(() => err("error").unwrap()).toThrowError(`called \`Result.unwrap()\` on an \`Err\` value: error`);
* ```
* See also the **[Result.unwrap](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap)**
*/
unwrap<T, E>(this: Result<T, E>): T;
/**
* Returns the contained value `Err`, otherwise throw `Error` exception.
*
* ```ts
* import { err, ok } from "perlica/Result";
*
* expect(() => ok(4).unwrapErr()).toThrowError(`called \`Result.unwrapErr()\` on an \`Ok\` value: 4`);
* expect(err("error").unwrapErr()).toEqual("error");
* ```
* See also the **[Result.unwrap_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_err)**
*/
unwrapErr<T, E>(this: Result<T, E>): E;
/**
* Returns the contained value `Ok`, otherwise returns `def`.
*
* ```ts
* import { err, ok } from "perlica/Result";
*
* expect(ok(4).unwrapOr(10)).toEqual(4);
* expect(err("error").unwrapOr(10)).toEqual(10);
* ```
* See also the **[Result.unwrap_or](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or)**
*/
unwrapOr<T, E>(this: Result<T, E>, def: T): T;
/**
* Returns the contained value `Ok`, otherwise `def` call.
*
* ```ts
* import { err, ok } from "perlica/Result";
*
* const len = (s: string) => s.length;
*
* expect(ok(4).unwrapOrElse(len)).toEqual(4);
* expect(err("error").unwrapOrElse(len)).toEqual(5);
* ```
* See also the **[Result.unwrap_or_else](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else)**
*/
unwrapOrElse<T, E>(this: Result<T, E>, def: (v: E) => T): T;
[Symbol.iterator](this: Result<T, E>): OnceIterator<Result<T, E>, T>;
}
/**
* Returns `that` if `self` is `Ok`, otherwise returns `self`.
*
* ```ts
* import * as R from "perlica/Result";
*
* const a = R.ok(4);
* const b = R.err("new error");
* expect(R.and(a, b)).toEqual(R.err("new error"));
*
* const a = R.err("old error");
* const b = R.ok(4);
* expect(R.and(a, b)).toEqual(R.err("old error"));
*
* const a = R.err("old error");
* const b = R.err("new error");
* expect(R.and(a, b)).toEqual(R.err("old error"));
*
* const a = R.ok(4);
* const b = R.ok(12);
* expect(R.and(a, b)).toEqual(R.ok(12));
* ```
* See also the **[Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and)**
*/
export declare const and: <U, T, E>(self: Result<T, E>, that: Result<U, E>) => Result<U, E>;
/**
* Returns `f` call if `self` is `Ok`, otherwise returns `self`.
*
* ```ts
* import * as R from "perlica/Result";
*
* const string_to_number = (num: string): R.Result<number, Error> => {
* const v = Number(num);
* return isNaN(v) ? R.err(Error("Not a Number")) : R.ok(v);
* };
*
* expect(R.andThen(R.ok("4"), string_to_number)).toEqual(R.ok(4));
* expect(R.andThen(R.ok("hello"), string_to_number)).toEqual(R.err(Error("Not a Number")));
* expect(R.andThen(R.err(Error("old error")), string_to_number)).toEqual(R.err(Error("old error")));
* ```
* See also the **[Result.and_then](https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then)**
*/
export declare const andThen: <U, T, E>(self: Result<T, E>, f: (v: T) => Result<U, E>) => Result<U, E>;
/**
* Returns the contained value `T` if `self` is `Ok`, otherwise throw exception `msg`.
*
* ```ts
* import * as R from "perlica/Result";
*
* expect(R.expect(R.ok(4), "my error message")).toEqual(4);
* expect(() => R.expect(R.err("error"), "my error message")).toThrowError("my error message");
* ```
* See also the **[Result.expect](https://doc.rust-lang.org/std/result/enum.Result.html#method.expect)**
*/
export declare const expect: <T, E>(self: Result<T, E>, msg: string) => T;
/**
* Returns the contained value `E` if `self` is `Err`, otherwise throw exception `msg`.
*
* ```ts
* import * as R from "perlica/Result";
*
* expect(() => R.expectErr(R.ok(4), "my error message")).toThrowError("my error message");
* expect(R.expectErr(R.err("error"), "my error message")).toEqual("error");
* ```
* See also the **[Result.expect_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err)**
*/
export declare const expectErr: <T, E>(self: Result<T, E>, msg: string) => E;
export declare const isOk: <T, E>(r: Result<T, E>) => r is Ok<T>;
export declare const isErr: <T, E>(r: Result<T, E>) => r is Err<E>;
/**
* Returns the contained value `Ok`, otherwise throw `Error` exception.
*
* ```ts
* import * as R from "perlica/Result";
*
* expect(R.unwrap(R.ok(4))).toEqual(4);
* expect(() => R.unwrap(R.err("error"))).toThrowError(`called \`Result.unwrap()\` on an \`Err\` value: error`);
* ```
* See also the **[Result.unwrap](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap)**
*/
export declare const unwrap: <T, E>(self: Result<T, E>) => T;
/**
* Returns the contained value `Err`, otherwise throw `Error` exception.
*
* ```ts
* import * as R from "perlica/Result";
*
* expect(() => R.unwrapErr(R.ok(4))).toThrowError(`called \`Result.unwrapErr()\` on an \`Ok\` value: 4`);
* expect(R.unwrapErr(R.err("error"))).toEqual("error");
* ```
* See also the **[Result.unwrap_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_err)**
*/
export declare const unwrapErr: <T, E>(self: Result<T, E>) => E;
/**
* Returns the contained value `Ok`, otherwise returns `def`.
*
* ```ts
* import * as R from "perlica/Result";
*
* expect(R.unwrapOr(R.ok(4), 10)).toEqual(4);
* expect(R.unwrapOr(R.err("error"), 10)).toEqual(10);
* ```
* See also the **[Result.unwrap_or](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or)**
*/
export declare const unwrapOr: <T, E>(self: Result<T, E>, def: T) => T;
/**
* Returns the contained value `Ok`, otherwise `def` call.
*
* ```ts
* import * as R from "perlica/Result";
*
* const len = (s: string) => s.length;
*
* expect(R.unwrapOrElse(R.ok(4), len)).toEqual(4);
* expect(R.unwrapOrElse(R.err("error"), len)).toEqual(5);
* ```
* See also the **[Result.unwrap_or_else](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else)**
*/
export declare const unwrapOrElse: <T, E>(self: Result<T, E>, def: (v: E) => T) => T;
export declare const fromNullable: <T, E>(v: T, f: () => E) => Result<T, E>;
export declare const fromOption: <T, E>(self: Option<T>, f: () => E) => Result<T, E>;
export declare const tryCatch: <T, E>(f: () => T) => Result<T, E>;
export declare const tryPromise: <T, E>(v: Promise<T>) => Promise<Result<T, E>>;
export declare const bind: Bind;
export declare const ok: <T, E = never>(v: T) => Result<T, E>;
export declare const err: <E, T = never>(v: E) => Result<T, E>;
export interface Ok<T> extends ResultTrait<T, never> {
tag: "ok";
value: T;
}
export interface Err<E> extends ResultTrait<never, E> {
tag: "err";
value: E;
}
export type Result<T, E> = Ok<T> | Err<E>;
export declare const ResultProto: <T, E>() => ResultTrait<T, E>;
interface Bind {
<T extends Result<any, any>, R>(fn: () => Generator<T, R>): Result<R, T extends Err<infer E> ? E : never>;
}
export {};