@rustresult/result
Version:
Rust-like Result and ResultAsync for Javascript
138 lines • 4.12 kB
JavaScript
import { RustlikeResultAsync } from './RustlikeResultAsync';
export class RustlikeResult {
constructor(type, value) {
this._type = type;
if (type === 'ok') {
this._value = value;
this._error = undefined;
}
else {
this._value = undefined;
this._error = value;
}
}
static Ok(value) {
return new RustlikeResult('ok', value);
}
static Err(error) {
return new RustlikeResult('err', error);
}
isOk() {
return this._type === 'ok';
}
isOkAnd(fn) {
return this.isOk() && fn(this._value);
}
isErr() {
return this._type === 'err';
}
isErrAnd(fn) {
return this.isErr() && fn(this._error);
}
ok() {
return this.isOk() ? this._value : undefined;
}
err() {
return this.isOk() ? undefined : this._error;
}
map(op) {
return this.isOk() ? RustlikeResult.Ok(op(this._value)) : RustlikeResult.Err(this._error);
}
mapOr(fallback, map) {
return this.isOk() ? map(this._value) : fallback;
}
mapOrElse(fallback, map) {
return this.isOk() ? map(this._value) : fallback(this._error);
}
mapErr(op) {
return this.isOk() ? RustlikeResult.Ok(this._value) : RustlikeResult.Err(op(this._error));
}
inspect(fn) {
if (this.isOk()) {
fn(this._value);
}
return this;
}
inspectErr(fn) {
if (this.isErr()) {
fn(this._error);
}
return this;
}
_unwrapFailed(msg, err) {
throw new Error(`${msg}: ${String(err)}`);
}
expect(msg) {
return this.isOk() ? this._value : this._unwrapFailed(msg, this._error);
}
unwrap() {
if (this.isOk())
return this._value;
throw new Error(String(this._error));
}
expectErr(msg) {
return this.isErr() ? this._error : this._unwrapFailed(msg, this._value);
}
unwrapErr() {
if (this.isErr())
return this._error;
throw new Error(String(this._value));
}
unwrapOr(fallback) {
return this.isOk() ? this._value : fallback;
}
unwrapOrElse(op) {
return this.isOk() ? this._value : op(this._error);
}
unwrapUnchecked() {
return this._value;
}
unwrapErrUnchecked() {
return this._error;
}
and(res) {
return this.isOk() ? res : RustlikeResult.Err(this._error);
}
andThen(op) {
return this.isOk() ? op(this._value) : RustlikeResult.Err(this._error);
}
or(res) {
return this.isOk() ? RustlikeResult.Ok(this._value) : res;
}
orElse(op) {
return this.isOk() ? RustlikeResult.Ok(this._value) : op(this._error);
}
transpose() {
if (this.isOk()) {
return this._value === undefined || this._value === null ? undefined : RustlikeResult.Ok(this._value);
}
return RustlikeResult.Err(this._error);
}
static _equal(self, other) {
const isSelfResult = self instanceof RustlikeResult;
const isOtherResult = other instanceof RustlikeResult;
if (isSelfResult && isOtherResult) {
const _self = self;
const _other = other;
const isOk = _self.isOk();
if (isOk !== _other.isOk())
return false;
return isOk
? RustlikeResult._equal(_self.unwrapUnchecked(), _other.unwrapUnchecked())
: RustlikeResult._equal(_self.unwrapErrUnchecked(), _other.unwrapErrUnchecked());
}
return self === other || (Number.isNaN(self) && Number.isNaN(other));
}
equal(other) {
const isOk = this.isOk();
if (isOk !== other.isOk())
return false;
return isOk
? RustlikeResult._equal(this._value, other.unwrapUnchecked())
: RustlikeResult._equal(this._error, other.unwrapErrUnchecked());
}
async() {
return new RustlikeResultAsync(this);
}
}
//# sourceMappingURL=RustlikeResult.js.map