package com.sunrisechoir.rnpatchql import com.facebook.react.bridge.Promise class Result(val value: T?, val error: String?) { companion object { fun ok(value: T): Result { return Result(value, null) } fun err(err: String): Result { return Result(null, err) } } fun map(mapper: (value: T) -> T2): Result { return if (this.value != null) { try { ok(mapper(this.value)) } catch (err: Throwable) { err(err.toString()) } } else { Result(null, this.error) } } fun isOk(): Boolean { return this.error == null } fun isErr(): Boolean { return this.error != null } fun mapErr(mapper: (err: String) -> String): Result { return if (this.error != null) { err(mapper(this.error)) } else { this } } fun pour(promise: Promise) { if (this.error != null) { promise.reject("0", this.error) } else { promise.resolve(this.value) } } }