Skip to content

Commit

Permalink
Merge pull request #11 from jifalops/more-copy-types
Browse files Browse the repository at this point in the history
More copy types
  • Loading branch information
jifalops authored Aug 12, 2023
2 parents 894b88b + 4d2aa16 commit de6d6c1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
41 changes: 35 additions & 6 deletions macros/src/derive/entity/field/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl ArgHelper {
let mut getter_return =
syn::parse_str(&format!("Option<&{}>", quote!(#inner))).unwrap();
let mut field_into_getter_suffix = quote!(.as_ref());
if Num::from_type(inner).is_some() {
if is_copy_type(inner) {
getter_return = ty.clone();
field_into_getter_suffix = quote!();
}
Expand All @@ -119,11 +119,18 @@ impl ArgHelper {
field_into_getter_suffix,
}
} else {
let is_copy = is_copy_type(&ty);
Self {
arg_type: ty.clone(),
arg_into_field_suffix: quote!(),
getter_return: syn::parse_str(&format!("&{}", quote!(#ty))).unwrap(),
field_into_getter_prefix_amp: quote!(&),
getter_return: match is_copy {
true => ty.clone(),
false => syn::parse_str(&format!("&{}", quote!(#ty))).unwrap(),
},
field_into_getter_prefix_amp: match is_copy {
true => quote!(),
false => quote!(&),
},
field_into_getter_suffix: quote!(),
}
}
Expand Down Expand Up @@ -203,6 +210,23 @@ fn inner_type(ty: &Type) -> Option<&Type> {
}
}

fn is_copy_type(ty: &Type) -> bool {
if Num::from_type(ty).is_some() {
return true;
}
let path = match ty {
Type::Path(path) => path,
_ => return false,
};
let last_segment = path.path.segments.last().unwrap();
let name = last_segment.ident.to_string();
match name.as_str() {
"bool" => true,
"char" => true,
_ => false,
}
}

/// Number info for adding to a [neo4rs::Query::param] or extracting from a BoltMap (e.g. [neo4rs::Row]).
#[derive(Clone, Debug, PartialEq)]
pub enum Num {
Expand Down Expand Up @@ -364,19 +388,24 @@ mod tests {
let ty = syn::parse_str("String").unwrap();
let helper = ArgHelper::new(&ty);
assert_eq!(to_string(&helper.arg_type), "& str");
assert_eq!(to_string(&helper.getter_return), "str");
assert_eq!(to_string(&helper.getter_return), "& str");
assert_eq!(helper.arg_into_field_suffix.to_string(), ". to_owned ()");

let ty = syn::parse_str("Vec<String>").unwrap();
let helper = ArgHelper::new(&ty);
assert_eq!(to_string(&helper.arg_type), "& [String]");
assert_eq!(to_string(&helper.getter_return), "[String]");
assert_eq!(to_string(&helper.getter_return), "& [String]");
assert_eq!(helper.arg_into_field_suffix.to_string(), ". to_vec ()");

let ty = syn::parse_str("Option<String>").unwrap();
let helper = ArgHelper::new(&ty);
assert_eq!(to_string(&helper.arg_type), "Option < String >");
assert_eq!(to_string(&helper.getter_return), "Option < String >");
assert_eq!(to_string(&helper.getter_return), "Option < & String >");
assert_eq!(helper.arg_into_field_suffix.to_string(), "");

let ty = syn::parse_str("bool").unwrap();
let helper = ArgHelper::new(&ty);
assert_eq!(to_string(&helper.arg_type), "bool");
assert_eq!(to_string(&helper.getter_return), "bool");
}
}
4 changes: 2 additions & 2 deletions macros/src/stamps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn stamps_impl(args: TokenStream, input: TokenStream) -> TokenStream {
let name = &input.ident;
let data = match &input.data {
syn::Data::Struct(s) => &s.fields,
_ => panic!("#[stamped] can only be used with structs"),
_ => panic!("#[stamps] can only be used with structs"),
};
let input_attrs = input.attrs;
let input_vis = input.vis;
Expand All @@ -30,7 +30,7 @@ pub fn stamps_impl(args: TokenStream, input: TokenStream) -> TokenStream {
#vis #name: #ty,
}
}),
_ => panic!("stamped can only be used with structs with named fields"),
_ => panic!("stamps can only be used on structs with named fields"),
};
let (stamp_idents, stamp_types) = stamps.into_fields();

Expand Down

0 comments on commit de6d6c1

Please sign in to comment.