cainome_cairo_serde/lib.rs
1//! This crate contains the definition of traits and types
2//! that map to Cairo types that can then be (de)serializable from an array of `Felt`.
3//!
4//! Some of the Cairo types are provided in the ABI event if they are very generic
5//! like `Option`, `Result`, etc...
6//! This crate provides the `CairoSerde` implementation for those types and all basic
7//! types from Cairo (integers, felt etc...).
8//!
9mod error;
10pub use error::{Error, Result};
11
12pub mod call;
13pub mod serde_hex;
14pub mod types;
15
16pub use serde_hex::*;
17pub use types::array_legacy::*;
18pub use types::byte_array::*;
19pub use types::non_zero::*;
20pub use types::starknet::*;
21pub use types::u256::*;
22pub use types::*;
23
24use ::starknet::core::types::Felt;
25
26/// CairoSerde trait to implement in order to serialize/deserialize
27/// a Rust type to/from a CairoSerde.
28pub trait CairoSerde {
29 /// The corresponding Rust type.
30 type RustType;
31
32 /// The serialized size of the type in felts, if known at compile time.
33 const SERIALIZED_SIZE: Option<usize> = Some(1);
34
35 /// Whether the serialized size is dynamic.
36 const DYNAMIC: bool = Self::SERIALIZED_SIZE.is_none();
37
38 /// Calculates the serialized size of the data for a single felt
39 /// it will always be 1.
40 /// If the type is dynamic, SERIALIZED_SIZE is None, but this
41
42 /// function is overriden to correctly compute the size.
43 #[inline]
44 fn cairo_serialized_size(_rust: &Self::RustType) -> usize {
45 Self::SERIALIZED_SIZE.unwrap()
46 }
47
48 /// Serializes the given type into a Felt sequence.
49 fn cairo_serialize(rust: &Self::RustType) -> Vec<Felt>;
50
51 /// TODO: add `serialize_to(rust: &Self::RustType, out: &mut Vec<Felt>)`.
52 /// for large buffers optimization.
53
54 /// Deserializes an array of felts into the given type.
55 fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result<Self::RustType>;
56}