mapstic_core/
param.rs

1use indexmap::IndexMap;
2use serde::Serialize;
3
4/// Collection of field mapping parameters.
5#[derive(Debug, Clone, Default, Serialize)]
6pub struct Params {
7    #[serde(flatten)]
8    options: IndexMap<&'static str, Value>,
9}
10
11impl Params {
12    /// Extends the options from another option collection.
13    pub fn extend(&mut self, other: Self) {
14        self.options.extend(other.options)
15    }
16
17    /// Inserts an option into the collection.
18    pub fn insert(&mut self, name: &'static str, value: Value) -> Option<Value> {
19        self.options.insert(name, value)
20    }
21
22    /// Returns an iterator over the collection.
23    pub fn iter(&self) -> impl Iterator<Item = (&'static str, &Value)> {
24        self.options.iter().map(|(name, value)| (*name, value))
25    }
26}
27
28impl IntoIterator for Params {
29    type Item = (&'static str, Value);
30    type IntoIter = <IndexMap<&'static str, Value> as IntoIterator>::IntoIter;
31
32    fn into_iter(self) -> Self::IntoIter {
33        self.options.into_iter()
34    }
35}
36
37impl FromIterator<(&'static str, Value)> for Params {
38    fn from_iter<T>(iter: T) -> Self
39    where
40        T: IntoIterator<Item = (&'static str, Value)>,
41    {
42        Self {
43            options: iter.into_iter().collect(),
44        }
45    }
46}
47
48/// A value for a specific field mapping option.
49#[derive(Debug, Clone, Serialize)]
50#[serde(untagged)]
51pub enum Value {
52    Bool(bool),
53    Int(i64),
54    Uint(u64),
55    Float(f64),
56
57    // It's convenient to reuse this type both in the proc macro and the user facing API, but
58    // there's an implementation difference: the proc macro needs to be able to deal with owned
59    // strings, since we're building this from `Ident` instances, but the user API gets generated
60    // with string literals so we don't need to bother instantiating `String`s at runtime. We'll
61    // define slightly different variants to handle each case, hidden behind a feature.
62    //
63    // First up, the public API.
64    String(&'static str),
65    Nested(IndexMap<&'static str, Value>),
66
67    // Now the proc macro specific versions, only defined when the `proc-macro` feature is enabled.
68    #[cfg(feature = "proc-macro")]
69    OwnedString(String),
70    #[cfg(feature = "proc-macro")]
71    OwnedNested(IndexMap<String, Value>),
72}