feat: detail profile validation failure

This commit is contained in:
Gabriele Musco 2024-07-29 17:09:55 +02:00
commit 0c3f6e98f5
2 changed files with 69 additions and 20 deletions

View file

@ -407,33 +407,67 @@ impl Profile {
dup dup
} }
pub fn validate(&self) -> bool { pub fn validate(&self) -> Result<(), Vec<&str>> {
!self.name.is_empty() let mut errors = vec![];
&& self.editable // impossible
&& !self.uuid.is_empty() if !self.editable {
&& !self.xrservice_path.is_empty() errors.push("You somehow managed to edit a non-editable profile. Congratulations, you found a bug! Please report it.");
&& !self.prefix.is_empty() }
&& (!self.features.libsurvive.enabled // 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 || self
.features .features
.libsurvive .libsurvive
.path .path
.as_ref() .as_ref()
.is_some_and(|p| !p.is_empty())) .is_some_and(|p| p.is_empty()))
&& (!self.features.basalt.enabled {
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 || self
.features .features
.basalt .basalt
.path .path
.as_ref() .as_ref()
.is_some_and(|p| !p.is_empty())) .is_some_and(|p| p.is_empty()))
&& (!self.features.openhmd.enabled {
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 || self
.features .features
.openhmd .openhmd
.path .path
.as_ref() .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 { pub fn xrservice_binary(&self) -> PathBuf {

View file

@ -1,5 +1,5 @@
use super::{ use super::{
alert::alert, alert::alert_w_widget,
factories::env_var_row_factory::{EnvVarModel, EnvVarModelInit}, factories::env_var_row_factory::{EnvVarModel, EnvVarModelInit},
}; };
use crate::{ use crate::{
@ -359,17 +359,32 @@ impl SimpleComponent for ProfileEditor {
} }
Self::Input::SaveProfile => { Self::Input::SaveProfile => {
let prof = self.profile.borrow(); let prof = self.profile.borrow();
if prof.validate() { if let Err(errors) = prof.validate() {
alert_w_widget(
"Profile validation failed",
None,
Some(
&gtk::Label::builder()
.label(
errors
.iter()
.map(|e| format!(" \u{2022} {e}"))
.collect::<Vec<String>>()
.join("\n"),
)
.wrap(true)
.xalign(0.0)
.max_width_chars(40)
.build()
.upcast(),
),
Some(&self.parent),
);
} else {
sender sender
.output(ProfileEditorOutMsg::SaveProfile(prof.clone())) .output(ProfileEditorOutMsg::SaveProfile(prof.clone()))
.expect("Sender output failed"); .expect("Sender output failed");
self.win.as_ref().unwrap().close(); 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) => { Self::Input::EnvVarChanged(name, value) => {