bon_macros/parsing/
spanned_key.rs

1use crate::util::prelude::*;
2use darling::FromMeta;
3use std::fmt;
4use std::ops::Deref;
5
6/// A type that stores the attribute key path information along with the parsed value.
7/// It is useful for error reporting. For example, if some key was unexpected, it's
8/// possible to point to the key's span in the error instead of the attribute's value.
9#[derive(Clone)]
10pub(crate) struct SpannedKey<T> {
11    pub(crate) key: syn::Ident,
12    pub(crate) value: T,
13}
14
15impl<T> SpannedKey<T> {
16    pub(crate) fn new(path: &syn::Path, value: T) -> Result<Self> {
17        Ok(Self {
18            key: path.require_ident()?.clone(),
19            value,
20        })
21    }
22
23    pub(crate) fn into_value(self) -> T {
24        self.value
25    }
26
27    pub(crate) fn key(&self) -> &syn::Ident {
28        &self.key
29    }
30
31    pub(crate) fn with_value<U>(self, value: U) -> SpannedKey<U> {
32        SpannedKey {
33            value,
34            key: self.key,
35        }
36    }
37
38    pub(crate) fn map_value<U>(self, map: impl FnOnce(T) -> U) -> SpannedKey<U> {
39        SpannedKey {
40            value: map(self.value),
41            key: self.key,
42        }
43    }
44}
45
46impl<T: FromMeta> FromMeta for SpannedKey<T> {
47    fn from_meta(meta: &syn::Meta) -> Result<Self> {
48        let value = T::from_meta(meta)?;
49        Self::new(meta.path(), value)
50    }
51}
52
53impl<T: fmt::Debug> fmt::Debug for SpannedKey<T> {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        fmt::Debug::fmt(&self.value, f)
56    }
57}
58
59impl<T> Deref for SpannedKey<T> {
60    type Target = T;
61
62    fn deref(&self) -> &T {
63        &self.value
64    }
65}