File: common_macro.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (145 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#[allow(unused)]
macro_rules! set_insert_deser_assert_macro [

    [$set: ident, $data: ident, $($key: expr),*] => [
        $($set.insert($key));*
        ;

        let $data = borsh::to_vec(&$set).unwrap();
        #[cfg(feature = "std")]
        insta::assert_debug_snapshot!($data);
    ]
];

#[allow(unused)]
macro_rules! map_insert_deser_assert_macro [

    [$map: ident, $data: ident, $($key: expr => $value: expr),*] => [
        $($map.insert($key, $value));*
        ;

        let $data = borsh::to_vec(&$map).unwrap();
        #[cfg(feature = "std")]
        insta::assert_debug_snapshot!($data);
    ]
];

#[allow(unused)]
macro_rules! set_wrong_order_test [

    [$test_name: ident, $set_type: ty] => [

        #[test]
        fn $test_name() {
            let mut data = vec![];
            let arr_key = ["various".to_string(), "foo".to_string(), "many".to_string()];
            let len = arr_key.len() as u32;
            u32::serialize(&len, &mut data).expect("no error");

            for key in &arr_key {
                key.serialize(&mut data).expect("no error");
            }

            let result = from_slice::<$set_type>(&data);

            #[cfg(not(feature = "de_strict_order"))]
            {
                let result = result.unwrap();
                assert_eq!(result.len(), arr_key.len());
                for key in &arr_key {
                    assert!(result.contains(key));

                }

            }

            #[cfg(feature = "de_strict_order")]
            {
                assert!(result.is_err());

                assert_eq!(result.unwrap_err().to_string(), ERROR_WRONG_ORDER_OF_KEYS);
            }
        }
    ]
];

#[allow(unused)]
macro_rules! map_wrong_order_test [

    [$test_name: ident, $map_type: ty] => [

        #[test]
        fn $test_name() {
            let mut data = vec![];
            let arr_key = ["various".to_string(), "foo".to_string(), "many".to_string()];
            let arr_val = [
                "value".to_string(),
                "different".to_string(),
                "unexp".to_string(),
            ];
            let len = arr_key.len() as u32;
            u32::serialize(&len, &mut data).expect("no error");

            let entries = IntoIterator::into_iter(arr_key.clone())
                .zip(IntoIterator::into_iter(arr_val))
                .collect::<Vec<_>>();

            for (key, value) in entries.clone() {
                key.serialize(&mut data).expect("no error");
                value.serialize(&mut data).expect("no error");
            }

            let result = from_slice::<$map_type>(&data);

            #[cfg(not(feature = "de_strict_order"))]
            {
                let result = result.unwrap();
                assert_eq!(result.len(), arr_key.len());
                for (key, value) in entries {
                    assert_eq!(result.get(&key), Some(&value));
                }
            }

            #[cfg(feature = "de_strict_order")]
            {
                assert!(result.is_err());

                assert_eq!(result.unwrap_err().to_string(), ERROR_WRONG_ORDER_OF_KEYS);
            }
        }
    ]
];

#[allow(unused)]
macro_rules! schema_map(
    () => { BTreeMap::new() };
    { $($key:expr => $value:expr),+ } => {
        {
            let mut m = BTreeMap::new();
            $(
                m.insert($key.to_string(), $value);
            )+
            m
        }
     };
);

#[allow(unused)]
#[cfg(feature = "unstable__schema")]
pub mod schema_imports {
    extern crate alloc;
    pub use alloc::{
        boxed::Box,
        collections::BTreeMap,
        format,
        string::{String, ToString},
        vec,
        vec::Vec,
    };

    pub use borsh::schema::{
        add_definition, BorshSchemaContainer, Declaration, Definition, Fields,
        SchemaContainerValidateError, SchemaMaxSerializedSizeError,
    };
    pub use borsh::{schema_container_of, BorshSchema};
}