aboutsummaryrefslogtreecommitdiff
path: root/lib/naming/config.ml
blob: ce1b433443059a71f6c6ea03b368fc3b15c5d854 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module OI = Colitur_kernel.Overlay_ini

type t = {
  lang : string option;
  overlays : string list;
  template : string option;
  format : string option;
  unknown_keys : string list;
}

let empty = { lang = None; overlays = []; template = None; format = None; unknown_keys = [] }

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 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)

let resolve ~flag ~config ~default =
  match flag with
  | Some v -> (v, "flag")
  | None -> ( match config with Some v -> (v, "config") | None -> (default, "default"))