diff --git a/sylvia-derive/src/contract/communication/wrapper_msg.rs b/sylvia-derive/src/contract/communication/wrapper_msg.rs index e7151b14..ff0da794 100644 --- a/sylvia-derive/src/contract/communication/wrapper_msg.rs +++ b/sylvia-derive/src/contract/communication/wrapper_msg.rs @@ -1,6 +1,6 @@ use crate::crate_module; +use crate::fold::StripGenerics; use crate::parser::{ContractErrorAttr, Custom, MsgType}; -use crate::strip_generics::StripGenerics; use crate::types::interfaces::Interfaces; use crate::utils::emit_bracketed_generics; use proc_macro2::TokenStream; diff --git a/sylvia-derive/src/entry_points.rs b/sylvia-derive/src/entry_points.rs index 95407f2d..d205a541 100644 --- a/sylvia-derive/src/entry_points.rs +++ b/sylvia-derive/src/entry_points.rs @@ -5,12 +5,12 @@ use syn::fold::Fold; use syn::{parse_quote, GenericParam, Ident, ItemImpl, Type, WhereClause}; use crate::crate_module; +use crate::fold::StripGenerics; use crate::parser::attributes::msg::MsgType; use crate::parser::variant_descs::AsVariantDescs; use crate::parser::{ EntryPointArgs, FilteredOverrideEntryPoints, OverrideEntryPoint, ParsedSylviaAttributes, }; -use crate::strip_generics::StripGenerics; use crate::types::msg_variant::MsgVariants; pub struct EntryPointInput<'a> { diff --git a/sylvia-derive/src/strip_input.rs b/sylvia-derive/src/fold.rs similarity index 75% rename from sylvia-derive/src/strip_input.rs rename to sylvia-derive/src/fold.rs index 19f47dd8..11ac2cc3 100644 --- a/sylvia-derive/src/strip_input.rs +++ b/sylvia-derive/src/fold.rs @@ -1,7 +1,7 @@ use syn::fold::{self, Fold}; use syn::punctuated::Punctuated; use syn::{ - FnArg, ImplItemFn, ItemImpl, ItemTrait, PatType, Receiver, Signature, Token, TraitItemFn, + FnArg, ImplItemFn, ItemImpl, ItemTrait, PatType, Path, Receiver, Signature, Token, TraitItemFn, }; use crate::parser::SylviaAttribute; @@ -77,3 +77,26 @@ impl Fold for StripInput { fold::fold_item_impl(self, ItemImpl { attrs, ..i }) } } + +/// Removes all generics from type assumning default values for it +pub struct StripGenerics; + +impl Fold for StripGenerics { + fn fold_path_arguments(&mut self, _: syn::PathArguments) -> syn::PathArguments { + syn::PathArguments::None + } +} + +/// Removes `Self` from associated type [Path]. +pub struct StripSelfPath; + +impl Fold for StripSelfPath { + fn fold_path(&mut self, path: Path) -> Path { + let segments = path + .segments + .into_iter() + .filter(|segment| segment.ident != "Self") + .collect(); + syn::fold::fold_path(self, Path { segments, ..path }) + } +} diff --git a/sylvia-derive/src/lib.rs b/sylvia-derive/src/lib.rs index 712d5f56..f581f8aa 100644 --- a/sylvia-derive/src/lib.rs +++ b/sylvia-derive/src/lib.rs @@ -5,23 +5,21 @@ use crate::parser::EntryPointArgs; use contract::ContractInput; use entry_points::EntryPointInput; +use fold::StripInput; use interface::InterfaceInput; use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use proc_macro_error::proc_macro_error; use quote::quote; -use strip_input::StripInput; use syn::fold::Fold; use syn::spanned::Spanned; use syn::{parse2, parse_quote, ItemImpl, ItemTrait, Path}; mod contract; mod entry_points; +mod fold; mod interface; mod parser; -mod strip_generics; -mod strip_input; -mod strip_self_path; mod types; mod utils; diff --git a/sylvia-derive/src/parser/attributes/messages.rs b/sylvia-derive/src/parser/attributes/messages.rs index 138f9e05..e7708210 100644 --- a/sylvia-derive/src/parser/attributes/messages.rs +++ b/sylvia-derive/src/parser/attributes/messages.rs @@ -7,7 +7,7 @@ use syn::{parenthesized, Error, Ident, MetaList, Path, Result, Token}; use proc_macro_error::emit_error; -use crate::strip_generics::StripGenerics; +use crate::fold::StripGenerics; #[derive(Debug)] pub struct ContractMessageAttr { diff --git a/sylvia-derive/src/strip_generics.rs b/sylvia-derive/src/strip_generics.rs deleted file mode 100644 index 7681d515..00000000 --- a/sylvia-derive/src/strip_generics.rs +++ /dev/null @@ -1,10 +0,0 @@ -use syn::fold::Fold; - -/// Removes all generics from type assumning default values for it -pub struct StripGenerics; - -impl Fold for StripGenerics { - fn fold_path_arguments(&mut self, _: syn::PathArguments) -> syn::PathArguments { - syn::PathArguments::None - } -} diff --git a/sylvia-derive/src/strip_self_path.rs b/sylvia-derive/src/strip_self_path.rs deleted file mode 100644 index 93657ce0..00000000 --- a/sylvia-derive/src/strip_self_path.rs +++ /dev/null @@ -1,14 +0,0 @@ -use syn::fold::Fold; -use syn::Path; -pub struct StripSelfPath; - -impl Fold for StripSelfPath { - fn fold_path(&mut self, path: Path) -> Path { - let segments = path - .segments - .into_iter() - .filter(|segment| segment.ident != "Self") - .collect(); - syn::fold::fold_path(self, Path { segments, ..path }) - } -} diff --git a/sylvia-derive/src/types/msg_field.rs b/sylvia-derive/src/types/msg_field.rs index a3e719ed..14f26de8 100644 --- a/sylvia-derive/src/types/msg_field.rs +++ b/sylvia-derive/src/types/msg_field.rs @@ -1,5 +1,5 @@ +use crate::fold::StripSelfPath; use crate::parser::check_generics::{CheckGenerics, GetPath}; -use crate::strip_self_path::StripSelfPath; use proc_macro2::TokenStream; use proc_macro_error::emit_error; use quote::quote; diff --git a/sylvia-derive/src/types/msg_type.rs b/sylvia-derive/src/types/msg_type.rs index a80f0069..a2c0d49e 100644 --- a/sylvia-derive/src/types/msg_type.rs +++ b/sylvia-derive/src/types/msg_type.rs @@ -5,9 +5,9 @@ use syn::fold::Fold; use syn::{parse_quote, GenericParam, Ident, Type}; use crate::crate_module; +use crate::fold::StripSelfPath; use crate::parser::attributes::msg::MsgType; use crate::parser::Customs; -use crate::strip_self_path::StripSelfPath; impl MsgType { pub fn emit_ctx_type(self, query_type: &Type) -> TokenStream { diff --git a/sylvia-derive/src/types/msg_variant.rs b/sylvia-derive/src/types/msg_variant.rs index 6debfee9..c8a106ff 100644 --- a/sylvia-derive/src/types/msg_variant.rs +++ b/sylvia-derive/src/types/msg_variant.rs @@ -1,9 +1,9 @@ use crate::crate_module; +use crate::fold::StripSelfPath; use crate::parser::attributes::VariantAttrForwarding; use crate::parser::check_generics::{CheckGenerics, GetPath}; use crate::parser::variant_descs::VariantDescs; use crate::parser::{MsgAttr, MsgType}; -use crate::strip_self_path::StripSelfPath; use crate::utils::{extract_return_type, filter_wheres, process_fields, SvCasing}; use convert_case::{Case, Casing}; use proc_macro2::TokenStream;