package data-encoding

  1. Overview
  2. Docs
type read_error =
  1. | Not_enough_data
  2. | Extra_bytes
  3. | No_case_matched
  4. | Unexpected_tag of int
  5. | Invalid_size of int
  6. | Invalid_int of {
    1. min : int;
    2. v : int;
    3. max : int;
    }
  7. | Invalid_float of {
    1. min : float;
    2. v : float;
    3. max : float;
    }
  8. | Trailing_zero
  9. | Size_limit_exceeded
  10. | List_too_long
  11. | Array_too_long

All the errors that might be returned while reading a binary value

exception Read_error of read_error
val pp_read_error : Stdlib.Format.formatter -> read_error -> unit
val read_error_encoding : read_error t
type write_error =
  1. | Size_limit_exceeded
  2. | No_case_matched
  3. | Invalid_int of {
    1. min : int;
    2. v : int;
    3. max : int;
    }
  4. | Invalid_float of {
    1. min : float;
    2. v : float;
    3. max : float;
    }
  5. | Invalid_bytes_length of {
    1. expected : int;
    2. found : int;
    }
  6. | Invalid_string_length of {
    1. expected : int;
    2. found : int;
    }
  7. | Invalid_natural
  8. | List_too_long
  9. | Array_too_long

All the errors that might be returned while writing a binary value

val pp_write_error : Stdlib.Format.formatter -> write_error -> unit
val write_error_encoding : write_error t
exception Write_error of write_error
val length : 'a Encoding.t -> 'a -> int

Compute the expected length of the binary representation of a value

val fixed_length : 'a Encoding.t -> int option

Returns the size of the binary representation that the given encoding might produce, only when the size of the representation does not depends of the value itself.

val fixed_length_exn : 'a Encoding.t -> int
val read : 'a Encoding.t -> Stdlib.Bytes.t -> int -> int -> (int * 'a, read_error) Stdlib.result

read enc buf ofs len tries to reconstruct a value from the bytes in buf starting at offset ofs and reading at most len bytes. This function also returns the offset of the first unread bytes in the buf.

val read_opt : 'a Encoding.t -> Stdlib.Bytes.t -> int -> int -> (int * 'a) option
val read_exn : 'a Encoding.t -> Stdlib.Bytes.t -> int -> int -> int * 'a
type 'ret status =
  1. | Await of Stdlib.Bytes.t -> 'ret status
    (*

    Partially decoded value.

    *)
  2. | Error of read_error
    (*

    Failure. The stream is garbled and it should be dropped.

    *)

Return type for the function read_stream.

val read_stream : ?init:Data_encoding__.Binary_stream.t -> 'a Encoding.t -> 'a status

Streamed equivalent of read. This variant cannot be called on variable-size encodings.

val write : 'a Encoding.t -> 'a -> Stdlib.Bytes.t -> int -> int -> (int, write_error) Stdlib.result

write enc v buf ofs len writes the binary representation of v as described by to enc, in buf starting at the offset ofs and writing at most len bytes. The function returns the offset of first unwritten bytes, or returns None in case of failure. In the latter case, some data might have been written on the buffer.

val write_opt : 'a Encoding.t -> 'a -> Stdlib.Bytes.t -> int -> int -> int option
val write_exn : 'a Encoding.t -> 'a -> Stdlib.Bytes.t -> int -> int -> int
val of_bytes : 'a Encoding.t -> Stdlib.Bytes.t -> ('a, read_error) Stdlib.result

of_bytes enc buf is equivalent to read enc buf 0 (length buf). The function fails if the buffer is not fully read.

val of_bytes_opt : 'a Encoding.t -> Stdlib.Bytes.t -> 'a option
val of_bytes_exn : 'a Encoding.t -> Stdlib.Bytes.t -> 'a

of_bytes_exn enc buf is equivalent to of_bytes, except

  • raises [Read_error]

    instead of returning None in case of error.

val to_bytes : ?buffer_size:int -> 'a Encoding.t -> 'a -> (Stdlib.Bytes.t, write_error) Stdlib.result

to_bytes enc v is the equivalent of write env buf 0 len where buf is a newly allocated buffer of the expected length len (see length env v). The parameter buffer_size controls the initial size of buf.

val to_bytes_opt : ?buffer_size:int -> 'a Encoding.t -> 'a -> Stdlib.Bytes.t option
val to_bytes_exn : ?buffer_size:int -> 'a Encoding.t -> 'a -> Stdlib.Bytes.t

to_bytes_exn enc v is equivalent to to_bytes enc v, except

  • raises [Write_error]

    instead of returning None in case of error.

val describe : 'a Encoding.t -> Binary_schema.t