From 5c11f945b9c02ca77c5ee6959e5e721fece59de5 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 15 May 2013 17:59:19 +0200 Subject: [PATCH] update of Bij.version --- bij.ml | 26 +++++++++++++++----------- bij.mli | 7 +++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/bij.ml b/bij.ml index d8038505..e9e5da5a 100644 --- a/bij.ml +++ b/bij.ml @@ -67,17 +67,6 @@ let guard f t = Guard (f, t) let map ~inject ~extract b = Map (inject, extract, b) 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} *) exception EOF @@ -88,6 +77,21 @@ exception EncodingError of string exception DecodingError of string (** 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} *) module type SOURCE = sig diff --git a/bij.mli b/bij.mli index 14dc0525..41d8217a 100644 --- a/bij.mli +++ b/bij.mli @@ -64,10 +64,9 @@ val switch : inject:('a -> char * 'a inject_branch) -> val fix : ((unit -> 'a t) -> 'a t) -> 'a t (** Helper for recursive encodings *) -type 'a versioned = string * 'a - -val with_version : string -> 'a t -> 'a versioned t - (** Guards the values with a given version *) +val with_version : string -> 'a t -> 'a t + (** Guards the values with a given version. Only values encoded with + the same version will fit. *) (** {2 Exceptions} *)