Skip to content

Commit

Permalink
Improve codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
vklachkov committed Nov 4, 2024
1 parent a8dec3b commit 00fb9f6
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions crates/jrsonnet-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ impl TypedField {
(#name, <#ty as Typed>::TYPE)
})
}

fn expand_parse(&self) -> TokenStream {
if self.is_option {
self.expand_parse_optional()
Expand All @@ -566,18 +567,18 @@ impl TypedField {
let name = self.name().unwrap();
let aliases = &self.attr.aliases;

let value = quote! {
if let Some(__value) = obj.get(#name.into())? {
Some(<#ty as Typed>::from_untyped(__value)?)
} #(else if let Some(__value) = obj.get(#aliases) {
Some(<#ty as Typed>::from_untyped(__value)?)
})* else {
None
}
};

quote! {
#ident: #value,
#ident: {
let __value = if let Some(__v) = obj.get(#name.into())? {
Some(__v)
} #(else if let Some(__v) = obj.get(#aliases.into())? {
Some(__v)
})* else {
None
};

__value.map(<#ty as Typed>::from_untyped).transpose()?
},
}
}

Expand All @@ -603,18 +604,18 @@ impl TypedField {
format!("{name} (alias {})", aliases.join(", "))
};

let value = quote! {
if let Some(__value) = obj.get(#name.into())? {
<#ty as Typed>::from_untyped(__value)?
} #(else if let Some(__value) = obj.get(#aliases.into())? {
<#ty as Typed>::from_untyped(__value)?
})* else {
return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());
}
};

quote! {
#ident: #value,
#ident: {
let __value = if let Some(__v) = obj.get(#name.into())? {
__v
} #(else if let Some(__v) = obj.get(#aliases.into())? {
__v
})* else {
return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());
};

<#ty as Typed>::from_untyped(__value)?
},
}
}

Expand Down

0 comments on commit 00fb9f6

Please sign in to comment.