Module Data_encoding.Binary

type read_error =
| Not_enough_data
| Extra_bytes
| No_case_matched
| Unexpected_tag of int
| Invalid_size of int
| Invalid_int of {
min : int;
v : int;
max : int;
}
| Invalid_float of {
min : float;
v : float;
max : float;
}
| Trailing_zero
| Size_limit_exceeded
| List_too_long
| 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 =
| Size_limit_exceeded
| No_case_matched
| Invalid_int of {
min : int;
v : int;
max : int;
}
| Invalid_float of {
min : float;
v : float;
max : float;
}
| Invalid_bytes_length of {
expected : int;
found : int;
}
| Invalid_string_length of {
expected : int;
found : int;
}
| Invalid_natural
| List_too_long
| 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 this size does not depends of the value itself.

E.g., fixed_length (tup2 int64 (Fixed.string 2)) is Some _

E.g., fixed_length (result int64 (Fixed.string 2)) is None

E.g., fixed_length (list (tup2 int64 (Fixed.string 2))) is None

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

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

E.g., maximum_length (tup2 int64 (Fixed.string 2)) is Some _

E.g., maximum_length (result int64 (Fixed.string 2)) is Some _

E.g., maximum_length (list (tup2 int64 (Fixed.string 2))) is None

Note that the function assumes that recursive encodings (build using mu) are used for recursive data types. As a result, maximum_length will return None if given a recursive encoding.

If there are static guarantees about the maximum size of the representation for values of a given type, you can wrap your encoding in check_size. This will cause maximum_length to return Some _.

val read : 'a Encoding.t -> string -> int -> int -> (int * 'aread_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.

The function will fail (returning Error _) if it needs to read more than len bytes to decode the value.

The returned value contains no pointer back to buf (as a whole or as sub-strings), even in the case where the encoding is or includes Fixed.string or Fixed.bytes.

val read_opt : 'a Encoding.t -> string -> int -> int -> (int * 'a) option
val read_exn : 'a Encoding.t -> string -> int -> int -> int * 'a
type 'ret status =
| Success of {
result : 'ret;
size : int;
stream : Data_encoding__.Binary_stream.t;
}

Fully decoded value, together with the total amount of bytes reads, and the remaining unread stream.

| Await of Stdlib.Bytes.t -> 'ret status

Partially decoded value.

| 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.

type writer_state

The internal state that writers handle. It is presented explicitly as an abstract type so that you must use the constructor to obtain it. The constructor (make_writer_state) performs basic bound checks.

val make_writer_state : bytes -> offset:int -> allowed_bytes:int -> writer_state option

make_writer_state buffer ~offset ~allowed_bytes is None if allowed_bytes < 0, None if allowed_bytes > length buffer - offset, Some _ otherwise.

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

write enc v state where Some state is make_writer_state buffer ~offset ~allowed_bytes writes the binary enc representation of v onto buffer starting at offset. The function will fail (returning Error _) if the encoding would use more than allowed_bytes.

The function returns the offset of first unwritten bytes, or returns Error _ in case of failure. In the latter case, some data might have been written on the buffer.

val write_opt : 'a Encoding.t -> 'a -> writer_state -> int option
val write_exn : 'a Encoding.t -> 'a -> writer_state -> int
val of_bytes : 'a Encoding.t -> Stdlib.Bytes.t -> ('aread_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 of_string : 'a Encoding.t -> string -> ('aread_error) Stdlib.result

of_string enc buf is like of_bytes enc buf but it reads bytes from a string.

val of_string_opt : 'a Encoding.t -> string -> 'a option
val of_string_exn : 'a Encoding.t -> string -> 'a
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. The parameter buffer_size controls the initial size of buf.

raises [Invalid_argument]

if buffer_size < 0.

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 to_string : ?⁠buffer_size:int -> 'a Encoding.t -> 'a -> (string, write_error) Stdlib.result

to_string enc v is like to_bytes but it returns a string.

Note: to_string enc v is always equal to Bytes.to_string (to_bytes enc v) but more efficient.

raises [Invalid_argument]

if buffer_size < 0.

val to_string_opt : ?⁠buffer_size:int -> 'a Encoding.t -> 'a -> string option
val to_string_exn : ?⁠buffer_size:int -> 'a Encoding.t -> 'a -> string
raises [Write_error]

instead of returning None in case of error.

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