diff --git a/src/file_builders/active_runtime_json.rs b/src/file_builders/active_runtime_json.rs index 26be062..f7f7873 100644 --- a/src/file_builders/active_runtime_json.rs +++ b/src/file_builders/active_runtime_json.rs @@ -1,7 +1,13 @@ -use std::{fs::File, io::BufReader, path::Path}; +use crate::{ + file_utils::{ + deserialize_file, get_writer, get_xdg_config_dir, get_xdg_data_dir, set_file_radonly, + }, + paths::SYSTEM_PREFIX, + profile::Profile, +}; use expect_dialog::ExpectDialog; use serde::{Deserialize, Serialize}; -use crate::{file_utils::{get_xdg_config_dir, get_xdg_data_dir, get_writer, set_file_radonly}, profile::Profile, paths::SYSTEM_PREFIX}; +use std::path::Path; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ActiveRuntimeInnerRuntime { @@ -31,18 +37,12 @@ pub fn is_steam(active_runtime: ActiveRuntime) -> bool { } } -fn get_active_runtime_from_path(path_s: String) -> Option { - let path = Path::new(&path_s); - if !(path.is_file() || path.is_symlink()) { - return None; - } - let fd = File::open(path).expect_dialog("Unable to open active_runtime.json"); - let reader = BufReader::new(fd); - Some(serde_json::from_reader(reader).expect_dialog("Failed to deserialize active_runtime.json")) +fn get_active_runtime_from_path(path_s: &String) -> Option { + deserialize_file(path_s) } pub fn get_current_active_runtime() -> Option { - get_active_runtime_from_path(get_active_runtime_json_path()) + get_active_runtime_from_path(&get_active_runtime_json_path()) } fn dump_active_runtime_to_path(active_runtime: ActiveRuntime, path_s: String) { @@ -81,10 +81,7 @@ fn build_profile_active_runtime(profile: Profile) -> ActiveRuntime { runtime: ActiveRuntimeInnerRuntime { name: None, valve_runtime_is_steamvr: None, - library_path: format!( - "{prefix}/lib/libopenxr_monado.so", - prefix = profile.prefix - ), + library_path: format!("{prefix}/lib/libopenxr_monado.so", prefix = profile.prefix), }, } } @@ -96,10 +93,16 @@ pub fn set_current_active_runtime_to_profile(profile: Profile) { if pfx == SYSTEM_PREFIX { let path_s = get_active_runtime_json_path(); let path = Path::new(&path_s); - let mut rel_chain = path.components().map(|c| String::from("..")).collect::>(); + let mut rel_chain = path + .components() + .map(|_| String::from("..")) + .collect::>(); rel_chain.pop(); rel_chain.pop(); - ar.runtime.library_path = format!("{rels}/usr/lib/libopenxr_monado.so", rels = rel_chain.join("/")); + ar.runtime.library_path = format!( + "{rels}/usr/lib/libopenxr_monado.so", + rels = rel_chain.join("/") + ); } dump_current_active_runtime(ar); } @@ -113,7 +116,7 @@ mod tests { #[test] fn can_read_active_runtime_json_steamvr() { - let ar = get_active_runtime_from_path("./test/files/active_runtime.json.steamvr".into()) + let ar = get_active_runtime_from_path(&"./test/files/active_runtime.json.steamvr".into()) .unwrap(); assert_eq!(ar.file_format_version, "1.0.0"); assert!(ar.runtime.valve_runtime_is_steamvr.unwrap()); diff --git a/src/file_builders/openvrpaths_vrpath.rs b/src/file_builders/openvrpaths_vrpath.rs index b6ebcdb..a791db5 100644 --- a/src/file_builders/openvrpaths_vrpath.rs +++ b/src/file_builders/openvrpaths_vrpath.rs @@ -1,10 +1,12 @@ -use std::{fs::File, io::BufReader, path::Path}; - +use crate::{ + file_utils::{ + deserialize_file, get_writer, get_xdg_config_dir, get_xdg_data_dir, set_file_radonly, + }, + profile::Profile, +}; use expect_dialog::ExpectDialog; use serde::{Deserialize, Serialize}; -use crate::{file_utils::{get_xdg_config_dir, get_xdg_data_dir, get_writer, set_file_radonly}, profile::Profile}; - #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct OpenVrPaths { config: Vec, @@ -33,18 +35,12 @@ pub fn is_steam(ovr_paths: OpenVrPaths) -> bool { .is_some() } -fn get_openvrpaths_from_path(path_s: String) -> Option { - let path = Path::new(&path_s); - if !(path.is_file() || path.is_symlink()) { - return None; - } - let fd = File::open(path).expect_dialog("Unable to open openvrpaths.vrpath"); - let reader = BufReader::new(fd); - Some(serde_json::from_reader(reader).expect_dialog("Failed to deserialize openvrpaths.vrpath")) +fn get_openvrpaths_from_path(path_s: &String) -> Option { + deserialize_file(path_s) } pub fn get_current_openvrpaths() -> Option { - get_openvrpaths_from_path(get_openvrpaths_vrpath_path()) + get_openvrpaths_from_path(&get_openvrpaths_vrpath_path()) } fn dump_openvrpaths_to_path(ovr_paths: OpenVrPaths, path_s: String) { @@ -65,7 +61,10 @@ fn build_steam_openvrpaths() -> OpenVrPaths { external_drivers: None, jsonid: "vrpathreg".into(), log: vec![format!("{data}/Steam/logs", data = datadir)], - runtime: vec![format!("{data}/Steam/steamapps/common/SteamVR", data = datadir)], + runtime: vec![format!( + "{data}/Steam/steamapps/common/SteamVR", + data = datadir + )], version: 1, } } @@ -81,7 +80,10 @@ fn build_profile_openvrpaths(profile: Profile) -> OpenVrPaths { external_drivers: None, jsonid: "vrpathreg".into(), log: vec![format!("{data}/Steam/logs", data = datadir)], - runtime: vec![format!("{opencomp_dir}/build", opencomp_dir = profile.opencomposite_path)], + runtime: vec![format!( + "{opencomp_dir}/build", + opencomp_dir = profile.opencomposite_path + )], version: 1, } } @@ -96,7 +98,7 @@ mod tests { #[test] fn can_read_openvrpaths_vrpath_steamvr() { - let ovrp = get_openvrpaths_from_path("./test/files/openvrpaths.vrpath".into()).unwrap(); + let ovrp = get_openvrpaths_from_path(&"./test/files/openvrpaths.vrpath".into()).unwrap(); assert_eq!(ovrp.config.len(), 1); assert_eq!( ovrp.config.get(0).unwrap(), diff --git a/src/file_utils.rs b/src/file_utils.rs index 17e1bf7..9e7ca65 100644 --- a/src/file_utils.rs +++ b/src/file_utils.rs @@ -2,8 +2,8 @@ use crate::{constants::CMD_NAME, runner::Runner}; use expect_dialog::ExpectDialog; use std::{ env, - fs::{self, create_dir_all, OpenOptions}, - io::BufWriter, + fs::{self, create_dir_all, OpenOptions, File}, + io::{BufWriter, BufReader}, path::Path, }; @@ -26,6 +26,37 @@ pub fn get_writer(path_s: &String) -> BufWriter { BufWriter::new(file) } +pub fn get_reader(path_s: &String) -> Option> { + let path = Path::new(&path_s); + if !(path.is_file() || path.is_symlink()) { + return None; + } + match File::open(path) { + Err(e) => { + println!("Error opening {}: {}", path_s, e); + return None; + } + Ok(fd) => { + Some(BufReader::new(fd)) + } + } +} + +pub fn deserialize_file(path_s: &String) -> Option { + match get_reader(&path_s) { + None => None, + Some(reader) => { + match serde_json::from_reader(reader) { + Err(e) => { + println!("Failed to deserialize {}: {}", path_s, e); + None + } + Ok(res) => Some(res) + } + } + } +} + pub fn get_home_dir() -> String { env::var("HOME").expect_dialog("HOME env var not defined") }