Change signature for Property pattern helpers

This commit is contained in:
Tangent Wantwight 2024-07-19 23:41:11 -04:00
parent 63c41f07cb
commit 6c6c9386be
2 changed files with 13 additions and 24 deletions

View file

@ -1,5 +1,5 @@
import { h } from "../lib/html"; import { h } from "../lib/html";
import { Idv, StringFromDocument } from "../lib/idv"; import { Idv, StringProperty } from "../lib/idv";
export function IdvDebug() { export function IdvDebug() {
const textarea = h( const textarea = h(
@ -51,8 +51,8 @@ User: tirga
const UserFromDocument = (idv: Idv) => { const UserFromDocument = (idv: Idv) => {
return { return {
shell: idv.getProperty("Shell", String, StringFromDocument), shell: idv.getProperty("Shell", StringProperty),
groups: idv.getList("Group", String, StringFromDocument), groups: idv.getList("Group", StringProperty),
banner: idv.getProperty("Banner", String, StringFromDocument), banner: idv.getProperty("Banner", StringProperty),
}; };
}; };

View file

@ -1,5 +1,6 @@
export type DistinguisherParser<T> = (distinguisher: string) => T; export type DistinguisherParser<T> = (distinguisher: string) => T;
export type DocumentParser<T> = (document: string[]) => T; export type DocumentParser<T> = (document: string[]) => T;
export type UnionParser<T> = (distinguisher: string, document: string[]) => T;
export type IdvConverter<T> = (idv: Idv) => T; export type IdvConverter<T> = (idv: Idv) => T;
const LEADING_WHITESPACE = /^([ \t]+)/; const LEADING_WHITESPACE = /^([ \t]+)/;
@ -58,6 +59,7 @@ export class Idv {
currentDocument = []; currentDocument = [];
currentIndent = undefined; currentIndent = undefined;
// TODO: implement backslash escaping in the distinguisher
this.collections[collection].push([distinguisher, currentDocument]); this.collections[collection].push([distinguisher, currentDocument]);
} else { } else {
throw new Error("Failed to parse a property"); throw new Error("Failed to parse a property");
@ -68,29 +70,14 @@ export class Idv {
return this; return this;
} }
public getProperty<T>( public getProperty<T>(name: string, parse: UnionParser<T>): T | undefined {
name: string,
parseDistinguisher: DistinguisherParser<T>,
parseDocument: DocumentParser<T>
): T | undefined {
const firstEntry = this.collections[name]?.[0]; const firstEntry = this.collections[name]?.[0];
return ( return firstEntry && parse(firstEntry[0], firstEntry[1]);
firstEntry &&
(firstEntry[1].length > 0
? parseDocument(firstEntry[1])
: parseDistinguisher(firstEntry[0]))
);
} }
public getList<T>( public getList<T>(name: string, parse: UnionParser<T>): T[] {
name: string,
parseDistinguisher: DistinguisherParser<T>,
parseDocument: DocumentParser<T>
): T[] {
return (this.collections[name] ?? []).map(([distinguisher, document]) => return (this.collections[name] ?? []).map(([distinguisher, document]) =>
document.length > 0 parse(distinguisher, document)
? parseDocument(document)
: parseDistinguisher(distinguisher)
); );
} }
@ -131,4 +118,6 @@ export class Idv {
} }
} }
export const StringFromDocument = (lines: string[]) => lines.join("\n"); // TODO: implement backslash-escaping in the document?
export const StringProperty = (distinguisher: string, lines: string[]) =>
lines.length == 0 ? distinguisher : lines.join("\n");