summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2022-10-22 10:49:32 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2022-10-22 10:49:32 +0100
commit9318464daaee62481f7f80105bc2ec14c9c01e16 (patch)
tree798db05798d173b1b27163fcd57374a608b4c7a5
parent81e985cc026d67e3d506d485b3bf013bfd82987e (diff)
downloadsubplot-9318464daaee62481f7f80105bc2ec14c9c01e16.tar.gz
(derive): Support steps with &Path arguments
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
-rw-r--r--subplotlib-derive/src/lib.rs30
1 files changed, 29 insertions, 1 deletions
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<P: Into<std::path::PathBuf>>(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
}