OCaml HTML5 parser/serialiser based on Python's JustHTML
1(** Message collector for accumulating validation messages. *)
2
3type t = { mutable messages : Message.t list }
4
5let create () = { messages = [] }
6
7let add t msg = t.messages <- msg :: t.messages
8
9(** Add a message from a typed error code *)
10let add_typed t ?location ?element ?attribute ?extract error_code =
11 let msg = Message.of_error_code ?location ?element ?attribute ?extract error_code in
12 add t msg
13
14(** Add an error from a typed error code *)
15let add_error_code t ?location ?element ?attribute ?extract error_code =
16 add_typed t ?location ?element ?attribute ?extract error_code
17
18(** Legacy: Add an error with manual message text *)
19let add_error t ~message ?code ?location ?element ?attribute ?extract () =
20 let msg =
21 Message.error ~message ?code ?location ?element ?attribute ?extract ()
22 in
23 add t msg
24
25(** Legacy: Add a warning with manual message text *)
26let add_warning t ~message ?code ?location ?element ?attribute ?extract () =
27 let msg =
28 Message.warning ~message ?code ?location ?element ?attribute ?extract ()
29 in
30 add t msg
31
32(** Legacy: Add an info message with manual message text *)
33let add_info t ~message ?code ?location ?element ?attribute ?extract () =
34 let msg =
35 Message.info ~message ?code ?location ?element ?attribute ?extract ()
36 in
37 add t msg
38
39let messages t = List.rev t.messages
40
41let errors t =
42 List.filter (fun msg -> msg.Message.severity = Message.Error) (messages t)
43
44let warnings t =
45 List.filter (fun msg -> msg.Message.severity = Message.Warning) (messages t)
46
47let infos t =
48 List.filter (fun msg -> msg.Message.severity = Message.Info) (messages t)
49
50let has_errors t =
51 List.exists (fun msg -> msg.Message.severity = Message.Error) t.messages
52
53let count t = List.length t.messages
54
55let error_count t =
56 List.fold_left
57 (fun acc msg ->
58 if msg.Message.severity = Message.Error then acc + 1 else acc)
59 0 t.messages
60
61let clear t = t.messages <- []