summaryrefslogtreecommitdiff
path: root/src/util/with_original.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/with_original.rs')
-rw-r--r--src/util/with_original.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/util/with_original.rs b/src/util/with_original.rs
new file mode 100644
index 0000000..5aa82f8
--- /dev/null
+++ b/src/util/with_original.rs
@@ -0,0 +1,35 @@
+use crate::{
+ FromDeriveInput, FromField, FromGenericParam, FromGenerics, FromMeta, FromTypeParam,
+ FromVariant, Result,
+};
+
+/// A container to parse some syntax and retain access to the original.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct WithOriginal<T, O> {
+ pub parsed: T,
+ pub original: O,
+}
+
+impl<T, O> WithOriginal<T, O> {
+ pub fn new(parsed: T, original: O) -> Self {
+ WithOriginal { parsed, original }
+ }
+}
+
+macro_rules! with_original {
+ ($trayt:ident, $func:ident, $syn:path) => {
+ impl<T: $trayt> $trayt for WithOriginal<T, $syn> {
+ fn $func(value: &$syn) -> Result<Self> {
+ Ok(WithOriginal::new($trayt::$func(value)?, value.clone()))
+ }
+ }
+ };
+}
+
+with_original!(FromDeriveInput, from_derive_input, syn::DeriveInput);
+with_original!(FromField, from_field, syn::Field);
+with_original!(FromGenerics, from_generics, syn::Generics);
+with_original!(FromGenericParam, from_generic_param, syn::GenericParam);
+with_original!(FromMeta, from_meta, syn::Meta);
+with_original!(FromTypeParam, from_type_param, syn::TypeParam);
+with_original!(FromVariant, from_variant, syn::Variant);