From 9318464daaee62481f7f80105bc2ec14c9c01e16 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 22 Oct 2022 10:49:32 +0100 Subject: (derive): Support steps with &Path arguments Signed-off-by: Daniel Silverstone --- subplotlib-derive/src/lib.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/subplotlib-derive/src/lib.rs b/subplotlib-derive/src/lib.rs index 86f5c5e..7aef0dc 100644 --- a/subplotlib-derive/src/lib.rs +++ b/subplotlib-derive/src/lib.rs @@ -29,6 +29,25 @@ fn ty_is_borrow_str(ty: &Type) -> bool { } } +fn ty_is_borrow_path(ty: &Type) -> bool { + if let Type::Reference(ty) = ty { + if ty.mutability.is_none() && ty.lifetime.is_none() { + if let Type::Path(pp) = &*ty.elem { + pp.path.is_ident("Path") + } else { + // not a path, so not &Path + false + } + } else { + // mutable, or a lifetime stated, so not &Path + false + } + } else { + // Not & so not &Path + false + } +} + fn ty_is_datafile(ty: &Type) -> bool { if let Type::Path(ty) = ty { ty.path.is_ident("SubplotDataFile") @@ -242,6 +261,8 @@ fn process_step(mut input: ItemFn) -> proc_macro2::TokenStream { .map(|(id, ty)| { let ty = if ty_is_borrow_str(ty) { parse_quote!(::std::string::String) + } else if ty_is_borrow_path(ty) { + parse_quote!(::std::path::PathBuf) } else { ty.clone() }; @@ -278,6 +299,13 @@ fn process_step(mut input: ItemFn) -> proc_macro2::TokenStream { self } } + } else if ty_is_borrow_path(ty) { + quote! { + pub fn #id>(mut self, value: P) -> Self { + self.#id = value.into(); + self + } + } } else { quote! { pub fn #id(mut self, value: #ty) -> Self { @@ -292,7 +320,7 @@ fn process_step(mut input: ItemFn) -> proc_macro2::TokenStream { let buildargs: Vec<_> = fields .iter() .map(|(id, ty)| { - if ty_is_borrow_str(ty) { + if ty_is_borrow_str(ty) || ty_is_borrow_path(ty) { quote! { &self.#id } -- cgit v1.2.1