import { Ok , Err } from './helpers' ; import { panic } from '../helpers' ; import { Inner } from '../option/types' ; import { Result , ResultKind } from './types' ; import { OptionEquipped } from '../option/equipped' ; import { None } from '../option/consts' ; /**
 * Wrapper for Result<T, E> containing all methods (that make sense)
 * from Rust's Result<T, E>
 */ export class ResultEquipped < T , E > { private _res : Result < T , E > ; constructor ( res ) { this . _res = res ; } get inner ( ) : Result < T , E > { return this . _res ; } get __kind ( ) : ResultKind { return this . _res . __kind ; } get data ( ) : T | E { return this . _res . data ; } isOk ( ) { return this . _res . __kind === ResultKind . Ok ; } isErr ( ) { return this . _res . __kind === ResultKind . Err ; } contains ( value ) { if ( this . _res . __kind === ResultKind . Ok ) { return this . _res . data === value ; } else { return false ; } } containsErr ( value ) { if ( this . _res . __kind === ResultKind . Err ) { return this . _res . data === value ; } else { return false ; } } ok ( ) { if ( this . _res . __kind === ResultKind . Ok ) { return new OptionEquipped ( this . _res . data ) ; } else { return new OptionEquipped < T > ( None ) ; } } err ( ) { if ( this . _res . __kind === ResultKind . Err ) { return new OptionEquipped ( this . _res . data ) ; } else { return new OptionEquipped < E > ( None ) ; } } map < U > ( f ) { if ( this . _res . __kind === ResultKind . Ok ) { return new ResultEquipped < U , E > ( Ok ( f ( this . _res . data ) ) ) ; } else { return this as unknown as ResultEquipped < U , E > ; } } mapErr < F > ( f ) { if ( this . _res . __kind === ResultKind . Err ) { return new ResultEquipped < T , F > ( Err ( f ( this . _res . data ) ) ) ; } else { return this as unknown as ResultEquipped < T , F > ; } } mapOr < U > ( d , f ) { if ( this . _res . __kind === ResultKind . Ok ) { return f ( this . _res . data ) ; } else { return d ; } } mapOrElse < U > ( df , mf ) { if ( this . _res . __kind === ResultKind . Ok ) { return mf ( this . _res . data ) ; } else { return df ( ) ; } } and < U > ( res ) { if ( this . _res . __kind === ResultKind . Ok ) { return new ResultEquipped ( res ) ; } else { return this as unknown as ResultEquipped < U , E > ; } } andThen < U > ( f ) { if ( this . _res . __kind === ResultKind . Ok ) { return new ResultEquipped ( f ( this . _res . data ) ) ; } else { return this as unknown as ResultEquipped < U , E > ; } } or < F > ( res ) { if ( this . _res . __kind === ResultKind . Err ) { return new ResultEquipped < T , F > ( res ) ; } else { return this as unknown as ResultEquipped < T , F > ; } } orElse < F > ( f ) { if ( this . _res . __kind === ResultKind . Err ) { return new ResultEquipped ( f ( this . _res . data ) ) ; } else { return this as unknown as ResultEquipped < T , F > ; } } unwrap ( ) { if ( this . _res . __kind === ResultKind . Ok ) { return this . _res . data ; } else { return panic ( `called ResultEquipped.unwrap() on an \`Err\` value ${ JSON . stringify ( this . _res . data ) }` ) ; } } unwrapErr ( ) { if ( this . _res . __kind === ResultKind . Err ) { return this . _res . data ; } else { return panic ( `called ResultEquipped.unwrapErr() on an \`Ok\` value: ${ JSON . stringify ( this . _res . data ) }` ) ; } } unwrapOr ( d ) { if ( this . _res . __kind === ResultKind . Ok ) { return this . _res . data ; } else { return d ; } } unwrapOrElse ( f ) { if ( this . _res . __kind === ResultKind . Ok ) { return this . _res . data ; } else { return f ( ) ; } } expect ( msg ) { if ( this . _res . __kind === ResultKind . Ok ) { return this . _res . data ; } else { return panic ( `${ msg }: ${ JSON . stringify ( this . _res . data ) }` ) ; } } expectErr ( msg ) { if ( this . _res . __kind === ResultKind . Err ) { return this . _res . data ; } else { return panic ( `${ msg }: ${ JSON . stringify ( this . _res . data ) }` ) ; } } transpose < I extends Inner < T > > ( ) { if ( this . _res . __kind === ResultKind . Ok ) { if ( this . _res . data == null ) { return new OptionEquipped < Result < I , E > > ( None ) ; } else { return new OptionEquipped < Result < I , E > > ( Ok ( this . _res . data as I ) ) ; } } else { return new OptionEquipped ( Err ( this . _res . data ) ) ; } } flatten ( ) { const value = this . _res . data as unknown as Result < T , E > ; if ( value . __kind == null ) { return this ; } return new ResultEquipped ( { __kind : value . __kind , data : value . data , } as Result < T , E > ) ; } }