workflow_serializer/
macros.rs

1//!
2//! Macros for loading and storing items using Borsh and Serializer.
3//!
4
5/// Create Payload struct - a `#repr[transparent]` struct
6/// wrapping `Cursor<Vec<u8>>`. This struct acts as a helper
7/// for storing and loading items into a `Vec<u8>` buffer.
8#[macro_export]
9macro_rules! payload {
10    () => {{
11        $crate::payload::Payload::default()
12    }};
13    ($value:expr) => {{
14        $crate::payload::Payload::with_capacity($value)
15    }};
16}
17
18#[macro_export]
19macro_rules! version {
20    ($major:expr, $minor:expr) => {{
21        $crate::payload::Version::new($major, $minor)
22    }};
23}
24
25#[macro_export]
26macro_rules! writer {
27    ($value:expr) => {{
28        ($value.as_mut() as &mut std::io::Cursor<Vec<u8>>)
29    }};
30}
31
32#[macro_export]
33macro_rules! reader {
34    ($value:expr) => {{
35        &mut std::io::Cursor::new($value.into_inner())
36    }};
37}
38
39/// Store item using Borsh serialization
40#[macro_export]
41macro_rules! store {
42    ($type:ty, $value:expr, $writer:expr) => {
43        <$type as borsh::BorshSerialize>::serialize($value, $writer)
44    };
45}
46
47/// Load item using Borsh deserialization
48#[macro_export]
49macro_rules! load {
50    ($type:ty, $reader:expr) => {
51        <$type as borsh::BorshDeserialize>::deserialize_reader($reader)
52    };
53}
54
55/// Store item using Serializer serialization. [`crate::serializer::Serializer`] is meant to provide
56/// custom serialization over Borsh that can be used to store additional
57/// metadata such as struct version.
58#[macro_export]
59macro_rules! serialize {
60    ($type:ty, $value:expr, $writer:expr) => {
61        $crate::payload::ser::Payload::<$type>($value).serialize($writer)
62    };
63}
64
65/// Load item using Serializer deserialization. [`crate::serializer::Serializer`] is meant to provide
66/// custom serialization over Borsh that can be used to store additional
67/// metadata such as struct version.
68#[macro_export]
69macro_rules! deserialize {
70    ($type:ty, $reader:expr) => {
71        $crate::payload::de::Payload::<$type>::deserialize($reader).map(|x| x.into_inner())
72    };
73}