Store user state in binary CBOR format
The state structure is growing a little too complex to store it as a TOML. User state is now stored at `~/.config/ncspot/userstate.cbor`.
This commit is contained in:
@@ -9,7 +9,7 @@ use platform_dirs::AppDirs;
|
||||
use crate::command::{SortDirection, SortKey};
|
||||
use crate::playable::Playable;
|
||||
use crate::queue;
|
||||
use crate::serialization::{Serializer, TOML};
|
||||
use crate::serialization::{Serializer, CBOR, TOML};
|
||||
|
||||
pub const CLIENT_ID: &str = "d420a117a32841c2b3474932e49fb54b";
|
||||
|
||||
@@ -66,7 +66,6 @@ pub struct QueueState {
|
||||
pub current_track: Option<usize>,
|
||||
pub random_order: Option<Vec<usize>>,
|
||||
pub track_progress: std::time::Duration,
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub queue: Vec<Playable>,
|
||||
}
|
||||
|
||||
@@ -76,7 +75,6 @@ pub struct UserState {
|
||||
pub shuffle: bool,
|
||||
pub repeat: queue::RepeatSetting,
|
||||
pub queuestate: QueueState,
|
||||
#[serde(serialize_with = "toml::ser::tables_last")]
|
||||
pub playlist_orders: HashMap<String, SortingOrder>,
|
||||
}
|
||||
|
||||
@@ -109,8 +107,8 @@ impl Config {
|
||||
});
|
||||
|
||||
let mut userstate = {
|
||||
let path = config_path("userstate.toml");
|
||||
TOML.load_or_generate_default(path, || Ok(UserState::default()), true)
|
||||
let path = config_path("userstate.cbor");
|
||||
CBOR.load_or_generate_default(path, || Ok(UserState::default()), true)
|
||||
.expect("could not load user state")
|
||||
};
|
||||
|
||||
@@ -145,9 +143,9 @@ impl Config {
|
||||
}
|
||||
|
||||
pub fn save_state(&self) {
|
||||
let path = config_path("userstate.toml");
|
||||
let path = config_path("userstate.cbor");
|
||||
debug!("saving user state to {}", path.display());
|
||||
if let Err(e) = TOML.write(path, self.state().clone()) {
|
||||
if let Err(e) = CBOR.write(path, self.state().clone()) {
|
||||
error!("Could not save user state: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,4 +68,42 @@ impl Serializer for TomlSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CborSerializer {}
|
||||
impl Serializer for CborSerializer {
|
||||
fn load<P: AsRef<Path>, T: serde::Serialize + serde::de::DeserializeOwned>(
|
||||
&self,
|
||||
path: P,
|
||||
) -> Result<T, String> {
|
||||
let contents = std::fs::read(&path)
|
||||
.map_err(|e| format!("Unable to read {}: {}", path.as_ref().to_string_lossy(), e))?;
|
||||
serde_cbor::from_slice(&contents).map_err(|e| {
|
||||
format!(
|
||||
"Unable to parse CBOR {}: {}",
|
||||
path.as_ref().to_string_lossy(),
|
||||
e
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn write<P: AsRef<Path>, T: serde::Serialize>(&self, path: P, value: T) -> Result<T, String> {
|
||||
let file = std::fs::File::create(&path).map_err(|e| {
|
||||
format!(
|
||||
"Failed creating file {}: {}",
|
||||
path.as_ref().to_string_lossy(),
|
||||
e
|
||||
)
|
||||
})?;
|
||||
serde_cbor::to_writer(file, &value)
|
||||
.map(|_| value)
|
||||
.map_err(|e| {
|
||||
format!(
|
||||
"Failed writing content to {}: {}",
|
||||
path.as_ref().display(),
|
||||
e
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub static TOML: TomlSerializer = TomlSerializer {};
|
||||
pub static CBOR: CborSerializer = CborSerializer {};
|
||||
|
||||
Reference in New Issue
Block a user