import { Parser } from "./index"; import { IParser, OnParse, Optional } from "./interfaces"; export class MaybeParser implements IParser, Optional> { constructor( readonly parent: Parser, readonly description = { name: "Maybe", children: [parent], extras: [], } as const, ) {} parse(a: A, onParse: OnParse, Optional, C, D>): C | D { if (a === undefined) { return onParse.parsed(undefined); } const parser = this; const parentState = this.parent.enumParsed(a); if ("error" in parentState) { const { error } = parentState; error.parser = parser; return onParse.invalid(error); } return onParse.parsed(parentState.value); } }