summaryrefslogtreecommitdiff
path: root/src/config_tree.ml
blob: cd2eafd7954717a095cd427f5c7ae391ceeef7f6 (plain)
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
type value_behaviour = AddValue | ReplaceValue

exception Duplicate_value
exception Node_has_no_value

type config_node_data = {
    values: string list;
    comment: string;
}

type t = config_node_data Vytree.t

let default_data = {
    values = [];
    comment = "";
}

let make name = Vytree.make default_data name

let replace_value node path value =
    let data = {default_data with values=[value]} in
    Vytree.update node path data

let add_value node path value =
    let node' = Vytree.get node path in
    let data = Vytree.data_of_node node' in
    let values = data.values in
    match (Vylist.find (fun x -> x = value) values) with
    | Some _ -> raise Duplicate_value
    | None ->
        let values = values @ [value] in
        Vytree.update node path ({data with values=values})

let set_value node path value behaviour =
    match behaviour with
    | AddValue -> add_value node path value
    | ReplaceValue -> replace_value node path value

let set node path value position behaviour =
    if Vytree.exists node path then set_value node path value behaviour
    else Vytree.insert ~position:position node path {default_data with values=[value]}

let get_values node path =
    let node' = Vytree.get node path in
    let data = Vytree.data_of_node node' in
    data.values

let get_value node path =
    let values = get_values node path in
    match values with
    | [] -> raise Node_has_no_value
    | x :: _ -> x