Skip to content

Commit

Permalink
bump syn
Browse files Browse the repository at this point in the history
  • Loading branch information
nrxus committed Jul 18, 2023
1 parent 72c06c8 commit fe61c37
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 61 deletions.
6 changes: 3 additions & 3 deletions faux_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ keywords = ["mock", "mocking", "test", "testing", "faux"]
rust-version = "1.56"

[dependencies]
syn = { version = "1.0.58", features = ["full", "extra-traits"] }
syn = { version = "2", features = ["full", "extra-traits"] }
quote = "1.0.8"
proc-macro2 = "1.0.24"
darling = "0.14"
uuid = { version = "0.8.2", features = ["v4"] }
darling = "0.20"
uuid = { version = "1", features = ["v4"] }

[dev-dependencies]
faux = { path = "../" }
Expand Down
26 changes: 13 additions & 13 deletions faux_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ mod create;
mod methods;
mod self_type;

use darling::FromMeta;
use darling::{export::NestedMeta, FromMeta};
use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_attribute]
pub fn create(args: TokenStream, original: TokenStream) -> TokenStream {
let original = syn::parse_macro_input!(original as syn::ItemStruct);

let args = syn::parse_macro_input!(args as syn::AttributeArgs);
let args = match create::Args::from_list(&args) {
let args = match NestedMeta::parse_meta_list(args.into())
.map_err(darling::Error::from)
.and_then(|v| create::Args::from_list(&v))
{
Ok(v) => v,
Err(e) => {
return e.write_errors().into();
}
Err(e) => return e.write_errors().into(),
};

let mockable = create::Mockable::new(original, args);
Expand All @@ -29,12 +29,12 @@ pub fn create(args: TokenStream, original: TokenStream) -> TokenStream {
pub fn methods(args: TokenStream, original: TokenStream) -> TokenStream {
let original = syn::parse_macro_input!(original as syn::ItemImpl);

let args = syn::parse_macro_input!(args as syn::AttributeArgs);
let args = match methods::Args::from_list(&args) {
let args = match NestedMeta::parse_meta_list(args.into())
.map_err(darling::Error::from)
.and_then(|v| methods::Args::from_list(&v))
{
Ok(v) => v,
Err(e) => {
return e.write_errors().into();
}
Err(e) => return e.write_errors().into(),
};

match methods::Mockable::new(original, args) {
Expand Down Expand Up @@ -87,7 +87,7 @@ fn ref_matcher_maybe(
matcher: impl FnOnce() -> darling::Result<proc_macro2::TokenStream>,
) -> darling::Result<proc_macro2::TokenStream> {
match left {
syn::Expr::Verbatim(t) if t.to_string() == "_" => matcher(),
syn::Expr::Infer(_) => matcher(),
syn::Expr::Unary(syn::ExprUnary {
op: syn::UnOp::Deref(_),
expr,
Expand All @@ -102,7 +102,7 @@ fn ref_matcher_maybe(

fn expr_to_matcher(expr: syn::Expr) -> darling::Result<proc_macro2::TokenStream> {
match &expr {
syn::Expr::Verbatim(t) if t.to_string() == "_" => Ok(quote! { faux::matcher::any() }),
syn::Expr::Infer(_) => Ok(quote! { faux::matcher::any() }),
syn::Expr::Assign(syn::ExprAssign { left, right, .. }) => {
ref_matcher_maybe(&expr, left, || Ok(right.to_token_stream()))
}
Expand Down
10 changes: 5 additions & 5 deletions faux_macros/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Mockable {
let mut morphed = real.clone();

let mut methods = morphed.items.iter_mut().filter_map(|item| match item {
syn::ImplItem::Method(m) => Some(m),
syn::ImplItem::Fn(m) => Some(m),
_ => None,
});

Expand All @@ -70,7 +70,7 @@ impl Mockable {
);
func.block = signature.create_body(args.self_type, &real_ty, &morphed_ty)?;
if let Some(methods) = signature.create_when() {
when_methods.extend(methods.into_iter().map(syn::ImplItem::Method));
when_methods.extend(methods.into_iter().map(syn::ImplItem::Fn));
}
}

Expand Down Expand Up @@ -143,12 +143,12 @@ impl From<Mockable> for proc_macro::TokenStream {
let ident = &real_ty.path.segments.last().unwrap().ident;
syn::Ident::new(
&match &real.trait_ {
None => format!("_faux_real_impl_{}_{}", ident, uuid.to_simple()),
None => format!("_faux_real_impl_{}_{}", ident, uuid.simple()),
Some((_, trait_, _)) => format!(
"_faux_real_impl_{}_{}_{}",
ident,
trait_.segments.last().unwrap().ident,
uuid.to_simple()
uuid.simple()
),
},
proc_macro2::Span::call_site(),
Expand Down Expand Up @@ -253,7 +253,7 @@ fn publicize_methods(impl_block: &mut syn::ItemImpl) {
.items
.iter_mut()
.filter_map(|item| match item {
syn::ImplItem::Method(m) => Some(m),
syn::ImplItem::Fn(m) => Some(m),
_ => None,
})
.filter(|method| method.vis == syn::Visibility::Inherited)
Expand Down
20 changes: 6 additions & 14 deletions faux_macros/src/methods/morphed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{spanned::Spanned, PathArguments};

struct SpanMarker(proc_macro2::Span);

impl Spanned for SpanMarker {
fn span(&self) -> proc_macro2::Span {
self.0
}
}

pub struct Signature<'a> {
name: &'a syn::Ident,
args: Vec<&'a syn::Pat>,
Expand Down Expand Up @@ -230,12 +222,12 @@ impl<'a> Signature<'a> {
};

Ok(syn::Block {
stmts: vec![syn::Stmt::Expr(ret)],
stmts: vec![syn::Stmt::Expr(ret, None)],
brace_token: Default::default(),
})
}

pub fn create_when(&self) -> Option<Vec<syn::ImplItemMethod>> {
pub fn create_when(&self) -> Option<Vec<syn::ImplItemFn>> {
self.method_data
.as_ref()
.filter(|m| !m.is_private)
Expand Down Expand Up @@ -341,13 +333,13 @@ impl<'a> MethodData<'a> {
&self,
output: Option<&syn::Type>,
name: &syn::Ident,
) -> Vec<syn::ImplItemMethod> {
) -> Vec<syn::ImplItemFn> {
let MethodData {
arg_types,
receiver,
..
} = self;
let receiver_tokens = &receiver.tokens;
let receiver_ty = &receiver.ty;

let when_ident =
syn::Ident::new(&format!("_when_{}", name), proc_macro2::Span::call_site());
Expand All @@ -359,7 +351,7 @@ impl<'a> MethodData<'a> {
let name_str = name.to_string();

let when_method = syn::parse_quote! {
pub fn #when_ident<'m>(&'m mut self) -> faux::When<'m, #receiver_tokens, (#(#arg_types),*), #output, faux::matcher::AnyInvocation> {
pub fn #when_ident<'m>(&'m mut self) -> faux::When<'m, #receiver_ty, (#(#arg_types),*), #output, faux::matcher::AnyInvocation> {
match &mut self.0 {
faux::MaybeFaux::Faux(_maybe_faux_faux) => faux::When::new(
<Self>::#faux_ident,
Expand All @@ -375,7 +367,7 @@ impl<'a> MethodData<'a> {
let faux_method = syn::parse_quote! {
#[allow(clippy::needless_arbitrary_self_type)]
#[allow(clippy::boxed_local)]
pub fn #faux_ident(self: #receiver_tokens, _: (#(#arg_types),*)) -> #output {
pub fn #faux_ident(self: #receiver_ty, _: (#(#arg_types),*)) -> #output {
panic!(#panic_message)
}
};
Expand Down
43 changes: 17 additions & 26 deletions faux_macros/src/methods/receiver.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
use crate::self_type::SelfType;
use proc_macro2::{Span, TokenStream};
use quote::quote;

use proc_macro2::TokenStream;
use quote::{quote, ToTokens};

use std::{
boxed::Box,
fmt::{self, Formatter},
};
use syn::spanned::Spanned;

pub struct Receiver {
span: Span,
pub kind: SelfKind,
pub tokens: TokenStream,
pub ty: Box<syn::Type>,
}

impl Receiver {
pub fn from_signature(signature: &syn::Signature) -> Option<Self> {
match signature.inputs.first()? {
syn::FnArg::Typed(arg) => match &*arg.pat {
syn::Pat::Ident(pat_ident) if pat_ident.ident == "self" => {
let arg_ty = &*arg.ty;
Some(Receiver {
kind: SelfKind::from_type(arg_ty),
span: arg_ty.span(),
tokens: quote! { #arg_ty },
})
}
_ => None,
},
syn::FnArg::Receiver(receiver) => {
let (kind, tokens) = match (&receiver.reference, &receiver.mutability) {
(None, _) => (SelfKind::Owned, quote! { Self }),
(Some(_), None) => (SelfKind::Pointer(PointerKind::Ref), quote! { &Self }),
(Some(_), Some(_)) => {
(SelfKind::Pointer(PointerKind::MutRef), quote! { &mut Self })
let kind = if receiver.colon_token.is_some() {
SelfKind::from_type(receiver.ty.as_ref())
} else {
match (&receiver.reference, &receiver.mutability) {
(None, _) => SelfKind::Owned,
(Some(_), None) => SelfKind::Pointer(PointerKind::Ref),
(Some(_), Some(_)) => SelfKind::Pointer(PointerKind::MutRef),
}
};

Some(Receiver {
kind,
tokens,
span: receiver.span(),
ty: receiver.ty.clone(),
})
}
_ => None,
}
}

Expand Down Expand Up @@ -233,9 +224,9 @@ impl fmt::Display for PointerKind {
}
}

impl Spanned for Receiver {
fn span(&self) -> Span {
self.span
impl ToTokens for Receiver {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.ty.to_tokens(tokens)
}
}

Expand Down

0 comments on commit fe61c37

Please sign in to comment.