Source file collection.ml

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(*****************************************************************************)
(*                                                                           *)
(*  Copyright (C) 2026 Yves Ndiaye                                           *)
(*                                                                           *)
(* This Source Code Form is subject to the terms of the Mozilla Public       *)
(* License, v. 2.0. If a copy of the MPL was not distributed with this       *)
(* file, You can obtain one at https://mozilla.org/MPL/2.0/.                 *)
(*                                                                           *)
(*****************************************************************************)

module Keys = struct
  let name = "name"
  let database = "database"
  let music_directory = "music_directory"
  let plugins_directory = "plugins_directory"
end

type t = {
  name : string;
  database : string;
  music_directory : string;
  plugins_directory : string;
}

let section = "collection"

let init ~name ~database ~music_directory ~plugins_directory =
  { name; database; music_directory; plugins_directory }

let conv key section =
  match Section.assoc key section with
  | Some e ->
      Ok e
  | None ->
      Error (Err.missing_key key)

let of_section section =
  let ( let* ) = Result.bind in
  let* name = conv Keys.name section in
  let* database = conv Keys.database section in
  let* music_directory = conv Keys.music_directory section in
  let* plugins_directory = conv Keys.plugins_directory section in
  Ok { name; database; music_directory; plugins_directory }

let to_section t =
  let { name; database; music_directory; plugins_directory } = t in
  Section.init section
    [
      (Keys.name, name);
      (Keys.database, database);
      (Keys.music_directory, music_directory);
      (Keys.plugins_directory, plugins_directory);
    ]

let name t = t.name
let database t = t.database
let music_directory t = t.music_directory
let plugins_directory t = t.plugins_directory