aboutsummaryrefslogtreecommitdiffstats
path: root/lib/condition.ml
blob: e5c44b7c27d0abc5cac1a5a54c9ec0d26e0fe3d9 (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
class type serializable = object
  method as_sexp : Sexplib.Sexp.t
end

class literal (expr : Sexplib.Sexp.t) : serializable = object
  method as_sexp = expr
end

let of_sexp (expr : Sexplib.Sexp.t) = new literal expr
let of_string (s : string) = new literal (Sexplib.Sexp.Atom s)

type t = { name : string; properties : (string, serializable) BatMap.t }

let condition name ?parent props =
  { name;
    properties =
      parent
      |> Option.map (fun { properties; _ } -> properties)
      |> Option.value ~default:BatMap.empty
      |> props }

let ( |=| ) name value = (fun p -> BatMap.add name value p)
let ( & ) l r = (fun p -> r (l p))
let empty = (fun p -> p)

let to_sexp { name; properties } =
  let open Sexplib.Sexp in
  let properties' =
    BatMap.to_seq properties
    |> BatSeq.map (fun (k, v) -> List [Atom k; v#as_sexp])
    |> BatList.of_seq in
  List (Atom name :: properties')