@@ -4,14 +4,14 @@ open Format
44
55type entry = {
66 fields : string String.Map .t
7- } [@@ deriving compare , sexp ]
7+ } [@@ deriving bin_io , compare , sexp ]
88
99type row = {row : entry array } [@@ deriving sexp ]
1010type 'a seq = 'a Sequence .t
1111
1212module Type = struct
1313 type typ = Int | Str | Bool | Float
14- [@@ deriving compare ,enumerate,sexp]
14+ [@@ deriving bin_io , compare ,enumerate,sexp]
1515 type 'a t = {
1616 parse : string -> 'a option ;
1717 pack : 'a -> string ;
@@ -26,9 +26,9 @@ module Type = struct
2626 type header = {
2727 fname : string ;
2828 ftype : typ ;
29- } [@@ deriving compare , sexp ]
29+ } [@@ deriving bin_io , compare , sexp ]
3030
31- type signature = header list [@@ deriving compare , sexp ]
31+ type signature = header list [@@ deriving bin_io , compare , sexp ]
3232
3333 type ('f,'k) scheme = {
3434 read : entry -> 'a -> 'b option ;
@@ -169,19 +169,22 @@ let declare = Attribute.declare
169169
170170
171171module Doc = struct
172+ module Problem = Error
172173 module Error = Monad.Result. Error
173174 open Error.Syntax
174175
175176 type t = {
176177 scheme : Type .signature String.Map .t ;
177178 entries : entry list String.Map .t ;
178- } [@@ deriving compare ]
179+ } [@@ deriving bin_io , compare ]
179180
180181 let empty = {
181182 scheme = String.Map. empty;
182183 entries = String.Map. empty;
183184 }
184185
186+ let is_empty {scheme} = Map. is_empty scheme
187+
185188 let errorf fmt = Or_error. errorf fmt
186189
187190 let declarations {scheme} = Map. length scheme
@@ -395,6 +398,24 @@ module Doc = struct
395398 Sexp. scan_sexps (String. strip str |> Lexing. from_string) |>
396399 of_sexps)
397400
401+ let sexps_of_t {scheme; entries} =
402+ let attrs =
403+ Map. fold entries ~init: [] ~f: (fun ~key :n ~data :vs sexps ->
404+ List. fold vs ~init: sexps ~f: (fun sexps v ->
405+ sexp_of_attr scheme n v :: sexps)) in
406+ Map. fold scheme ~init: attrs ~f: (fun ~key :n ~data :s xs ->
407+ sexp_of_decl (n,s) :: xs)
408+
409+ let sexp_of_t doc = Sexp. List (sexps_of_t doc)
410+
411+ let t_of_sexp = function
412+ | Sexp. Atom _ -> invalid_arg " Ogre.Doc.t_of_sexp: expects a list"
413+ | List exp -> match of_sexps exp with
414+ | Ok x -> x
415+ | Error err ->
416+ invalid_argf " Ogre.Doc.t_of_sexp: ill-formed document: %s"
417+ (Problem. to_string_hum err) ()
418+
398419 let to_string x = asprintf " %a" pp x
399420end
400421
0 commit comments