@rustresult/result
Version:
Rust-like Result and ResultAsync for Javascript
121 lines • 4.88 kB
JavaScript
import { Err, Ok } from './factory';
import { RustlikeResult } from './RustlikeResult';
export class RustlikeResultAsync {
constructor(promise) {
this._promise = Promise.resolve(promise);
}
isOk() {
return this._promise.then((result) => result.isOk());
}
isOkAnd(fn) {
return this._promise.then((result) => result.isOk() && fn(result.unwrapUnchecked()));
}
isErr() {
return this._promise.then((result) => result.isErr());
}
isErrAnd(fn) {
return this._promise.then((result) => result.isErr() && fn(result.unwrapErrUnchecked()));
}
ok() {
return this._promise.then((result) => result.ok());
}
err() {
return this._promise.then((result) => result.err());
}
map(op) {
return new RustlikeResultAsync(this._promise.then(async (result) => result.isOk() ? Ok(await op(result.unwrapUnchecked())) : Err(result.unwrapErrUnchecked())));
}
mapOr(fallback, map) {
return this._promise.then((result) => (result.isOk() ? map(result.unwrapUnchecked()) : fallback));
}
mapOrElse(fallback, map) {
return this._promise.then((result) => result.isOk() ? map(result.unwrapUnchecked()) : fallback(result.unwrapErrUnchecked()));
}
mapErr(op) {
return new RustlikeResultAsync(this._promise.then(async (result) => result.isOk() ? Ok(result.unwrapUnchecked()) : Err(await op(result.unwrapErrUnchecked()))));
}
inspect(fn) {
return new RustlikeResultAsync(this._promise.then(async (result) => {
if (result.isOk()) {
await fn(result.unwrapUnchecked());
}
return result;
}));
}
inspectErr(fn) {
return new RustlikeResultAsync(this._promise.then(async (result) => {
if (result.isErr()) {
await fn(result.unwrapErrUnchecked());
}
return result;
}));
}
expect(msg) {
return this._promise.then((result) => result.expect(msg));
}
unwrap() {
return this._promise.then((result) => result.unwrap());
}
expectErr(msg) {
return this._promise.then((result) => result.expectErr(msg));
}
unwrapErr() {
return this._promise.then((result) => result.unwrapErr());
}
unwrapOr(fallback) {
return this._promise.then((result) => result.unwrapOr(fallback));
}
unwrapOrElse(op) {
return this._promise.then((result) => result.isOk() ? result.unwrapUnchecked() : op(result.unwrapErrUnchecked()));
}
unwrapUnchecked() {
return this._promise.then((result) => result.unwrapUnchecked());
}
unwrapErrUnchecked() {
return this._promise.then((result) => result.unwrapErrUnchecked());
}
and(res) {
return new RustlikeResultAsync(this._promise.then((result) => (result.isOk() ? res : Err(result.unwrapErrUnchecked()))));
}
andThen(op) {
return new RustlikeResultAsync(this._promise.then((result) => result.isOk() ? op(result.unwrapUnchecked()) : Err(result.unwrapErrUnchecked())));
}
or(res) {
return new RustlikeResultAsync(this._promise.then((result) => (result.isOk() ? Ok(result.unwrapUnchecked()) : res)));
}
orElse(op) {
return new RustlikeResultAsync(this._promise.then((result) => result.isOk() ? Ok(result.unwrapUnchecked()) : op(result.unwrapErrUnchecked())));
}
transpose() {
return this._promise.then((result) => result.transpose());
}
static async _equal(self, other) {
const isSelfResult = self instanceof RustlikeResult || self instanceof RustlikeResultAsync;
const isOtherResult = other instanceof RustlikeResult || other instanceof RustlikeResultAsync;
if (isSelfResult && isOtherResult) {
const _self = await self;
const _other = await other;
const isOk = _self.isOk();
if (isOk !== _other.isOk())
return false;
return isOk
? RustlikeResultAsync._equal(_self.unwrapUnchecked(), _other.unwrapUnchecked())
: RustlikeResultAsync._equal(_self.unwrapErrUnchecked(), _other.unwrapErrUnchecked());
}
return self === other || (Number.isNaN(self) && Number.isNaN(other));
}
async equal(other) {
const _self = await this._promise;
const _other = await other;
const isOk = _self.isOk();
if (isOk !== _other.isOk())
return false;
return isOk
? RustlikeResultAsync._equal(_self.unwrapUnchecked(), _other.unwrapUnchecked())
: RustlikeResultAsync._equal(_self.unwrapErrUnchecked(), _other.unwrapErrUnchecked());
}
then(onfulfilled, onrejected) {
return this._promise.then(onfulfilled, onrejected);
}
}
//# sourceMappingURL=RustlikeResultAsync.js.map