bon_macros/normalization/
syntax_variant.rs

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
/// Struct, that contains both the original syntax (unprocessed) and the normalized
/// version. This is useful for code that needs access to both versions of the syntax.
#[derive(Debug)]
pub(crate) struct SyntaxVariant<T> {
    /// Original syntax that was passed to the macro without any modifications.
    pub(crate) orig: T,

    /// The value that is equivalent to `orig`, but it underwent normalization.
    pub(crate) norm: T,
}

impl<T> SyntaxVariant<T> {
    pub(crate) fn apply_ref<'a, U>(&'a self, f: impl Fn(&'a T) -> U) -> SyntaxVariant<U> {
        let orig = f(&self.orig);
        let norm = f(&self.norm);
        SyntaxVariant { orig, norm }
    }

    pub(crate) fn into_iter(self) -> impl Iterator<Item = SyntaxVariant<T::Item>>
    where
        T: IntoIterator,
    {
        self.orig
            .into_iter()
            .zip(self.norm)
            .map(|(orig, norm)| SyntaxVariant { orig, norm })
    }
}