This commit is contained in:
swrup 2025-10-13 09:40:39 +02:00
parent 636d351a09
commit 7099a78667

View file

@ -120,37 +120,39 @@ let parse s =
| Error msg -> Fmt.failwith "config parse error: %s" msg | Error msg -> Fmt.failwith "config parse error: %s" msg
| Ok v -> fold_sections v | Ok v -> fold_sections v
let pp_item ppf { key; value } = module Pp_debug = struct
let open Fmt in let pp_item ppf { key; value } =
match String.contains value '"' || String.contains value ' ' with let open Fmt in
| false -> pf ppf "%s = %s" key value match String.contains value '"' || String.contains value ' ' with
| true -> pf ppf "%s = \"%s\"" key value | false -> pf ppf "%s = %s" key value
| true -> pf ppf "%s = \"%s\"" key value
let pp_line ppf line = let pp_line ppf line =
let open Fmt in let open Fmt in
match line with match line with
| Blank -> Fmt.nop ppf () | Blank -> Fmt.nop ppf ()
| Comment s -> pf ppf "#%s" s | Comment s -> pf ppf "#%s" s
| Header s -> pf ppf "[%s]" s | Header s -> pf ppf "[%s]" s
| Item item -> pf ppf "%a" pp_item item | Item item -> pf ppf "%a" pp_item item
let _pp_lines ppf raw_line_l = let _pp_lines ppf raw_line_l =
let open Fmt in let open Fmt in
pf ppf "%a" (list ~sep:(any "\n") pp_line) raw_line_l pf ppf "%a" (list ~sep:(any "\n") pp_line) raw_line_l
let pp_section ppf { header; items } = let pp_section ppf { header; items } =
let open Fmt in let open Fmt in
pf ppf "[%s]@\n%a" header (list ~sep:(any "\n") pp_item) items pf ppf "[%s]@\n%a" header (list ~sep:(any "\n") pp_item) items
let pp_config ppf l = let pp_config ppf l =
let open Fmt in let open Fmt in
pf ppf "%a" (list ~sep:(any "\n") pp_section) l pf ppf "%a" (list ~sep:(any "\n") pp_section) l
end
let () = let () =
let content = read_file "default.config" in let content = read_file "default.config" in
(*Fmt.pr "%s" content;*) (*Fmt.pr "%s" content;*)
let config = parse content in let config = parse content in
(*Fmt.pr "%a@." pp_lines config;*) (*Fmt.pr "%a@." pp_lines config;*)
Fmt.pr "%a@." pp_config config; Fmt.pr "%a@." Pp_debug.pp_config config;
() ()