import { IParser, OnParse } from "./interfaces"; import { Parser } from "./parser"; export class OrParsers implements IParser { constructor( readonly parent: Parser, readonly otherParser: Parser, readonly description = { name: "Or", children: [parent, otherParser], extras: [], } as const, ) {} parse(a: A & A2, onParse: OnParse): C | D { const parser = this; const parent = this.parent.enumParsed(a); if ("value" in parent) { return onParse.parsed(parent.value); } const other = this.otherParser.enumParsed(a); if ("error" in other) { const { error } = other; error.parser = parser; return onParse.invalid(error); } return onParse.parsed(other.value); } }