fix: remove checkerr macro, prefer using question mark operator and anyhow error

This commit is contained in:
Gabriele Musco 2024-01-20 22:38:06 +01:00
commit fa6e8ac56a
No known key found for this signature in database
GPG key ID: 1068D795C80E51DE
5 changed files with 25 additions and 36 deletions

View file

@ -1,8 +0,0 @@
#[macro_export]
macro_rules! checkerr {
($ex:expr) => {
if $ex.is_err() {
return Err(());
}
};
}

View file

@ -1,5 +1,4 @@
use crate::{ use crate::{
checkerr,
file_utils::{copy_file, deserialize_file, get_writer, 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, SYSTEM_PREFIX}, paths::{get_backup_dir, get_xdg_config_dir, get_xdg_data_dir, SYSTEM_PREFIX},
profile::{Profile, XRServiceType}, profile::{Profile, XRServiceType},
@ -97,9 +96,9 @@ fn build_steam_active_runtime() -> ActiveRuntime {
} }
} }
pub fn set_current_active_runtime_to_steam() -> Result<(), ()> { pub fn set_current_active_runtime_to_steam() -> anyhow::Result<()> {
checkerr!(set_file_readonly(&get_active_runtime_json_path(), false)); set_file_readonly(&get_active_runtime_json_path(), false)?;
checkerr!(dump_current_active_runtime(&build_steam_active_runtime())); dump_current_active_runtime(&build_steam_active_runtime())?;
Ok(()) Ok(())
} }
@ -156,9 +155,9 @@ fn relativize_active_runtime_lib_path(ar: &ActiveRuntime, dest: &str) -> ActiveR
res res
} }
pub fn set_current_active_runtime_to_profile(profile: &Profile) -> Result<(), ()> { pub fn set_current_active_runtime_to_profile(profile: &Profile) -> anyhow::Result<()> {
let dest = get_active_runtime_json_path(); let dest = get_active_runtime_json_path();
checkerr!(set_file_readonly(&dest, false)); set_file_readonly(&dest, false)?;
backup_steam_active_runtime(); backup_steam_active_runtime();
let pfx = profile.clone().prefix; let pfx = profile.clone().prefix;
let mut ar = build_profile_active_runtime(profile); let mut ar = build_profile_active_runtime(profile);
@ -166,8 +165,8 @@ pub fn set_current_active_runtime_to_profile(profile: &Profile) -> Result<(), ()
if pfx == SYSTEM_PREFIX { if pfx == SYSTEM_PREFIX {
ar = relativize_active_runtime_lib_path(&ar, &dest); ar = relativize_active_runtime_lib_path(&ar, &dest);
} }
checkerr!(dump_current_active_runtime(&ar)); dump_current_active_runtime(&ar)?;
checkerr!(set_file_readonly(&dest, true)); set_file_readonly(&dest, true)?;
Ok(()) Ok(())
} }

View file

@ -1,5 +1,4 @@
use crate::{ use crate::{
checkerr,
file_utils::{copy_file, deserialize_file, get_writer, 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}, paths::{get_backup_dir, get_xdg_config_dir, get_xdg_data_dir},
profile::Profile, profile::Profile,
@ -94,9 +93,9 @@ fn build_steam_openvrpaths() -> OpenVrPaths {
} }
} }
pub fn set_current_openvrpaths_to_steam() -> Result<(), ()> { pub fn set_current_openvrpaths_to_steam() -> anyhow::Result<()> {
checkerr!(set_file_readonly(&get_openvrpaths_vrpath_path(), false)); set_file_readonly(&get_openvrpaths_vrpath_path(), false)?;
checkerr!(dump_current_openvrpaths(&build_steam_openvrpaths())); dump_current_openvrpaths(&build_steam_openvrpaths())?;
Ok(()) Ok(())
} }
@ -115,14 +114,14 @@ pub fn build_profile_openvrpaths(profile: &Profile) -> OpenVrPaths {
} }
} }
pub fn set_current_openvrpaths_to_profile(profile: &Profile) -> Result<(), ()> { pub fn set_current_openvrpaths_to_profile(profile: &Profile) -> anyhow::Result<()> {
let dest = get_openvrpaths_vrpath_path(); let dest = get_openvrpaths_vrpath_path();
checkerr!(set_file_readonly(&dest, false)); set_file_readonly(&dest, false)?;
backup_steam_openvrpaths(); backup_steam_openvrpaths();
checkerr!(dump_current_openvrpaths(&build_profile_openvrpaths( dump_current_openvrpaths(&build_profile_openvrpaths(
profile profile
))); ))?;
checkerr!(set_file_readonly(&dest, true)); set_file_readonly(&dest, true)?;
Ok(()) Ok(())
} }

View file

@ -15,7 +15,6 @@ use ui::app::{App, AppInit};
pub mod adb; pub mod adb;
pub mod build_tools; pub mod build_tools;
pub mod builders; pub mod builders;
pub mod checkerr;
pub mod cmd_runner; pub mod cmd_runner;
pub mod config; pub mod config;
pub mod constants; pub mod constants;
@ -48,7 +47,7 @@ fn restore_steam_xr_files() {
if !file_builders::active_runtime_json::is_steam(&ar) { if !file_builders::active_runtime_json::is_steam(&ar) {
match set_current_active_runtime_to_steam() { match set_current_active_runtime_to_steam() {
Ok(_) => {} Ok(_) => {}
Err(_) => println!("Warning: failed to restore active runtime to steam!"), Err(e) => eprintln!("Warning: failed to restore active runtime to steam: {e}"),
}; };
} }
} }
@ -56,7 +55,7 @@ fn restore_steam_xr_files() {
if !file_builders::openvrpaths_vrpath::is_steam(&ovrp) { if !file_builders::openvrpaths_vrpath::is_steam(&ovrp) {
match set_current_openvrpaths_to_steam() { match set_current_openvrpaths_to_steam() {
Ok(_) => {} Ok(_) => {}
Err(_) => println!("Warning: failed to restore openvrpaths to steam!"), Err(e) => eprintln!("Warning: failed to restore openvrpaths to steam: {e}"),
}; };
} }
} }

View file

@ -162,18 +162,18 @@ impl App {
pub fn start_xrservice(&mut self, sender: ComponentSender<Self>, debug: bool) { pub fn start_xrservice(&mut self, sender: ComponentSender<Self>, debug: bool) {
let prof = self.get_selected_profile(); let prof = self.get_selected_profile();
if set_current_active_runtime_to_profile(&prof).is_err() { if let Err(e) = set_current_active_runtime_to_profile(&prof) {
alert( alert(
"Failed to start XR Service", "Failed to start XR Service",
Some("Error setting current active runtime to profile"), Some(&format!("Error setting current active runtime to profile: {e}")),
Some(&self.app_win.clone().upcast::<gtk::Window>()), Some(&self.app_win.clone().upcast::<gtk::Window>()),
); );
return; return;
} }
if set_current_openvrpaths_to_profile(&prof).is_err() { if let Err(e) = set_current_openvrpaths_to_profile(&prof) {
alert( alert(
"Failed to start XR Service", "Failed to start XR Service",
Some("Error setting current openvrpaths file to profile"), Some(&format!("Error setting current openvrpaths file to profile: {e}")),
Some(&self.app_win.clone().upcast::<gtk::Window>()), Some(&self.app_win.clone().upcast::<gtk::Window>()),
); );
return; return;
@ -218,17 +218,17 @@ impl App {
} }
pub fn restore_openxr_openvr_files(&self) { pub fn restore_openxr_openvr_files(&self) {
if set_current_active_runtime_to_steam().is_err() { if let Err(e) = set_current_active_runtime_to_steam() {
alert( alert(
"Could not restore Steam active runtime", "Could not restore Steam active runtime",
None, Some(&format!("{e}")),
Some(&self.app_win.clone().upcast::<gtk::Window>()), Some(&self.app_win.clone().upcast::<gtk::Window>()),
); );
} }
if set_current_openvrpaths_to_steam().is_err() { if let Err(e) = set_current_openvrpaths_to_steam() {
alert( alert(
"Could not restore Steam openvrpaths", "Could not restore Steam openvrpaths",
None, Some(&format!("{e}")),
Some(&self.app_win.clone().upcast::<gtk::Window>()), Some(&self.app_win.clone().upcast::<gtk::Window>()),
); );
}; };