import { Parser } from "./index";
import { IParser, OnParse } from "./interfaces";
export class ConcatParsers implements IParser {
private constructor(
readonly parent: Parser,
readonly otherParser: Parser,
readonly description = {
name: "Concat",
children: [parent, otherParser],
extras: [],
} as const,
) {}
static of(parent: Parser, otherParser: Parser) {
if (parent.unwrappedParser().description.name === "Any") {
return otherParser;
}
if (otherParser.unwrappedParser().description.name === "Any") {
return parent;
}
return new ConcatParsers(parent, otherParser);
}
parse(a: A, onParse: OnParse): C | D {
const parent = this.parent.enumParsed(a);
if ("error" in parent) {
return onParse.invalid(parent.error);
}
const other = this.otherParser.enumParsed(parent.value);
if ("error" in other) {
return onParse.invalid(other.error);
}
return onParse.parsed(other.value);
}
}