refactor: config improvements

This commit is contained in:
Ashley Wulber 2024-01-18 19:01:11 -05:00 committed by Jeremy Soller
parent 3eed30f723
commit efe4ce2f5b
7 changed files with 69 additions and 86 deletions

View file

@ -1,6 +1,6 @@
use std::ops::Deref;
use crate::CosmicConfigEntry;
use crate::{CosmicConfigEntry, Update};
use cosmic_settings_daemon::{ConfigProxy, CosmicSettingsDaemonProxy};
use futures_util::SinkExt;
use iced_futures::futures::{future::pending, StreamExt};
@ -51,13 +51,6 @@ impl Watcher {
}
}
#[derive(Debug)]
pub struct Update<T> {
pub errors: Vec<crate::Error>,
pub keys: Vec<&'static str>,
pub config: T,
}
pub fn watcher_subscription<T: CosmicConfigEntry + Send + Sync + Default + 'static + Clone>(
settings_daemon: CosmicSettingsDaemonProxy<'static>,
config_id: &'static str,
@ -79,7 +72,7 @@ pub fn watcher_subscription<T: CosmicConfigEntry + Send + Sync + Default + 'stat
Ok(config) => config,
Err((errors, default)) => {
if !errors.is_empty() {
eprintln!("Failed to get config: {errors:?}");
eprintln!("Error getting config: {config_id} {errors:?}");
}
default
}

View file

@ -32,6 +32,7 @@ pub enum Error {
Notify(notify::Error),
Ron(ron::Error),
RonSpanned(ron::error::SpannedError),
GetKey(String, std::io::Error),
}
impl fmt::Display for Error {
@ -44,6 +45,7 @@ impl fmt::Display for Error {
Self::Notify(err) => err.fmt(f),
Self::Ron(err) => err.fmt(f),
Self::RonSpanned(err) => err.fmt(f),
Self::GetKey(key, err) => write!(f, "failed to get key '{}': {}", key, err),
}
}
}
@ -264,11 +266,11 @@ impl ConfigGet for Config {
let key_path = self.key_path(key)?;
let data = if key_path.is_file() {
// Load user override
fs::read_to_string(key_path)?
fs::read_to_string(key_path).map_err(|err| Error::GetKey(key.to_string(), err))?
} else {
// Load system default
let default_path = self.default_path(key)?;
fs::read_to_string(default_path)?
fs::read_to_string(default_path).map_err(|err| Error::GetKey(key.to_string(), err))?
};
let t = ron::from_str(&data)?;
Ok(t)
@ -339,3 +341,9 @@ where
changed_keys: &[T],
) -> (Vec<crate::Error>, Vec<&'static str>);
}
pub struct Update<T> {
pub errors: Vec<crate::Error>,
pub keys: Vec<&'static str>,
pub config: T,
}

View file

@ -12,8 +12,7 @@ pub enum ConfigState<T> {
}
pub enum ConfigUpdate<T> {
Update(T),
UpdateError(T, Vec<crate::Error>),
Update(crate::Update<T>),
Failed,
}
@ -24,7 +23,7 @@ pub fn config_subscription<
id: I,
config_id: Cow<'static, str>,
config_version: u64,
) -> iced_futures::Subscription<(I, Result<T, (Vec<crate::Error>, T)>)> {
) -> iced_futures::Subscription<crate::Update<T>> {
subscription::channel(id, 100, move |mut output| {
let config_id = config_id.clone();
async move {
@ -45,7 +44,7 @@ pub fn config_state_subscription<
id: I,
config_id: Cow<'static, str>,
config_version: u64,
) -> iced_futures::Subscription<(I, Result<T, (Vec<crate::Error>, T)>)> {
) -> iced_futures::Subscription<crate::Update<T>> {
subscription::channel(id, 100, move |mut output| {
let config_id = config_id.clone();
async move {
@ -64,7 +63,7 @@ async fn start_listening<
T: 'static + Send + Sync + PartialEq + Clone + CosmicConfigEntry,
>(
state: ConfigState<T>,
output: &mut mpsc::Sender<(I, Result<T, (Vec<crate::Error>, T)>)>,
output: &mut mpsc::Sender<crate::Update<T>>,
id: I,
) -> ConfigState<T> {
use iced_futures::futures::{future::pending, StreamExt};
@ -90,11 +89,21 @@ async fn start_listening<
match T::get_entry(&config) {
Ok(t) => {
_ = output.send((id, Ok(t.clone()))).await;
let update = crate::Update {
errors: Vec::new(),
keys: Vec::new(),
config: t.clone(),
};
_ = output.send(update).await;
ConfigState::Waiting(t, watcher, rx, config)
}
Err((errors, t)) => {
_ = output.send((id, Err((errors, t.clone())))).await;
let update = crate::Update {
errors: errors,
keys: Vec::new(),
config: t.clone(),
};
_ = output.send(update).await;
ConfigState::Waiting(t, watcher, rx, config)
}
}
@ -104,11 +113,13 @@ async fn start_listening<
let (errors, changed) = conf_data.update_keys(&config, &keys);
if !changed.is_empty() {
if errors.is_empty() {
_ = output.send((id, Ok(conf_data.clone()))).await;
} else {
_ = output.send((id, Err((errors, conf_data.clone())))).await;
}
_ = output
.send(crate::Update {
errors: errors,
keys: changed,
config: conf_data.clone(),
})
.await;
}
ConfigState::Waiting(conf_data, watcher, rx, config)
}