Skip to content

Commit

Permalink
add double into for option types
Browse files Browse the repository at this point in the history
  • Loading branch information
maccesch committed Jul 15, 2023
1 parent 12519b2 commit 268d56b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "default-struct-builder"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
authors = ["Marc-Stefan Cassola"]
description = "Generates builder methods of every field of a struct."
Expand Down
41 changes: 27 additions & 14 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,34 @@ impl ToTokens for DefaultBuilderDeriveInput {
}
}

let boxed_inner_type = get_boxed_inner_type(&ty);
let boxed_inner_type = get_inner_type(&ty, "Box");
let option_inner_type = get_inner_type(&ty, "Option");

if f.into {
methods.push(quote! {
#(#attrs)*
pub fn #name<T__>(self, value: T__) -> Self
where
T__: Into<#ty>,
{
Self {
#name: value.into(),
#dot_dot_self
if let Some(inner_type) = option_inner_type {
methods.push(quote! {
#(#attrs)*
pub fn #name<OptionInnerType>(self, value: impl Into<Option<OptionInnerType>>) -> Self
where
OptionInnerType: Into<#inner_type>
{
Self {
#name: value.into().map(|v| v.into()),
#dot_dot_self
}
}
}
})
})
} else {
methods.push(quote! {
#(#attrs)*
pub fn #name(self, value: impl Into<#ty>) -> Self {
Self {
#name: value.into(),
#dot_dot_self
}
}
})
}
} else if boxed_inner_type.is_some() && !f.keep_box {
let boxed_inner_type = boxed_inner_type.expect("just checked above");

Expand Down Expand Up @@ -407,11 +420,11 @@ fn stream_contains(s: &TokenStream, t: &TokenTree) -> bool {
})
}

fn get_boxed_inner_type(ty: &Type) -> Option<Type> {
fn get_inner_type(ty: &Type, outer_type_start: &str) -> Option<Type> {
match ty {
Type::Path(path) => {
if let Some(seg) = path.path.segments.last() {
if seg.ident.to_string().starts_with("Box") {
if seg.ident.to_string().starts_with(outer_type_start) {
if let PathArguments::AngleBracketed(args) = &seg.arguments {
if let Some(GenericArgument::Type(ty)) = args.args.first() {
return Some(ty.clone());
Expand Down

0 comments on commit 268d56b

Please sign in to comment.