ctrf_rs/
extra.rs

1// other import(s)
2use serde_json::Value;
3
4pub trait Extra {
5    fn insert_extra(&mut self, key: String, value: Value) -> Option<Value>;
6    fn get_extra(&mut self, key: &str) -> Option<&Value>;
7    fn remove_extra(&mut self, key: &str) -> Option<Value>;
8}
9
10#[macro_export]
11/// Implements standard insert/get/remove methods for a struct's `Extra` map
12macro_rules! impl_extra {
13    ($($t:ty),+ $(,)?) => ($(
14        impl $crate::extra::Extra for $t {
15            /// Inserts an element into the Extra map.
16            /// Returns the value that it replaced, if one was present, or None if not.
17            fn insert_extra(&mut self, key: String, value: Value) -> Option<Value> {
18                self.extra.insert(key, value)
19            }
20
21            /// Reads the value at the provided key.
22            /// Returns the borrowed value if one was present, or None if not.
23            fn get_extra(&mut self, key: &str) -> Option<&Value> {
24                self.extra.get(key)
25            }
26
27            /// Removes the value at the provided key.
28            /// Returns the value if one was present, or None if not.
29            fn remove_extra(&mut self, key: &str) -> Option<Value> {
30                self.extra.remove(key)
31            }
32        }
33    )+)
34}