package tezos-protocol-environment
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=7062cd57addd452852598a2214ade393130efa087b99068d53713bdf912b3680
sha512=08e4091144a03ce3c107fb91a66501bd8b65ca3278917c455a2eaac6df3e108ade63f6ab8340a4bb152d60f404326e464d0ec95d26cafe8e82f870465d24a5fc
doc/tezos-protocol-environment.structs/Tezos_protocol_environment_structs/V9/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