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