fix rules failing no-prefix extracts

This commit is contained in:
JOLIMAITRE Matthieu 2024-08-07 11:18:17 +02:00
parent 8c18398ae4
commit fa524a5976
4 changed files with 54 additions and 5 deletions

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"deno.enable": true
}

38
example.ts Executable file
View file

@ -0,0 +1,38 @@
#!/bin/env -S deno run
import { extr } from "./mod.ts"
const date = ["month", " ", "day", " ", "h", ":", "m", ":", "s"] as const;
const service = extr("", ...date, " ", "device", " ", "service", "[", "pid", "]: ", "line");
const kernel_ = extr("", ...date, " ", "device", " kernel: ", "line");
const lines = [
"août 06 09:25:18 navis systemd[1]: Finished Load Kernel Module configfs.",
"août 06 09:25:18 navis systemd[1]: modprobe@drm.service: Succeeded.",
"août 06 09:25:18 navis systemd[1]: Finished Load Kernel Module drm.",
"août 06 09:25:18 navis systemd[1]: Mounting Kernel Configuration File System...",
"août 06 09:25:18 navis kernel: fuse: init (API version 7.32)",
"août 06 09:25:18 navis systemd[1]: modprobe@fuse.service: Succeeded.",
"août 06 09:25:18 navis systemd[1]: Finished Load Kernel Module fuse.",
"août 06 09:25:18 navis systemd[1]: Mounted Kernel Configuration File System.",
"août 06 09:25:18 navis systemd[1]: Mounting FUSE Control File System...",
"août 06 09:25:18 navis systemd[1]: Mounted FUSE Control File System.",
"août 06 09:25:18 navis kernel: EXT4-fs (sda4): re-mounted. Opts: errors=remount-ro",
"août 06 09:25:18 navis systemd[1]: Finished Remount Root and Kernel File Systems.",
"août 06 09:25:18 navis systemd[1]: Condition check resulted in Rebuild Hardware Database being skipped.",
"août 06 09:25:18 navis systemd[1]: Condition check resulted in Platform Persistent Storage Archival being skipped.",
"août 06 09:25:18 navis systemd[1]: Starting Load/Save Random Seed...",
"août 06 09:25:18 navis systemd[1]: Starting Create System Users...",
"août 06 09:25:18 navis kernel: lp: driver loaded but no devices found",
"août 06 09:25:18 navis kernel: ppdev: user-space parallel port driver",
"août 06 09:25:18 navis systemd[1]: Finished Load/Save Random Seed.",
"août 06 09:25:18 navis systemd[1]: Finished Load Kernel Modules.",
"août 06 09:25:18 navis systemd[1]: Condition check resulted in First Boot Complete being skipped.",
"août 06 09:25:18 navis systemd[1]: Starting Apply Kernel Variables...",
"août 06 09:25:18 navis systemd[1]: Finished Create System Users.",
];
const mapped = lines.map(line => service.or(kernel_).get(line));
const failed = mapped.filter(m => m === null).length;
const content = mapped.filter(m => m !== null).map(m => "service" in m! ? [m.service, m.line] : ["kernel", m!.line]);
console.log({ failed, content })

View file

@ -17,18 +17,26 @@ export class Rule<V extends string[]> {
if (text.length > 0) return null;
else return {} as StructOfArr<V, string>;
}
if (!text.includes(first_sep.word)) return null;
let [val, rest] = text.split(first_sep.word, 2);
let [val, rest] = split_once(text, first_sep.word);
if (rest === null) return null;
const result = {} as Record<string, string>;
for (const [key, sep] of zip(this.vars, this.separators.slice(1))) {
if (sep.kind === "end") {
result[key] = rest;
break;
}
if (!rest.includes(sep.word)) return null;
[val, rest] = rest.split(sep.word, 2);
[val, rest] = split_once(rest, sep.word);
if (rest === null) return null;
result[key] = val;
}
return result as StructOfArr<V, string>;
}
}
function split_once(text: string, separator: string) {
const cursor = text.indexOf(separator);
if (cursor === -1) return [text, null] as const;
const left = text.slice(0, cursor);
const right = text.slice(cursor + separator.length);
return [left, right] as const;
}

2
mod.ts
View file

@ -1 +1 @@
import { extr } from "./lib/lib.ts"
export { extr } from "./lib/lib.ts"