diff --git a/src/profile.rs b/src/profile.rs index 4abf6d7..32f3565 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use std::{ - fs::{create_dir_all, File}, + collections::HashMap, + fs::{create_dir_all, File, OpenOptions}, io::{BufReader, BufWriter}, path::Path, }; @@ -15,16 +16,17 @@ pub struct Profile { libsurvive_enabled: bool, basalt_enabled: bool, mercury_enabled: bool, + environment: HashMap, } -pub fn load_profile(path: String) -> Result { +pub fn load_profile(path: &String) -> Result { let file = File::open(path).unwrap(); let reader = BufReader::new(file); serde_json::from_reader(reader) } -pub fn dump_profile(profile: Profile, path_s: String) -> () { - let path = Path::new(&path_s); +pub fn dump_profile(profile: Profile, path_s: &String) -> () { + let path = Path::new(path_s); match path.parent() { Some(parent) => { if !parent.is_dir() { @@ -33,21 +35,24 @@ pub fn dump_profile(profile: Profile, path_s: String) -> () { } None => {} }; - let file = match path.is_file() { - true => File::open(path).expect("Could not open file"), - false => File::create(path).expect("Could not create file") - }; + let file = OpenOptions::new() + .write(true) + .create(true) + .open(path) + .expect("Could not open file"); let writer = BufWriter::new(file); serde_json::to_writer_pretty(writer, &profile).expect("Could not write profile") } #[cfg(test)] mod tests { - use super::{dump_profile, Profile, load_profile}; + use std::collections::HashMap; + + use super::{dump_profile, load_profile, Profile}; #[test] fn profile_can_be_loaded() { - let profile = load_profile(String::from("./test/files/profile.json")); + let profile = load_profile(&"./test/files/profile.json".to_string()); match profile { Ok(profile) => { assert_eq!(profile.monado_path, "/home/user/monado"); @@ -61,6 +66,13 @@ mod tests { assert_eq!(profile.libsurvive_enabled, true); assert_eq!(profile.basalt_enabled, false); assert_eq!(profile.mercury_enabled, false); + assert!(profile + .environment + .contains_key("XRT_COMPOSITOR_SCALE_PERCENTAGE")); + assert!(profile.environment.contains_key("XRT_COMPOSITOR_COMPUTE")); + assert!(profile + .environment + .contains_key("SURVIVE_GLOBALSCENESOLVER")); } Err(_) => assert!(false), } @@ -68,6 +80,12 @@ mod tests { #[test] fn profile_can_be_dumped() { + let mut env = HashMap::new(); + env.insert( + "XRT_COMPOSITOR_SCALE_PERCENTAGE".to_string(), + "140".to_string(), + ); + env.insert("XRT_COMPOSITOR_COMPUTE".to_string(), "1".to_string()); let p = Profile { monado_path: String::from("/home/user/monado"), openovr_path: String::from("/home/user/openovr"), @@ -77,7 +95,21 @@ mod tests { libsurvive_enabled: true, basalt_enabled: false, mercury_enabled: false, + environment: env, }; - dump_profile(p, String::from("./target/testout/testprofile.json")) + let fpath = String::from("./target/testout/testprofile.json"); + dump_profile(p, &fpath); + let loaded = load_profile(&fpath).unwrap(); + assert_eq!( + loaded.libsurvive_path, + Some(String::from("/home/user/libsurvive")) + ); + assert_eq!( + loaded + .environment + .get("XRT_COMPOSITOR_COMPUTE") + .expect("Key XRT_COMPOSITOR_COMPUTE not found"), + "1" + ); } } diff --git a/test/files/profile.json b/test/files/profile.json index 95636c4..31f99f5 100644 --- a/test/files/profile.json +++ b/test/files/profile.json @@ -6,5 +6,10 @@ "mercury_path": null, "libsurvive_enabled": true, "basalt_enabled": false, - "mercury_enabled": false + "mercury_enabled": false, + "environment": { + "XRT_COMPOSITOR_SCALE_PERCENTAGE": "140", + "XRT_COMPOSITOR_COMPUTE": "1", + "SURVIVE_GLOBALSCENESOLVER": "0" + } }