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