(* Copyright (c) 2013, Simon Cruanes All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *) (** {1 Bijective Serializer/Deserializer} *) type 'a t (** {2 Bijection description} *) val unit_ : unit t val string_ : string t val int_ : int t val bool_ : bool t val float_ : float t val list_ : 'a t -> 'a list t val many : 'a t -> 'a list t (* non empty *) val opt : 'a t -> 'a option t val pair : 'a t -> 'b t -> ('a * 'b) t val triple : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t val map : inject:('a -> 'b) -> extract:('b -> 'a) -> 'b t -> 'a t val switch : ('a -> char) -> (char * 'a t) list -> 'a t (** discriminates based on the next character. The selection function, with type ['a -> char], is used to select a bijection depending on the value. ' ' means "default" *) exception EncodingError of string (** Raised when decoding is impossible *) exception DecodingError of string (** Raised when decoding is impossible *) (** {2 Source of parsing} *) module Source : sig type t = string -> int (* fills the buffer *) val of_str : string -> t val of_stream : char Stream.t -> t val of_chan : in_channel -> t end (** {2 Sink: Where to print} *) module Sink : sig type t = { mutable write : string -> unit; mutable write_int : int -> unit; mutable write_bool : bool -> unit; mutable write_float : float -> unit; } val of_buf : Buffer.t -> t val of_chan : out_channel -> t end (** {2 Encoding/decoding} *) module Sexp : sig val encode : bij:'a t -> Sink.t -> 'a -> unit val to_string : bij:'a t -> 'a -> string val decode : bij:'a t -> Source.t -> 'a val of_string : bij:'a t -> string -> 'a end