don't generate default with getter

This commit is contained in:
Max Känner 2024-08-18 18:28:29 +02:00
parent 0e59545f92
commit 903a3c7589
2 changed files with 16 additions and 2 deletions

View File

@ -39,6 +39,7 @@ impl ConfigField {
pub(crate) fn helper(&self, attrs: &[syn::Attribute]) -> (Option<TokenStream>, syn::Type) { pub(crate) fn helper(&self, attrs: &[syn::Attribute]) -> (Option<TokenStream>, syn::Type) {
if self.needs_newtype() { if self.needs_newtype() {
let default = self.helper_default();
let derives = attrs let derives = attrs
.iter() .iter()
.find(|attr| attr.path().is_ident("derive")) .find(|attr| attr.path().is_ident("derive"))
@ -50,7 +51,7 @@ impl ConfigField {
derive.contains("Tree") derive.contains("Tree")
|| derive.contains("Serialize") || derive.contains("Serialize")
|| derive.contains("Deserialize") || derive.contains("Deserialize")
|| (self.default.is_some() && derive.contains("Default")) || (default.is_some() && derive.contains("Default"))
}) == Some(false) }) == Some(false)
}); });
quote! {#[derive(#(#derives,)*)]} quote! {#[derive(#(#derives,)*)]}
@ -60,7 +61,6 @@ impl ConfigField {
let ty = &self.ty; let ty = &self.ty;
let new = self.helper_new(); let new = self.helper_new();
let default = self.helper_default();
let serde = self.helper_serde(); let serde = self.helper_serde();
let tree = self.helper_tree(); let tree = self.helper_tree();
@ -153,6 +153,9 @@ impl ConfigField {
} }
pub(crate) fn helper_default(&self) -> Option<TokenStream> { pub(crate) fn helper_default(&self) -> Option<TokenStream> {
if self.typ.is_some() {
return None;
}
self.default.as_ref().map(|default| { self.default.as_ref().map(|default| {
let ident = self.helper_ident(); let ident = self.helper_ident();
let default_default = parse_quote!(::core::default::Default::default()); let default_default = parse_quote!(::core::default::Default::default());

View File

@ -28,6 +28,17 @@ struct Config {
field: Cell<i32>, field: Cell<i32>,
} }
#[config]
struct WithSetterAndDefault {
#[config(
default = "42",
typ = "i32",
get = "Cell::get",
set = "set_cell::<{i32::MIN}, {i32::MAX}>"
)]
field: Cell<i32>,
}
#[test] #[test]
fn get() { fn get() {
let mut buffer = [0u8; 32]; let mut buffer = [0u8; 32];