import {isFailure, map, Result} from "@http4t/result"; import {PathMatcher, PathResult} from "../PathMatcher"; export type ParserResult = Result; export interface Parser { parse(value: string): ParserResult; unparse(value: T): string; } export abstract class ParserPath implements PathMatcher { protected constructor(private readonly base: PathMatcher, private readonly parser: Parser) { } consume(path: string): PathResult { const result = this.base.consume(path); if (isFailure(result)) return result; const parsed = this.parser.parse(result.value.value); return map( parsed, value => ({ ...result.value, value: value }), error => ({message: error, remaining: path})) } expand(value: T): string { return this.base.expand(this.parser.unparse(value)); } }