diff --git a/src/builders/build_mercury.rs b/src/builders/build_mercury.rs index 694052c..801e8a1 100644 --- a/src/builders/build_mercury.rs +++ b/src/builders/build_mercury.rs @@ -1,4 +1,4 @@ -use crate::{constants::pkg_data_dir, file_utils::get_cache_dir, profile::Profile, runner::Runner}; +use crate::{constants::pkg_data_dir, paths::get_cache_dir, profile::Profile, runner::Runner}; pub fn get_build_mercury_runner(profile: Profile) -> Runner { let args = vec![profile.prefix, get_cache_dir()]; diff --git a/src/config.rs b/src/config.rs index 7564b35..eb20881 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,4 @@ -use crate::{ - constants::CMD_NAME, - file_utils::{get_config_dir, get_writer}, profile::Profile, -}; +use crate::{constants::CMD_NAME, file_utils::get_writer, paths::get_config_dir, profile::Profile}; use serde::{Deserialize, Serialize}; use std::{fs::File, io::BufReader}; @@ -25,7 +22,10 @@ impl Default for Config { impl Config { pub fn get_selected_profile(&self, profiles: &Vec) -> Profile { - match profiles.iter().find(|p| {p.uuid == self.selected_profile_uuid}) { + match profiles + .iter() + .find(|p| p.uuid == self.selected_profile_uuid) + { Some(p) => p.clone(), None => profiles.get(0).expect("No profiles found").clone(), } @@ -58,7 +58,8 @@ impl Config { } pub fn save(&self) { - self.save_to_path(&Self::config_file_path()).expect("Failed to save config"); + self.save_to_path(&Self::config_file_path()) + .expect("Failed to save config"); } pub fn get_config() -> Self { @@ -66,7 +67,11 @@ impl Config { } pub fn set_profiles(&mut self, profiles: &Vec) { - self.user_profiles = profiles.iter().filter(|p| p.editable).map(Profile::clone).collect(); + self.user_profiles = profiles + .iter() + .filter(|p| p.editable) + .map(Profile::clone) + .collect(); } } diff --git a/src/constants.rs.in b/src/constants.rs.in index b96fb7f..8f8575e 100644 --- a/src/constants.rs.in +++ b/src/constants.rs.in @@ -1,4 +1,4 @@ -use crate::file_utils::get_exec_prefix; +use crate::paths::get_exec_prefix; pub const APP_NAME: &str = "@PRETTY_NAME@"; pub const APP_ID: &str = "@APP_ID@"; diff --git a/src/file_builders/active_runtime_json.rs b/src/file_builders/active_runtime_json.rs index 8b3cb07..977c237 100644 --- a/src/file_builders/active_runtime_json.rs +++ b/src/file_builders/active_runtime_json.rs @@ -1,9 +1,6 @@ use crate::{ - file_utils::{ - copy_file, deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, - get_xdg_data_dir, set_file_readonly, - }, - paths::SYSTEM_PREFIX, + file_utils::{copy_file, deserialize_file, get_writer, set_file_readonly}, + paths::{get_backup_dir, get_xdg_config_dir, get_xdg_data_dir, SYSTEM_PREFIX}, profile::{Profile, XRServiceType}, }; use serde::{Deserialize, Serialize}; @@ -74,8 +71,7 @@ pub fn get_current_active_runtime() -> Option { fn dump_active_runtime_to_path(active_runtime: &ActiveRuntime, path_s: &String) { let writer = get_writer(path_s); - serde_json::to_writer_pretty(writer, active_runtime) - .expect("Unable to save active runtime"); + serde_json::to_writer_pretty(writer, active_runtime).expect("Unable to save active runtime"); } pub fn dump_current_active_runtime(active_runtime: &ActiveRuntime) { diff --git a/src/file_builders/monado_autorun.rs b/src/file_builders/monado_autorun.rs index 794bea6..e60ae80 100644 --- a/src/file_builders/monado_autorun.rs +++ b/src/file_builders/monado_autorun.rs @@ -1,7 +1,9 @@ +use crate::{ + file_utils::{deserialize_file, get_writer}, + paths::get_xdg_config_dir, +}; use serde::{Deserialize, Serialize}; -use crate::file_utils::{deserialize_file, get_writer, get_xdg_config_dir}; - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct MonadoAutorun { pub exec: String, @@ -44,8 +46,7 @@ pub fn get_monado_autorun_config() -> MonadoAutorunConfig { fn dump_monado_autorun_config_to_path(config: &MonadoAutorunConfig, path_s: &String) { let writer = get_writer(path_s); - serde_json::to_writer_pretty(writer, config) - .expect("Unable to save Monado Autorun config"); + serde_json::to_writer_pretty(writer, config).expect("Unable to save Monado Autorun config"); } pub fn dump_monado_autorun_config(config: &MonadoAutorunConfig) { diff --git a/src/file_builders/openvrpaths_vrpath.rs b/src/file_builders/openvrpaths_vrpath.rs index 810ea1f..fadf8a1 100644 --- a/src/file_builders/openvrpaths_vrpath.rs +++ b/src/file_builders/openvrpaths_vrpath.rs @@ -1,8 +1,6 @@ use crate::{ - file_utils::{ - copy_file, deserialize_file, get_backup_dir, get_writer, get_xdg_config_dir, - get_xdg_data_dir, set_file_readonly, - }, + file_utils::{copy_file, deserialize_file, get_writer, set_file_readonly}, + paths::{get_backup_dir, get_xdg_config_dir, get_xdg_data_dir}, profile::Profile, }; use serde::{Deserialize, Serialize}; diff --git a/src/file_builders/wivrn_config.rs b/src/file_builders/wivrn_config.rs index dfea7ed..1802e97 100644 --- a/src/file_builders/wivrn_config.rs +++ b/src/file_builders/wivrn_config.rs @@ -1,4 +1,7 @@ -use crate::file_utils::{deserialize_file, get_writer, get_xdg_config_dir}; +use crate::{ + file_utils::{deserialize_file, get_writer}, + paths::get_xdg_config_dir, +}; use serde::{Deserialize, Serialize}; use std::{fmt::Display, slice::Iter}; diff --git a/src/file_utils.rs b/src/file_utils.rs index 6a6ce57..b4614ad 100644 --- a/src/file_utils.rs +++ b/src/file_utils.rs @@ -52,70 +52,6 @@ pub fn deserialize_file(path_s: &String) -> Opti } } -pub fn get_home_dir() -> String { - env::var("HOME").expect("HOME env var not defined") -} - -pub fn get_xdg_config_dir() -> String { - match env::var("XDG_CONFIG_HOME") { - Ok(conf_home) => conf_home, - Err(_) => format!("{home}/.config", home = get_home_dir()), - } -} - -pub fn get_xdg_data_dir() -> String { - match env::var("XDG_DATA_HOME") { - Ok(data_home) => data_home, - Err(_) => format!("{home}/.local/share", home = get_home_dir()), - } -} - -pub fn get_xdg_cache_dir() -> String { - match env::var("XDG_CACHE_HOME") { - Ok(cache_home) => cache_home, - Err(_) => format!("{home}/.cache", home = get_home_dir()), - } -} - -pub fn get_xdg_runtime_dir() -> String { - env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR is not set") -} - -pub fn get_config_dir() -> String { - format!( - "{config}/{name}", - config = get_xdg_config_dir(), - name = CMD_NAME - ) -} - -pub fn get_data_dir() -> String { - format!("{data}/{name}", data = get_xdg_data_dir(), name = CMD_NAME) -} - -pub fn get_cache_dir() -> String { - format!( - "{cache}/{name}", - cache = get_xdg_cache_dir(), - name = CMD_NAME - ) -} - -pub fn get_backup_dir() -> String { - let p_s = format!("{data}/backups", data = get_data_dir()); - let p = Path::new(&p_s); - if !p.is_dir() { - if p.is_file() { - panic!( - "{} is a file but it should be a directory!", - p.to_str().unwrap() - ); - } - create_dir_all(p).expect("Failed to create backups dir"); - } - p.to_str().unwrap().to_string() -} - pub fn set_file_readonly(path_s: &String, readonly: bool) { let path = Path::new(&path_s); if !path.is_file() { @@ -139,23 +75,6 @@ pub fn setcap_cap_sys_nice_eip(file: String) { runner.join(); } -pub fn get_exec_prefix() -> String { - let p = Path::new("/proc/self/exe"); - if !p.is_symlink() { - panic!("/proc/self/exe is not a symlink!"); - } - p.read_link() - .unwrap() - .as_path() - .parent() - .unwrap() - .parent() - .unwrap() - .to_str() - .unwrap() - .to_string() -} - pub fn rm_rf(path_s: &String) { match remove_dir_all(path_s) { Err(_) => println!("Failed to remove path {}", path_s), diff --git a/src/paths.rs b/src/paths.rs index 62a8fc7..618b83e 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -1,4 +1,5 @@ -use crate::file_utils::{get_data_dir, get_cache_dir}; +use std::{env, path::Path, fs::create_dir_all}; +use crate::constants::CMD_NAME; pub fn data_opencomposite_path() -> String { format!("{data}/opencomposite", data = get_data_dir()) @@ -24,3 +25,84 @@ pub const SYSTEM_PREFIX: &str = "/usr"; /** System prefix inside a bubblewrap environment (flatpak or pressure vessel) */ pub const BWRAP_SYSTEM_PREFIX: &str = "/run/host/usr"; + +pub fn get_home_dir() -> String { + env::var("HOME").expect("HOME env var not defined") +} + +pub fn get_xdg_config_dir() -> String { + match env::var("XDG_CONFIG_HOME") { + Ok(conf_home) => conf_home, + Err(_) => format!("{home}/.config", home = get_home_dir()), + } +} + +pub fn get_xdg_data_dir() -> String { + match env::var("XDG_DATA_HOME") { + Ok(data_home) => data_home, + Err(_) => format!("{home}/.local/share", home = get_home_dir()), + } +} + +pub fn get_xdg_cache_dir() -> String { + match env::var("XDG_CACHE_HOME") { + Ok(cache_home) => cache_home, + Err(_) => format!("{home}/.cache", home = get_home_dir()), + } +} + +pub fn get_xdg_runtime_dir() -> String { + env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR is not set") +} + +pub fn get_config_dir() -> String { + format!( + "{config}/{name}", + config = get_xdg_config_dir(), + name = CMD_NAME + ) +} + +pub fn get_data_dir() -> String { + format!("{data}/{name}", data = get_xdg_data_dir(), name = CMD_NAME) +} + +pub fn get_cache_dir() -> String { + format!( + "{cache}/{name}", + cache = get_xdg_cache_dir(), + name = CMD_NAME + ) +} + +pub fn get_backup_dir() -> String { + let p_s = format!("{data}/backups", data = get_data_dir()); + let p = Path::new(&p_s); + if !p.is_dir() { + if p.is_file() { + panic!( + "{} is a file but it should be a directory!", + p.to_str().unwrap() + ); + } + create_dir_all(p).expect("Failed to create backups dir"); + } + p.to_str().unwrap().to_string() +} + +pub fn get_exec_prefix() -> String { + let p = Path::new("/proc/self/exe"); + if !p.is_symlink() { + panic!("/proc/self/exe is not a symlink!"); + } + p.read_link() + .unwrap() + .as_path() + .parent() + .unwrap() + .parent() + .unwrap() + .to_str() + .unwrap() + .to_string() +} diff --git a/src/profile.rs b/src/profile.rs index d442c69..26149fd 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -1,9 +1,11 @@ use crate::{ - file_utils::{get_data_dir, get_writer}, - paths::{data_monado_path, data_opencomposite_path, BWRAP_SYSTEM_PREFIX, SYSTEM_PREFIX}, + file_utils::get_writer, + paths::{ + data_monado_path, data_opencomposite_path, get_data_dir, BWRAP_SYSTEM_PREFIX, SYSTEM_PREFIX, + }, }; use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, fmt::Display, fs::File, io::BufReader, slice::Iter, path::Path}; +use std::{collections::HashMap, fmt::Display, fs::File, io::BufReader, path::Path, slice::Iter}; use uuid::Uuid; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -201,10 +203,7 @@ impl Profile { } pub fn get_survive_cli_path(&self) -> Option { - let path_s = format!( - "{pfx}/bin/survive-cli", - pfx = self.prefix - ); + let path_s = format!("{pfx}/bin/survive-cli", pfx = self.prefix); if Path::new(&path_s).is_file() { return Some(path_s); } diff --git a/src/profiles/valve_index.rs b/src/profiles/valve_index.rs index a403835..a5507ce 100644 --- a/src/profiles/valve_index.rs +++ b/src/profiles/valve_index.rs @@ -1,7 +1,6 @@ use crate::{ constants::APP_NAME, - file_utils::get_data_dir, - paths::{data_libsurvive_path, data_monado_path, data_opencomposite_path}, + paths::{data_libsurvive_path, data_monado_path, data_opencomposite_path, get_data_dir}, profile::{Profile, ProfileFeature, ProfileFeatureType, ProfileFeatures, XRServiceType}, }; use std::collections::HashMap; @@ -15,7 +14,10 @@ pub fn valve_index_profile() -> Profile { environment.insert("XRT_COMPOSITOR_COMPUTE".into(), "1".into()); environment.insert("SURVIVE_GLOBALSCENESOLVER".into(), "0".into()); environment.insert("SURVIVE_TIMECODE_OFFSET_MS".into(), "-6.94".into()); - environment.insert("LD_LIBRARY_PATH".into(), format!("{pfx}/lib:{pfx}/lib64", pfx = prefix)); + environment.insert( + "LD_LIBRARY_PATH".into(), + format!("{pfx}/lib:{pfx}/lib64", pfx = prefix), + ); Profile { uuid: "valve-index-default".into(), name: format!("Valve Index - {name} Default", name = APP_NAME), diff --git a/src/profiles/wivrn.rs b/src/profiles/wivrn.rs index c2a6712..7a6a6fa 100644 --- a/src/profiles/wivrn.rs +++ b/src/profiles/wivrn.rs @@ -1,7 +1,6 @@ use crate::{ constants::APP_NAME, - file_utils::get_data_dir, - paths::{data_opencomposite_path, data_wivrn_path}, + paths::{data_opencomposite_path, data_wivrn_path, get_data_dir}, profile::{Profile, ProfileFeatures, XRServiceType}, }; use std::collections::HashMap; @@ -10,7 +9,10 @@ pub fn wivrn_profile() -> Profile { let data_dir = get_data_dir(); let prefix = format!("{data}/prefixes/wivrn_default", data = data_dir); let mut environment: HashMap = HashMap::new(); - environment.insert("LD_LIBRARY_PATH".into(), format!("{pfx}/lib:{pfx}/lib64", pfx = prefix)); + environment.insert( + "LD_LIBRARY_PATH".into(), + format!("{pfx}/lib:{pfx}/lib64", pfx = prefix), + ); Profile { uuid: "wivrn-default".into(), name: format!("WiVRn - {name} Default", name = APP_NAME),