tezos_data_encoding/
lib.rs

1// Copyright (c) SimpleStaking, Viable Systems and Tezedge Contributors
2// SPDX-License-Identifier: MIT
3#![forbid(unsafe_code)]
4#![cfg_attr(feature = "fuzzing", feature(no_coverage))]
5
6//! This crate provides serialization and deserialization functionality for the data types used by the Tezos shell.
7//!
8//! You can either implement [`NomReader`] and [`BinWriter`] manually, or derive them.
9//!
10//! # Examples
11//!
12//! Let's create encodings for a struct containing two arrays - one of fixed size, and one dynamic.
13//!
14//! Derivation is supported across generic structs.
15//!
16//! ```rust
17//! use tezos_data_encoding::nom::NomReader;
18//! use tezos_data_encoding::enc::BinWriter;
19//! use tezos_data_encoding::encoding::HasEncoding;
20//! # use core::fmt::Debug;
21//!
22//! const INNER_SIZE: usize = 10;
23//!
24//! #[derive(Debug, PartialEq, HasEncoding, NomReader, BinWriter)]
25//! struct Inner {
26//!   #[encoding(sized = "INNER_SIZE", bytes)]
27//!   fixed_size: Vec<u8>
28//! }
29//!
30//! #[derive(Debug, PartialEq, HasEncoding, NomReader, BinWriter)]
31//! struct Outer<T>
32//! where T: Debug + PartialEq + HasEncoding + for<'a> NomReader<'a> + BinWriter {
33//!   #[encoding(dynamic)]
34//!   dynamic_size: Vec<T>
35//! }
36//! #
37//! # let inner_1 = Inner { fixed_size: vec![1; INNER_SIZE] };
38//! # let inner_2 = Inner { fixed_size: vec![2; INNER_SIZE] };
39//! #
40//! # let outer = Outer { dynamic_size: vec![inner_1, inner_2] };
41//! #
42//! # let mut encoded = Vec::new();
43//! # outer.bin_write(&mut encoded).expect("encoding works");
44//! #
45//! # let (_remaining_input, result) = Outer::<Inner>::nom_read(&encoded)
46//! #     .expect("decoding works");
47//! #
48//! # assert!(_remaining_input.is_empty());
49//! # assert_eq!(outer, result);
50//! ```
51
52mod bit_utils;
53pub mod types;
54
55pub mod binary_reader;
56pub mod binary_writer;
57
58pub mod enc;
59pub mod encoding;
60pub mod nom;
61
62#[cfg(feature = "fuzzing")]
63pub mod fuzzing;