bon_macros/parsing/
spanned_key.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::util::prelude::*;
use darling::FromMeta;
use std::fmt;
use std::ops::Deref;

/// A type that stores the attribute key path information along with the parsed value.
/// It is useful for error reporting. For example, if some key was unexpected, it's
/// possible to point to the key's span in the error instead of the attribute's value.
#[derive(Clone)]
pub(crate) struct SpannedKey<T> {
    pub(crate) key: syn::Ident,
    pub(crate) value: T,
}

impl<T> SpannedKey<T> {
    pub(crate) fn new(path: &syn::Path, value: T) -> Result<Self> {
        Ok(Self {
            key: path.require_ident()?.clone(),
            value,
        })
    }

    pub(crate) fn into_value(self) -> T {
        self.value
    }

    pub(crate) fn key(&self) -> &syn::Ident {
        &self.key
    }
}

impl<T: FromMeta> FromMeta for SpannedKey<T> {
    fn from_meta(meta: &syn::Meta) -> Result<Self> {
        let value = T::from_meta(meta)?;
        Self::new(meta.path(), value)
    }
}

impl<T: fmt::Debug> fmt::Debug for SpannedKey<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.value, f)
    }
}

impl<T> Deref for SpannedKey<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &self.value
    }
}