From 6c6c9386be94cccb10b23b431003817972ccb0b6 Mon Sep 17 00:00:00 2001
From: Tangent Wantwight <tangent128@gmail.com>
Date: Fri, 19 Jul 2024 23:41:11 -0400
Subject: [PATCH] Change signature for Property pattern helpers

---
 debug/idv.ts |  8 ++++----
 lib/idv.ts   | 29 +++++++++--------------------
 2 files changed, 13 insertions(+), 24 deletions(-)

diff --git a/debug/idv.ts b/debug/idv.ts
index 7583dbe..f5447bb 100644
--- a/debug/idv.ts
+++ b/debug/idv.ts
@@ -1,5 +1,5 @@
 import { h } from "../lib/html";
-import { Idv, StringFromDocument } from "../lib/idv";
+import { Idv, StringProperty } from "../lib/idv";
 
 export function IdvDebug() {
   const textarea = h(
@@ -51,8 +51,8 @@ User: tirga
 
 const UserFromDocument = (idv: Idv) => {
   return {
-    shell: idv.getProperty("Shell", String, StringFromDocument),
-    groups: idv.getList("Group", String, StringFromDocument),
-    banner: idv.getProperty("Banner", String, StringFromDocument),
+    shell: idv.getProperty("Shell", StringProperty),
+    groups: idv.getList("Group", StringProperty),
+    banner: idv.getProperty("Banner", StringProperty),
   };
 };
diff --git a/lib/idv.ts b/lib/idv.ts
index b0c753a..c8339f5 100644
--- a/lib/idv.ts
+++ b/lib/idv.ts
@@ -1,5 +1,6 @@
 export type DistinguisherParser<T> = (distinguisher: 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;
 
 const LEADING_WHITESPACE = /^([ \t]+)/;
@@ -58,6 +59,7 @@ export class Idv {
 
           currentDocument = [];
           currentIndent = undefined;
+          // TODO: implement backslash escaping in the distinguisher
           this.collections[collection].push([distinguisher, currentDocument]);
         } else {
           throw new Error("Failed to parse a property");
@@ -68,29 +70,14 @@ export class Idv {
     return this;
   }
 
-  public getProperty<T>(
-    name: string,
-    parseDistinguisher: DistinguisherParser<T>,
-    parseDocument: DocumentParser<T>
-  ): T | undefined {
+  public getProperty<T>(name: string, parse: UnionParser<T>): T | undefined {
     const firstEntry = this.collections[name]?.[0];
-    return (
-      firstEntry &&
-      (firstEntry[1].length > 0
-        ? parseDocument(firstEntry[1])
-        : parseDistinguisher(firstEntry[0]))
-    );
+    return firstEntry && parse(firstEntry[0], firstEntry[1]);
   }
 
-  public getList<T>(
-    name: string,
-    parseDistinguisher: DistinguisherParser<T>,
-    parseDocument: DocumentParser<T>
-  ): T[] {
+  public getList<T>(name: string, parse: UnionParser<T>): T[] {
     return (this.collections[name] ?? []).map(([distinguisher, document]) =>
-      document.length > 0
-        ? parseDocument(document)
-        : parseDistinguisher(distinguisher)
+      parse(distinguisher, document)
     );
   }
 
@@ -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");