aboutsummaryrefslogtreecommitdiff
path: root/lib/naming/config.ml
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-08-19 13:48:39 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-08-19 13:48:39 +0200
commit59fbd3718dbf2721557b97d7b4e87dce38fb745c (patch)
tree818feefb15c328bca3ecf9b1a24a2023d57980c4 /lib/naming/config.ml
parent6bdd5afc5e185b639f9cf5e7c3d662ddc3ecf0e1 (diff)
downloadcolitur-59fbd3718dbf2721557b97d7b4e87dce38fb745c.tar.gz
colitur-59fbd3718dbf2721557b97d7b4e87dce38fb745c.zip
fix(naming): config fix round 1 -- unknown sections, O(n) accumulate
F1: test_unknown_key_is_reported_not_fatal never asserted unknown_keys itself, only that parsing survives -- a no-op accumulator passed it. Now asserts the key is actually collected. F2: a misspelled section name, e.g. [deafults], was silently discarded -- Ok empty, lang and everything else gone, nothing reported. That is the highest-value typo this feature exists to catch. Any section other than [defaults] is now collected into a new Config.unknown_sections, kept separate from unknown_keys so the CLI can word the two warnings differently. Still non-fatal: a newer colitur's added section must not break an older binary. F3: overlays and unknown_keys accumulated with '@ [v]' per line, O(n^2) over the field count. Cons during the fold, List.rev once at the end. F4: documented that lang/template/format are last-wins on a repeated key, the opposite direction from Overlay_ini.get's first-wins over the same section type.
Diffstat (limited to 'lib/naming/config.ml')
-rw-r--r--lib/naming/config.ml55
1 files changed, 37 insertions, 18 deletions
diff --git a/lib/naming/config.ml b/lib/naming/config.ml
index ce1b433..0fafd1f 100644
--- a/lib/naming/config.ml
+++ b/lib/naming/config.ml
@@ -6,36 +6,55 @@ type t = {
template : string option;
format : string option;
unknown_keys : string list;
+ unknown_sections : string list;
}
-let empty = { lang = None; overlays = []; template = None; format = None; unknown_keys = [] }
+let empty =
+ { lang = None; overlays = []; template = None; format = None; unknown_keys = [];
+ unknown_sections = [] }
let lang t = t.lang
let overlays t = t.overlays
let template t = t.template
let format t = t.format
let unknown_keys t = t.unknown_keys
+let unknown_sections t = t.unknown_sections
let of_string text =
match OI.parse_sections text with
| Error e -> Error e
- | Ok sections -> (
- match List.find_opt (fun (s : OI.section) -> s.OI.name = "defaults") sections with
- | None -> Ok empty
- | Some s ->
- let acc =
- List.fold_left
- (fun acc (k, v) ->
- match k with
- | "lang" -> { acc with lang = Some v }
- | "template" -> { acc with template = Some v }
- | "format" -> { acc with format = Some v }
- (* accumulates: a user has more than one overlay *)
- | "overlay" -> { acc with overlays = acc.overlays @ [ v ] }
- | other -> { acc with unknown_keys = acc.unknown_keys @ [ other ] })
- empty s.OI.fields
- in
- Ok acc)
+ | Ok sections ->
+ (* Any section that is not [defaults] is unrecognised -- including a
+ plain typo such as [deafults] -- and must be REPORTED, never
+ silently dropped: that is precisely the failure this feature exists
+ to surface. *)
+ let unknown_sections =
+ List.filter_map
+ (fun (s : OI.section) -> if s.OI.name = "defaults" then None else Some s.OI.name)
+ sections
+ in
+ let acc =
+ match List.find_opt (fun (s : OI.section) -> s.OI.name = "defaults") sections with
+ | None -> empty
+ | Some s ->
+ (* Cons then reverse once at the end, not `@ [v]` per line: the
+ latter is O(n^2) over the field count, a real hang on a
+ machine-generated file with many overlay lines. *)
+ let acc =
+ List.fold_left
+ (fun acc (k, v) ->
+ match k with
+ | "lang" -> { acc with lang = Some v }
+ | "template" -> { acc with template = Some v }
+ | "format" -> { acc with format = Some v }
+ (* accumulates: a user has more than one overlay *)
+ | "overlay" -> { acc with overlays = v :: acc.overlays }
+ | other -> { acc with unknown_keys = other :: acc.unknown_keys })
+ empty s.OI.fields
+ in
+ { acc with overlays = List.rev acc.overlays; unknown_keys = List.rev acc.unknown_keys }
+ in
+ Ok { acc with unknown_sections }
let resolve ~flag ~config ~default =
match flag with