1 | use proc_macro2::Span; |
2 | use std::ops::{Deref, DerefMut}; |
3 | use syn::spanned::Spanned; |
4 | |
5 | use crate::{ |
6 | FromDeriveInput, FromField, FromGenericParam, FromGenerics, FromMeta, FromTypeParam, |
7 | FromVariant, Result, |
8 | }; |
9 | |
10 | /// A value and an associated position in source code. The main use case for this is |
11 | /// to preserve position information to emit warnings from proc macros. You can use |
12 | /// a `SpannedValue<T>` as a field in any struct that implements or derives any of |
13 | /// `darling`'s core traits. |
14 | /// |
15 | /// To access the underlying value, use the struct's `Deref` implementation. |
16 | /// |
17 | /// # Defaulting |
18 | /// This type is meant to be used in conjunction with attribute-extracted options, |
19 | /// but the user may not always explicitly set those options in their source code. |
20 | /// In this case, using `Default::default()` will create an instance which points |
21 | /// to `Span::call_site()`. |
22 | #[derive (Debug, Clone, Copy)] |
23 | pub struct SpannedValue<T> { |
24 | value: T, |
25 | span: Span, |
26 | } |
27 | |
28 | impl<T> SpannedValue<T> { |
29 | pub fn new(value: T, span: Span) -> Self { |
30 | SpannedValue { value, span } |
31 | } |
32 | |
33 | /// Get the source code location referenced by this struct. |
34 | pub fn span(&self) -> Span { |
35 | self.span |
36 | } |
37 | |
38 | /// Apply a mapping function to a reference to the spanned value. |
39 | pub fn map_ref<U>(&self, map_fn: impl FnOnce(&T) -> U) -> SpannedValue<U> { |
40 | SpannedValue::new(value:map_fn(&self.value), self.span) |
41 | } |
42 | } |
43 | |
44 | impl<T: Default> Default for SpannedValue<T> { |
45 | fn default() -> Self { |
46 | SpannedValue::new(value:Default::default(), Span::call_site()) |
47 | } |
48 | } |
49 | |
50 | impl<T> Deref for SpannedValue<T> { |
51 | type Target = T; |
52 | |
53 | fn deref(&self) -> &T { |
54 | &self.value |
55 | } |
56 | } |
57 | |
58 | impl<T> DerefMut for SpannedValue<T> { |
59 | fn deref_mut(&mut self) -> &mut T { |
60 | &mut self.value |
61 | } |
62 | } |
63 | |
64 | impl<T> AsRef<T> for SpannedValue<T> { |
65 | fn as_ref(&self) -> &T { |
66 | &self.value |
67 | } |
68 | } |
69 | |
70 | impl<T> Spanned for SpannedValue<T> { |
71 | fn span(&self) -> Span { |
72 | self.span |
73 | } |
74 | } |
75 | |
76 | macro_rules! spanned { |
77 | ($trayt:ident, $method:ident, $syn:path) => { |
78 | impl<T: $trayt> $trayt for SpannedValue<T> { |
79 | fn $method(value: &$syn) -> Result<Self> { |
80 | Ok(SpannedValue::new( |
81 | $trayt::$method(value).map_err(|e| e.with_span(value))?, |
82 | value.span(), |
83 | )) |
84 | } |
85 | } |
86 | }; |
87 | } |
88 | |
89 | impl<T: FromMeta> FromMeta for SpannedValue<T> { |
90 | fn from_meta(item: &syn::Meta) -> Result<Self> { |
91 | let value: T = T::from_meta(item).map_err(|e: Error| e.with_span(node:item))?; |
92 | let span: Span = match item { |
93 | // Example: `#[darling(skip)]` as SpannedValue<bool> |
94 | // should have the span pointing to the word `skip`. |
95 | syn::Meta::Path(path: &Path) => path.span(), |
96 | // Example: `#[darling(attributes(Value))]` as a SpannedValue<Vec<String>> |
97 | // should have the span pointing to the list contents. |
98 | syn::Meta::List(list: &MetaList) => list.nested.span(), |
99 | // Example: `#[darling(skip = true)]` as SpannedValue<bool> |
100 | // should have the span pointing to the word `true`. |
101 | syn::Meta::NameValue(nv: &MetaNameValue) => nv.lit.span(), |
102 | }; |
103 | |
104 | Ok(Self::new(value, span)) |
105 | } |
106 | } |
107 | |
108 | spanned!(FromGenericParam, from_generic_param, syn::GenericParam); |
109 | spanned!(FromGenerics, from_generics, syn::Generics); |
110 | spanned!(FromTypeParam, from_type_param, syn::TypeParam); |
111 | spanned!(FromDeriveInput, from_derive_input, syn::DeriveInput); |
112 | spanned!(FromField, from_field, syn::Field); |
113 | spanned!(FromVariant, from_variant, syn::Variant); |
114 | |
115 | impl<T: Spanned> From<T> for SpannedValue<T> { |
116 | fn from(value: T) -> Self { |
117 | let span: Span = value.span(); |
118 | SpannedValue::new(value, span) |
119 | } |
120 | } |
121 | |
122 | #[cfg (test)] |
123 | mod tests { |
124 | use super::*; |
125 | use proc_macro2::Span; |
126 | |
127 | /// Make sure that `SpannedValue` can be seamlessly used as its underlying type. |
128 | #[test ] |
129 | fn deref() { |
130 | let test = SpannedValue::new("hello" , Span::call_site()); |
131 | assert_eq!("hello" , test .trim()); |
132 | } |
133 | } |
134 | |