aboutsummaryrefslogtreecommitdiffstats
path: root/lib/concepts/Condition.ml
blob: a9082adc200df7b1993e5be15f5e7d198a25ac80 (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
(* @uses Value -- renders property values in to_string_hum *)

type properties = (string, Value.value) BatMap.t
type ps = properties -> properties

type condition =
  {name: string; message: string; backtrace: Printexc.raw_backtrace; properties: properties}

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

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

let complement props ({properties; _} as condition) = {condition with properties= props properties}

let to_string_hum {name; message; properties; backtrace} =
  let properties' =
    properties
    |> BatMap.to_seq
    |> BatSeq.to_string ~first:"" ~last:"" ~sep:"\n" (fun (k, v) ->
        "\t" ^ k ^ ": " ^ Value.to_string v )
  in
  let backtrace' = Printexc.raw_backtrace_to_string backtrace in
  name ^ ": " ^ message ^ "\n" ^ properties' ^ "\n\nBacktrace:\n" ^ backtrace'