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