Skip to content

Commit

Permalink
chore: Create fold module
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Aug 5, 2024
1 parent 940c79d commit 85226a8
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion sylvia-derive/src/contract/communication/wrapper_msg.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion sylvia-derive/src/entry_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
25 changes: 24 additions & 1 deletion sylvia-derive/src/strip_input.rs → sylvia-derive/src/fold.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 })
}
}
6 changes: 2 additions & 4 deletions sylvia-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion sylvia-derive/src/parser/attributes/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 0 additions & 10 deletions sylvia-derive/src/strip_generics.rs

This file was deleted.

14 changes: 0 additions & 14 deletions sylvia-derive/src/strip_self_path.rs

This file was deleted.

2 changes: 1 addition & 1 deletion sylvia-derive/src/types/msg_field.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion sylvia-derive/src/types/msg_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion sylvia-derive/src/types/msg_variant.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 85226a8

Please sign in to comment.