Version française
Home     About     Download     Resources     Contact us    
Browse thread
Escaped string in sexplib
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Paolo Donadeo <p.donadeo@g...>
Subject: Re: [Caml-list] Escaped string in sexplib
I'm using Sexplib to store and retrieve the sessions of a web
application into a data base. A "session" has this signature:

module type SESSION_DATA =
  sig
    type t
    val default : t
    val string_of_t : t -> string (* Is this what you want? *)
    val t_of_string : string -> t (* Is this what you want? *)
  end

and the concrete session, which is a blog session, is:

module Blog_session =
  struct
    type user =
      {
        email : string;
        passwd : string;
        first_name : string option;
        last_name : string option;
        displ_name : string option;
        website : string option
      } with sexp

    type t =
      | Anonymous
      | Logged of user
        with sexp

    let default = Anonymous

    let string_of_t = sexp_of_t |- Sexplib.Sexp.to_string_hum |- hex_encode

    let t_of_string s =
      try s |> hex_decode |> Sexplib.Sexp.of_string |> t_of_sexp
      with Sexplib.Conv.Of_sexp_error (_, _) -> default
  end

Functions "hex_encode" and "hex_decode" are trivially implemented
using Cryptokit:

let hex_encode = Cryptokit.transform_string (Cryptokit.Hexa.encode ())
let hex_decode = Cryptokit.transform_string (Cryptokit.Hexa.decode ())

The operators (|-) and (|>) are defined as always:

http://thelema.github.com/batteries-included/hdoc/BatPervasives.html#6_Fundamentalfunctionsandoperators


Hope this helps,


-- 
Paolo