happy-rusty • Docs
happy-rusty / Result
The Result
type is used for returning and propagating errors.
It is an enum with the variants, Ok(T)
, representing success and containing a value, and Err(E)
, representing error and containing an error value.
This interface includes methods that act on the Result
type, similar to Rust's Result
enum.
As Rust Code:
pub enum Result<T, E> {
Ok(T),
Err(E),
}
Type Parameter | Description |
---|---|
T |
The type of the value contained in a successful Result . |
E |
The type of the error contained in an unsuccessful Result . |
and<U>(other): Result<U, E>
Returns this
if the result is Err
, otherwise returns the passed Result
.
Type Parameter | Description |
---|---|
U |
The type of the value in the other Result . |
Parameter | Type | Description |
---|---|---|
other |
Result <U , E > |
The Result to return if this is Ok . |
Result
<U
, E
>
The passed Result
if this
is Ok
, otherwise returns this
(which is Err
).
andThen<U>(fn): Result<U, E>
Calls the provided function with the contained value if this
is Ok
, otherwise returns this
as Err
.
Type Parameter | Description |
---|---|
U |
The type of the value returned by the function. |
Parameter | Type | Description |
---|---|---|
fn |
(value ) => Result <U , E > |
A function that takes the Ok value and returns a Result . |
Result
<U
, E
>
The result of fn
if this
is Ok
, otherwise this
as Err
.
andThenAsync<U>(fn): AsyncResult<U, E>
Asynchronous version of andThen
.
Type Parameter |
---|
U |
Parameter | Type |
---|---|
fn |
(value ) => AsyncResult <U , E > |
AsyncResult
<U
, E
>
asErr<U>(): Result<U, E>
Transforms the current Result into a new Result where the type of the success result is replaced with a new type U
.
The type of the error result remains unchanged.
Useful where you need to return an Error chained to another type.
Just same as result as unknown as Result<U, E>
.
Type Parameter | Description |
---|---|
U |
The new type for the success result. |
Result
<U
, E
>
this
but the success result type is U
.
asOk<F>(): Result<T, F>
Transforms the current Result into a new Result where the type of the error result is replaced with a new type F
.
The type of the success result remains unchanged.
Just same as result as unknown as Result<T, F>
.
Type Parameter | Description |
---|---|
F |
The new type for the error result. |
Result
<T
, F
>
this
but the error result type is F
.
eq(other): boolean
Tests whether this
and other
are both Ok
containing equal values, or both are Err
containing equal errors.
Parameter | Type | Description |
---|---|---|
other |
Result <T , E > |
The other Result to compare with. |
boolean
true
if this
and other
are both Ok
with equal values, or both are Err
with equal errors, otherwise false
.
err(): Option<E>
Converts from Result<T, E>
to Option<E>
.
If the result is Err
, returns Some(E)
.
If the result is Ok
, returns None
.
Option
<E
>
expect(msg): T
Returns the contained Ok
value, with a provided error message if the result is Err
.
Parameter | Type | Description |
---|---|---|
msg |
string |
The error message to provide if the result is an Err . |
T
Throws an error with the provided message if the result is an Err
.
expectErr(msg): E
Returns the contained Err
value, with a provided error message if the result is Ok
.
Parameter | Type | Description |
---|---|---|
msg |
string |
The error message to provide if the result is an Ok . |
E
Throws an error with the provided message if the result is an Ok
.
flatten<T>(this): Result<T, E>
Converts from Result<Result<T, E>, E>
to Result<T, E>
.
If the result is Ok(Ok(T))
, returns Ok(T)
.
If the result is Ok(Err(E))
or Err(E)
, returns Err(E)
.
Type Parameter |
---|
T |
Parameter | Type |
---|---|
this |
Result <Result <T , E >, E > |
Result
<T
, E
>
inspect(fn): this
Calls the provided function with the contained value if this
is Ok
, for side effects only.
Does not modify the Result
.
Parameter | Type | Description |
---|---|---|
fn |
(value ) => void |
A function to call with the Ok value. |
this
this
, unmodified.
inspectErr(fn): this
Calls the provided function with the contained error if this
is Err
, for side effects only.
Does not modify the Result
.
Parameter | Type | Description |
---|---|---|
fn |
(error ) => void |
A function to call with the Err value. |
this
this
, unmodified.
isErr(): boolean
Returns true
if the result is Err
.
boolean
isErrAnd(predicate): boolean
Returns true
if the result is Err
and the provided predicate returns true
for the contained error.
Parameter | Type | Description |
---|---|---|
predicate |
(error ) => boolean |
A function that takes the Err value and returns a boolean. |
boolean
isErrAndAsync(predicate): Promise<boolean>
Asynchronous version of isErrAnd
.
Parameter | Type |
---|---|
predicate |
(error ) => Promise <boolean > |
Promise
<boolean
>
isOk(): boolean
Returns true
if the result is Ok
.
boolean
isOkAnd(predicate): boolean
Returns true
if the result is Ok
and the provided predicate returns true
for the contained value.
Parameter | Type | Description |
---|---|---|
predicate |
(value ) => boolean |
A function that takes the Ok value and returns a boolean. |
boolean
isOkAndAsync(predicate): Promise<boolean>
Asynchronous version of isOkAnd
.
Parameter | Type |
---|---|
predicate |
(value ) => Promise <boolean > |
Promise
<boolean
>
map<U>(fn): Result<U, E>
Maps a Result<T, E>
to Result<U, E>
by applying a function to a contained Ok
value,
leaving an Err
value untouched.
Type Parameter | Description |
---|---|
U |
The type of the value returned by the map function. |
Parameter | Type | Description |
---|---|---|
fn |
(value ) => U |
A function that takes the Ok value and returns a new value. |
Result
<U
, E
>
mapErr<F>(fn): Result<T, F>
Maps a Result<T, E>
to Result<T, F>
by applying a function to a contained Err
value,
leaving an Ok
value untouched.
Type Parameter | Description |
---|---|
F |
The type of the error returned by the map function. |
Parameter | Type | Description |
---|---|---|
fn |
(error ) => F |
A function that takes the Err value and returns a new error value. |
Result
<T
, F
>
mapOr<U>(defaultValue, fn): U
Maps a Result<T, E>
to U
by applying a function to the contained Ok
value (if Ok
), or returns the provided default (if Err
).
Type Parameter | Description |
---|---|
U |
The type of the value returned by the map function or the default value. |
Parameter | Type | Description |
---|---|---|
defaultValue |
U |
The value to return if the result is Err . |
fn |
(value ) => U |
A function that takes the Ok value and returns a new value. |
U
mapOrElse<U>(defaultFn, fn): U
Maps a Result<T, E>
to U
by applying a function to the contained Ok
value (if Ok
), or computes a default (if Err
).
Type Parameter | Description |
---|---|
U |
The type of the value returned by the map function or the default function. |
Parameter | Type | Description |
---|---|---|
defaultFn |
(error ) => U |
A function that returns the default value. |
fn |
(value ) => U |
A function that takes the Ok value and returns a new value. |
U
ok(): Option<T>
Converts from Result<T, E>
to Option<T>
.
If the result is Ok
, returns Some(T)
.
If the result is Err
, returns None
.
Option
<T
>
or<F>(other): Result<T, F>
Returns this
if it is Ok
, otherwise returns the passed Result
.
Type Parameter | Description |
---|---|
F |
The type of the error in the other Result . |
Parameter | Type | Description |
---|---|---|
other |
Result <T , F > |
The Result to return if this is Err . |
Result
<T
, F
>
this
if it is Ok
, otherwise returns other
.
orElse<F>(fn): Result<T, F>
Calls the provided function with the contained error if this
is Err
, otherwise returns this
as Ok
.
Type Parameter | Description |
---|---|
F |
The type of the error returned by the function. |
Parameter | Type | Description |
---|---|---|
fn |
(error ) => Result <T , F > |
A function that takes the Err value and returns a Result . |
Result
<T
, F
>
The result of fn
if this
is Err
, otherwise this
as Ok
.
orElseAsync<F>(fn): AsyncResult<T, F>
Asynchronous version of orElse
.
Type Parameter |
---|
F |
Parameter | Type |
---|---|
fn |
(error ) => AsyncResult <T , F > |
AsyncResult
<T
, F
>
toString(): string
Custom toString
implementation that uses the Result
's contained value.
string
transpose<T>(this): Option<Result<T, E>>
Transposes a Result
of an Option
into an Option
of a Result
.
Type Parameter | Description |
---|---|
T |
The type of the success value in the Ok variant of the Option . |
Parameter | Type |
---|---|
this |
Result <Option <T >, E > |
Some
containing Ok
if the result is Ok
containing Some
,
Some
containing Err
if the result is Err
,
None
if the result is Ok
containing None
.
unwrap(): T
Returns the contained Ok
value.
T
Throws an error if the result is an Err
.
unwrapErr(): E
Returns the contained Err
value.
E
Throws an error if the result is an Ok
.
unwrapOr(defaultValue): T
Returns the contained Ok
value or a provided default.
Parameter | Type | Description |
---|---|---|
defaultValue |
T |
The value to return if the result is an Err . |
T
unwrapOrElse(fn): T
Returns the contained Ok
value or computes it from a closure if the result is Err
.
Parameter | Type | Description |
---|---|---|
fn |
(error ) => T |
A function that takes the Err value and returns an Ok value. |
T
unwrapOrElseAsync(fn): Promise<T>
Asynchronous version of unwrapOrElse
.
Parameter | Type |
---|---|
fn |
(error ) => Promise <T > |
Promise
<T
>