Files
furumi-tui/integrations/opendeck/src/settings.rs
T

160 lines
5.5 KiB
Rust

//! Preferences are global to the OpenDeck plugin, not to an action instance.
//! The native runtime is the sole writer, merging validated inspector patches
//! into the canonical object while preserving unknown fields.
use serde_json::{Map, Value};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum InfobarBackground {
#[default]
None,
Spectrogram,
Bars,
}
impl InfobarBackground {
pub fn uses_spectrum(self) -> bool {
matches!(self, Self::Spectrogram | Self::Bars)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Preferences {
pub infobar_background: InfobarBackground,
pub album_art_2x2: bool,
pub invert_colors: bool,
}
impl Preferences {
pub fn read(value: &Value) -> Self {
Self {
infobar_background: match value["infobar_background"].as_str() {
Some("spectrogram") => InfobarBackground::Spectrogram,
Some("bars") => InfobarBackground::Bars,
_ => InfobarBackground::None,
},
album_art_2x2: value["album_art_2x2"].as_bool().unwrap_or(false),
invert_colors: value["invert_colors"].as_bool().unwrap_or(false),
}
}
}
#[derive(Default)]
pub struct Store {
raw: Map<String, Value>,
ready: bool,
queued: Map<String, Value>,
}
impl Store {
pub fn raw_value(&self) -> Option<Value> {
self.ready.then(|| Value::Object(self.raw.clone()))
}
pub fn preferences(&self) -> Preferences {
Preferences::read(&Value::Object(self.raw.clone()))
}
pub fn receive(&mut self, value: &Value) -> Option<Value> {
self.raw = value.as_object().cloned().unwrap_or_default();
self.ready = true;
let changed = !self.queued.is_empty();
self.raw.extend(std::mem::take(&mut self.queued));
changed.then(|| Value::Object(self.raw.clone()))
}
pub fn patch(&mut self, value: &Value) -> Option<Value> {
let patch = value.as_object()?;
if patch.is_empty()
|| patch.len() > 3
|| !patch.iter().all(|(key, value)| match key.as_str() {
"infobar_background" => {
matches!(value.as_str(), Some("none" | "spectrogram" | "bars"))
}
"album_art_2x2" | "invert_colors" => value.is_boolean(),
_ => false,
})
{
return None;
}
if !self.ready {
self.queued.extend(patch.clone());
return None;
}
self.raw.extend(patch.clone());
Some(Value::Object(self.raw.clone()))
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn bars_round_trips_and_old_or_unknown_modes_keep_safe_defaults() {
let mut store = Store::default();
store.receive(&json!({"future":42}));
let value = store.patch(&json!({"infobar_background":"bars"})).unwrap();
assert_eq!(value["future"], 42);
assert_eq!(
store.preferences().infobar_background,
InfobarBackground::Bars
);
assert!(InfobarBackground::Bars.uses_spectrum());
assert!(InfobarBackground::Spectrogram.uses_spectrum());
assert!(!InfobarBackground::None.uses_spectrum());
assert!(
store
.patch(&json!({"infobar_background":"invalid"}))
.is_none()
);
assert_eq!(
Preferences::read(&json!({})).infobar_background,
InfobarBackground::None
);
}
#[test]
fn defaults_and_unknown_preferences_are_safe_and_nondestructive() {
assert_eq!(Preferences::read(&json!({})), Preferences::default());
let source = json!({"infobar_background":"spectrogram","album_art_2x2":true,"invert_colors":true,"future":{"keep":42}});
let before = source.clone();
let preferences = Preferences::read(&source);
assert_eq!(
preferences.infobar_background,
InfobarBackground::Spectrogram
);
assert!(preferences.album_art_2x2 && preferences.invert_colors);
assert_eq!(source, before);
assert_eq!(
Preferences::read(
&json!({"album_art_2x2":"yes","invert_colors":1,"infobar_background":"future"})
),
Preferences::default()
);
}
#[test]
fn two_inspectors_merge_different_fields_without_discarding_unknown_settings() {
let mut store = Store::default();
store.receive(&json!({"future":{"keep":42}}));
let a = store.patch(&json!({"album_art_2x2":true})).unwrap();
let b = store.patch(&json!({"invert_colors":true})).unwrap();
assert_eq!(a["album_art_2x2"], true);
assert_eq!(b["album_art_2x2"], true);
assert_eq!(b["invert_colors"], true);
assert_eq!(b["future"]["keep"], 42);
assert!(store.patch(&json!({"future":false})).is_none());
assert!(store.patch(&json!({"invert_colors":"yes"})).is_none());
}
#[test]
fn patches_before_initial_host_read_are_queued_then_merged_with_unknown_fields() {
let mut store = Store::default();
assert!(store.patch(&json!({"album_art_2x2":true})).is_none());
assert!(store.patch(&json!({"invert_colors":true})).is_none());
let merged = store.receive(&json!({"future":"preserve"})).unwrap();
assert_eq!(merged["future"], "preserve");
assert_eq!(merged["album_art_2x2"], true);
assert_eq!(merged["invert_colors"], true);
}
}