package tezos-protocol-environment
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=ad9e08819871c75ba6f4530b125f7d157799398e4d77a1e6bfea9d91ff37ff55
sha512=c5dc4d40cc09bc6980fbbdb5c2e105bf4252cf9cfcb2b49660b0ebe4dc789f6709ec3b3bf2f87d81580d3eed9521eeb1c960f24d9b14eb0285aaba1f84d10a9b
doc/tezos-protocol-environment.structs/Tezos_protocol_environment_structs/V8/Data_encoding/Encoding/Fixed/index.html
Module Encoding.Fixed
Source
include module type of struct include Data_encoding.Encoding.Fixed end
val string' :
Data_encoding.Encoding.string_json_repr ->
int ->
string Data_encoding.Encoding.encoding
val bytes' :
Data_encoding.Encoding.string_json_repr ->
int ->
Bytes.t Data_encoding.Encoding.encoding
val add_padding :
'a Data_encoding.Encoding.encoding ->
int ->
'a Data_encoding.Encoding.encoding
add_padding e n
is a padded version of the encoding e
. In Binary, there are n
null bytes (\000
) added after the value encoded by e
. In JSON, padding is ignored.
val list :
int ->
'a Data_encoding.Encoding.encoding ->
'a list Data_encoding.Encoding.encoding
list n e
is an encoding for lists of exactly n
elements. If a list of more or fewer elements is provided, then the encoding fails with the write_error List_invalid_length
. For decoding, it can fail with read_error Not_enough_data
or read_error Extra_bytes
, or it may cause other failures further down the line when the AST traversal becomes out-of-sync with the underlying byte-stream traversal.
The difference of the errors being used when encoding and decoding is because when encoding we have access to the list and we can check the actual length, whereas when decoding we only see bytes, sometimes too many, sometimes not enough.
This encoding has a narrow set of possible applications because it is very restrictive. Still, it can to:
- mirror static guarantees about the length of some lists,
- special-case some common lengths of typical input in a union (see example below),
- other ends.
type expr =
| Op of string * expr list (* most commonly 1 or 2 operands *)
| Literal of string
let expr_encoding =
mu "expr" (fun e ->
union [
case ~title:"op-nonary" (Tag 0)
string
(function Op (op, []) -> Some op | _ -> None)
(fun op -> Op (op, []));
case ~title:"op-unary" (Tag 1)
(tup2 string (Fixed.list 1 e))
(function Op (op, ([_]) as operand) -> Some (op, operand) | _ -> None)
(fun (op, operand) -> Op (op, operand));
case ~title:"op-binary" (Tag 2)
(tup2 string (Fixed.list 2 e))
(function Op (op, ([_;_]) as operand) -> Some (op, operand) | _ -> None)
(fun (op, operand) -> Op (op, operand));
case ~title:"op-moreary" (Tag 3)
(tup2 string (list e))
(function Op (op, operand) -> Some (op, operand) | _ -> None)
(fun (op, operand) -> Op (op, operand));
case ~title:"literal" (Tag 4)
string
(function Literal l -> Some l | _ -> None)
(fun l -> Literal l);
]
)
Interestingly, the cases for known lengths can be generated programmatically.
val array :
int ->
'a Data_encoding.Encoding.encoding ->
'a array Data_encoding.Encoding.encoding
See list
above.
val string :
Data_encoding.Encoding.string_json_repr ->
int ->
string Data_encoding.Encoding.encoding
val bytes :
Data_encoding.Encoding.string_json_repr ->
int ->
Bytes.t Data_encoding.Encoding.encoding