cosmic-config-derive: automatically generate setters
This commit is contained in:
parent
2c6db80c64
commit
63802dfcf9
2 changed files with 25 additions and 5 deletions
|
|
@ -80,6 +80,27 @@ fn impl_cosmic_config_entry_macro(ast: &syn::DeriveInput) -> TokenStream {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let setters = fields.iter().filter_map(|field| {
|
||||||
|
let field_name = &field.ident.as_ref()?;
|
||||||
|
let field_type = &field.ty;
|
||||||
|
let setter_name = quote::format_ident!("set_{}", field_name);
|
||||||
|
let doc = format!("Sets [`{name}::{field_name}`] and writes to [`cosmic_config::Config`] if changed");
|
||||||
|
Some(quote! {
|
||||||
|
#[doc = #doc]
|
||||||
|
///
|
||||||
|
/// Returns `Ok(true)` when the field's value has changed and was written to disk
|
||||||
|
pub fn #setter_name(&mut self, config: &cosmic_config::Config, value: #field_type) -> Result<bool, cosmic_config::Error> {
|
||||||
|
if self.#field_name != value {
|
||||||
|
self.#field_name = value;
|
||||||
|
cosmic_config::ConfigSet::set(config, stringify!(#field_name), &self.#field_name)?;
|
||||||
|
Ok(true)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
let gen = quote! {
|
let gen = quote! {
|
||||||
impl CosmicConfigEntry for #name {
|
impl CosmicConfigEntry for #name {
|
||||||
const VERSION: u64 = #version;
|
const VERSION: u64 = #version;
|
||||||
|
|
@ -115,6 +136,10 @@ fn impl_cosmic_config_entry_macro(ast: &syn::DeriveInput) -> TokenStream {
|
||||||
(errors, keys)
|
(errors, keys)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl #name {
|
||||||
|
#(#setters)*
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
gen.into()
|
gen.into()
|
||||||
|
|
|
||||||
|
|
@ -35,11 +35,6 @@ impl ThemeMode {
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set auto-switch from light to dark mode
|
|
||||||
pub fn set_auto_switch(config: &Config, value: bool) -> Result<(), cosmic_config::Error> {
|
|
||||||
config.set("auto_switch", value)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the config for the theme mode
|
/// Get the config for the theme mode
|
||||||
pub fn config() -> Result<Config, cosmic_config::Error> {
|
pub fn config() -> Result<Config, cosmic_config::Error> {
|
||||||
Config::new(THEME_MODE_ID, Self::version())
|
Config::new(THEME_MODE_ID, Self::version())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue