-let require ~baseuri =
- let ch = open_in (path_of_baseuri baseuri) in
- let mmagic,dump = Marshal.from_channel ch in
- close_in ch;
- if mmagic <> magic then
- raise (LibraryOutOfSync (lazy "The library is out of sync with the implementation. Please recompile the library."))
- else
- dump
-;;
+module type Serializer =
+ sig
+ type status
+ type obj
+ val register: string -> ('a -> status -> status) -> ('a -> obj)
+ val serialize: baseuri:NUri.uri -> obj list -> unit
+ val require: baseuri:NUri.uri -> status -> status
+ end
+
+module Serializer(S: sig type status end) =
+ struct
+ type status = S.status
+ type obj = Obj.t
+
+ let require1 = ref (fun _ -> assert false (* unknown data*))
+ let already_registered = ref []
+
+ let register tag require =
+ assert (not (List.mem tag !already_registered));
+ already_registered := tag :: !already_registered;
+ require1 :=
+ (fun (tag',data) as x ->
+ if tag=tag' then Obj.magic require data else Obj.magic !require1 x);
+ Obj.magic (fun x -> tag,x)
+
+ let serialize = serialize
+
+ let require ~baseuri status =
+ let dump = require0 ~baseuri in
+ List.fold_right (Obj.magic !require1) dump status
+end