set config field take optional Config

This commit is contained in:
wiiznokes 2024-04-09 14:47:56 +02:00
parent 0caff62a18
commit 6c933d2d55
4 changed files with 22 additions and 5 deletions

View file

@ -71,7 +71,7 @@ serde = { version = "1.0.180", optional = true }
slotmap = "1.0.6" slotmap = "1.0.6"
thiserror = "1.0.44" thiserror = "1.0.44"
tokio = { version = "1.24.2", optional = true } tokio = { version = "1.24.2", optional = true }
tracing = "0.1" tracing.workspace = true
unicode-segmentation = "1.6" unicode-segmentation = "1.6"
url = "2.4.0" url = "2.4.0"
zbus = {version = "3.14.1", default-features = false, optional = true} zbus = {version = "3.14.1", default-features = false, optional = true}
@ -152,6 +152,7 @@ exclude = ["examples/design-demo", "iced"]
[workspace.dependencies] [workspace.dependencies]
dirs = "5.0.1" dirs = "5.0.1"
tracing = "0.1"
[patch."https://github.com/pop-os/libcosmic"] [patch."https://github.com/pop-os/libcosmic"]

View file

@ -9,4 +9,5 @@ proc-macro = true
[dependencies] [dependencies]
syn = "1.0" syn = "1.0"
quote = "1.0" quote = "1.0"
tracing.workspace = true

View file

@ -89,11 +89,25 @@ fn impl_cosmic_config_entry_macro(ast: &syn::DeriveInput) -> TokenStream {
Some(quote! { Some(quote! {
#[doc = #doc] #[doc = #doc]
/// ///
/// Returns `Ok(true)` when the field's value has changed and was written to disk /// 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> { /// Log error in case of faillure
pub fn #setter_name(&mut self, config: &Option<cosmic_config::Config>, value: #field_type) -> Result<bool, cosmic_config::Error> {
if self.#field_name != value { if self.#field_name != value {
self.#field_name = value; self.#field_name = value;
cosmic_config::ConfigSet::set(config, stringify!(#field_name), &self.#field_name)?; match config {
Some(config) => {
if let Err(err) = cosmic_config::ConfigSet::set(config, stringify!(#field_name), &self.#field_name) {
tracing::error!("failed to save config {:?}: {}", stringify!(#field_name), err);
return Err(err);
}
}
None => {
tracing::error!(
"failed to save config {}: no config handler",
stringify!(#field_name),
);
}
}
Ok(true) Ok(true)
} else { } else {
Ok(false) Ok(false)

View file

@ -27,3 +27,4 @@ csscolorparser = {version = "0.6.2", features = ["serde"]}
cosmic-config = { path = "../cosmic-config/", default-features = false, features = ["subscription", "macro"] } cosmic-config = { path = "../cosmic-config/", default-features = false, features = ["subscription", "macro"] }
dirs.workspace = true dirs.workspace = true
thiserror = "1.0.5" thiserror = "1.0.5"
tracing.workspace = true