From 0c3f6e98f5c25dae4b3f46f30760d4c49f959a19 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Mon, 29 Jul 2024 17:09:55 +0200 Subject: [PATCH] feat: detail profile validation failure --- src/profile.rs | 58 +++++++++++++++++++++++++++++++--------- src/ui/profile_editor.rs | 31 +++++++++++++++------ 2 files changed, 69 insertions(+), 20 deletions(-) diff --git a/src/profile.rs b/src/profile.rs index 3900336..9ad86b7 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -407,33 +407,67 @@ impl Profile { dup } - pub fn validate(&self) -> bool { - !self.name.is_empty() - && self.editable - && !self.uuid.is_empty() - && !self.xrservice_path.is_empty() - && !self.prefix.is_empty() - && (!self.features.libsurvive.enabled + pub fn validate(&self) -> Result<(), Vec<&str>> { + let mut errors = vec![]; + // impossible + if !self.editable { + errors.push("You somehow managed to edit a non-editable profile. Congratulations, you found a bug! Please report it."); + } + // impossible + if self.uuid.is_empty() { + errors.push("For some reason this profile's UUID is empty. Congratulations, you found a bug! Please report it."); + } + if self.name.is_empty() { + errors.push("The profile name cannot be empty."); + } + if self.xrservice_path.is_empty() { + errors.push("The XR service path cannot be empty.") + } + if self.prefix.is_empty() { + errors.push("The prefix path cannot be empty.") + } + if self.features.libsurvive.enabled + && (self.features.libsurvive.path.is_none() || self .features .libsurvive .path .as_ref() - .is_some_and(|p| !p.is_empty())) - && (!self.features.basalt.enabled + .is_some_and(|p| p.is_empty())) + { + errors.push("You enabled Libsurvive, but its path is empty. Disable Libsurvive or set a path for it.") + } + if self.features.basalt.enabled + && (self.features.basalt.path.is_none() || self .features .basalt .path .as_ref() - .is_some_and(|p| !p.is_empty())) - && (!self.features.openhmd.enabled + .is_some_and(|p| p.is_empty())) + { + errors.push( + "You enabled Basalt, but its path is empty. Disable Basalt or set a path for it.", + ) + } + if self.features.openhmd.enabled + && (self.features.openhmd.path.is_none() || self .features .openhmd .path .as_ref() - .is_some_and(|p| !p.is_empty())) + .is_some_and(|p| p.is_empty())) + { + errors.push( + "You enabled OpenHMD, but its path is empty. Disable OpenHMD or set a path for it.", + ) + } + if errors.is_empty() { + Ok(()) + } else { + Err(errors) + } } pub fn xrservice_binary(&self) -> PathBuf { diff --git a/src/ui/profile_editor.rs b/src/ui/profile_editor.rs index d9fee3b..b15dada 100644 --- a/src/ui/profile_editor.rs +++ b/src/ui/profile_editor.rs @@ -1,5 +1,5 @@ use super::{ - alert::alert, + alert::alert_w_widget, factories::env_var_row_factory::{EnvVarModel, EnvVarModelInit}, }; use crate::{ @@ -359,17 +359,32 @@ impl SimpleComponent for ProfileEditor { } Self::Input::SaveProfile => { let prof = self.profile.borrow(); - if prof.validate() { + if let Err(errors) = prof.validate() { + alert_w_widget( + "Profile validation failed", + None, + Some( + >k::Label::builder() + .label( + errors + .iter() + .map(|e| format!(" \u{2022} {e}")) + .collect::>() + .join("\n"), + ) + .wrap(true) + .xalign(0.0) + .max_width_chars(40) + .build() + .upcast(), + ), + Some(&self.parent), + ); + } else { sender .output(ProfileEditorOutMsg::SaveProfile(prof.clone())) .expect("Sender output failed"); self.win.as_ref().unwrap().close(); - } else { - alert( - "Profile validation failed", - Some("Check the values you set and try again"), - Some(&self.parent), - ); } } Self::Input::EnvVarChanged(name, value) => {