update of Bij.version

This commit is contained in:
Simon Cruanes 2013-05-15 17:59:19 +02:00
parent a893e6c0eb
commit 5c11f945b9
2 changed files with 18 additions and 15 deletions

26
bij.ml
View file

@ -67,17 +67,6 @@ let guard f t = Guard (f, t)
let map ~inject ~extract b = Map (inject, extract, b) let map ~inject ~extract b = Map (inject, extract, b)
let switch ~inject ~extract = Switch (inject, extract) let switch ~inject ~extract = Switch (inject, extract)
(** {2 Helpers} *)
let fix f =
let rec bij = lazy (f (fun () -> Lazy.force bij)) in
Lazy.force bij
type 'a versioned = string * 'a
let with_version v t =
pair (guard (fun v' -> v = v') string_) t
(** {2 Exceptions} *) (** {2 Exceptions} *)
exception EOF exception EOF
@ -88,6 +77,21 @@ exception EncodingError of string
exception DecodingError of string exception DecodingError of string
(** Raised when decoding is impossible *) (** Raised when decoding is impossible *)
(** {2 Helpers} *)
let fix f =
let rec bij = lazy (f (fun () -> Lazy.force bij)) in
Lazy.force bij
let with_version v t =
map
~inject:(fun x -> v, x)
~extract:(fun (v', x) ->
if v = v'
then x
else raise (DecodingError ("expected version " ^ v)))
(pair string_ t)
(** {2 Source of parsing} *) (** {2 Source of parsing} *)
module type SOURCE = sig module type SOURCE = sig

View file

@ -64,10 +64,9 @@ val switch : inject:('a -> char * 'a inject_branch) ->
val fix : ((unit -> 'a t) -> 'a t) -> 'a t val fix : ((unit -> 'a t) -> 'a t) -> 'a t
(** Helper for recursive encodings *) (** Helper for recursive encodings *)
type 'a versioned = string * 'a val with_version : string -> 'a t -> 'a t
(** Guards the values with a given version. Only values encoded with
val with_version : string -> 'a t -> 'a versioned t the same version will fit. *)
(** Guards the values with a given version *)
(** {2 Exceptions} *) (** {2 Exceptions} *)