package spin

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Spin.DecoderSource

Sourcetype error =
  1. | Decoder_error of Spin_std.string * Spin_std.Sexp.t Spin_std.option
  2. | Decoder_errors of error Spin_std.list
  3. | Decoder_tag of Spin_std.string * error
Sourcetype 'a t = Spin_std.Sexp.t -> ('a, error) Spin_std.Result.t

Error handling

Sourceval pp_error : Spin_std.Formatter.t -> error -> Spin_std.unit

Pretty-print an error.

Sourceval string_of_error : error -> Spin_std.string

Convert an error to a string.

Loading s-expressions

Sourceval of_string : Spin_std.string -> (Spin_std.Sexp.t, error) Spin_std.Result.t

Load an s-expression from a string.

Sourceval of_sexps_string : Spin_std.string -> (Spin_std.Sexp.t, error) Spin_std.Result.t

Load a list of s-expression from a string.

Sourceval of_file : Spin_std.string -> (Spin_std.Sexp.t, error) Spin_std.Result.t

Load an s-expression from a file.

Sourceval of_sexps_file : Spin_std.string -> (Spin_std.Sexp.t, error) Spin_std.Result.t

Load a list of s-expression from a file.

Decoding primitives

Decode a string.

Decode an int.

Decode a float.

Decode a bool.

Decode a null.

Helpers

Sourceval string_matching : regex:Spin_std.string -> err:Spin_std.string -> Spin_std.string t

Decoding lists

Sourceval list : 'a t -> 'a Spin_std.list t

Decode a collection into an OCaml list.

Decoding records

Sourceval field : f:'a t -> Spin_std.string -> 'a t

Decode a record from the field with the given name.

This will fail with an error if the field could not be found. It will also fail if several fields exist with the same name. Use fields if you want to decode a list of fields.

Sourceval fields : f:'a t -> Spin_std.string -> 'a Spin_std.list t

Decode a list of record from the fields with the given name.

It returns an empty list if no fields with the given name exist.

Sourceval field_opt : f:'a t -> Spin_std.string -> 'a Spin_std.option t

Decode a record from the field with the given name.

This will return None if the field could not be found. It will fail if several fields exist with the same name. Use fields if you want to decode a list of fields.

Inconsistent structure

Sourceval one_of_opt : (Spin_std.string * 'a t) Spin_std.list -> 'a Spin_std.option t

Try a sequence of different decoders.

Sourceval one_of : (Spin_std.string * 'a t) Spin_std.list -> 'a t

Try a sequence of different decoders and return an error if none of them worked.

Monadic operations

Sourceval return : 'a -> 'a t

Lift decoder from a value.

Sourceval map : f:('a -> 'b) -> 'a t -> 'b t

Map over the output of a decoder.

Sourceval bind : f:('a -> 'b t) -> 'a t -> 'b t

Create decoders that depend on previous outputs.

Sourceval product : 'a t -> 'b t -> ('a * 'b) t

Try two decoders and then combine the Result.t. We can use this to decode objects with many fields

Sourcemodule Infix : sig ... end
include module type of Infix
Sourceval (>|=) : 'a t -> ('a -> 'b) -> 'b t
Sourceval (>>=) : 'a t -> ('a -> 'b t) -> 'b t
Sourceval (<*>) : ('a -> 'b) t -> 'a t -> 'b t
Sourcemodule Let_syntax : sig ... end
include module type of Let_syntax
Sourceval (let*) : 'a t -> ('a -> 'b t) -> 'b t
Sourceval (let+) : 'a t -> ('a -> 'b) -> 'b t
Sourceval (and+) : 'a t -> 'b t -> ('a * 'b) t

Running decoders

Sourceval decode_sexp : f:'a t -> Spin_std.Sexp.t -> ('a, error) Spin_std.Result.t

Run a decoder on some input.

Sourceval decode_string : f:'a t -> Spin_std.string -> ('a, error) Spin_std.Result.t

Run a decoder on a string.

Sourceval decode_sexps_string : f:'a t -> Spin_std.string -> ('a, error) Spin_std.Result.t

Run a decoder on a string containing a list of s-expression.

Sourceval decode_file : f:'a t -> Spin_std.string -> ('a, error) Spin_std.Result.t

Run a decoder on a file.

Sourceval decode_sexps_file : f:'a t -> Spin_std.string -> ('a, error) Spin_std.Result.t

Run a decoder on a file containing a list of s-expression.