Skip to main content

qualia_client_core/wellfair/
accessibility_prefs.rs

1//! Owner accessibility preferences — persisted under storage root.
2
3use std::fs;
4use std::path::Path;
5
6use super::host_state::AccessibilityPreferences;
7
8pub const PREFS_FILE: &str = "wellfair/accessibility.json";
9
10/// Loads.
11pub fn load(storage_root: impl AsRef<Path>) -> AccessibilityPreferences {
12    let path = storage_root.as_ref().join(PREFS_FILE);
13    if !path.exists() {
14        return AccessibilityPreferences::default();
15    }
16    match fs::read_to_string(&path) {
17        Ok(text) => serde_json::from_str(&text).unwrap_or_default(),
18        Err(_) => AccessibilityPreferences::default(),
19    }
20}
21
22/// Saves.
23pub fn save(
24    storage_root: impl AsRef<Path>,
25    prefs: &AccessibilityPreferences,
26) -> std::io::Result<()> {
27    let path = storage_root.as_ref().join(PREFS_FILE);
28    if let Some(parent) = path.parent() {
29        fs::create_dir_all(parent)?;
30    }
31    let text =
32        serde_json::to_string_pretty(prefs).map_err(|e| std::io::Error::other(e.to_string()))?;
33    fs::write(&path, text)?;
34    Ok(())
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn accessibility_round_trip() {
43        let dir = tempfile::tempdir().unwrap();
44        let prefs = AccessibilityPreferences {
45            high_contrast: true,
46            reduced_motion: true,
47            text_scale_percent: 125,
48            screen_reader_hints: false,
49        };
50        save(dir.path(), &prefs).unwrap();
51        let loaded = load(dir.path());
52        assert_eq!(loaded, prefs);
53    }
54}