From af5c57f0f8e4131585ab1fa44fc22f19a87d17ea Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Mon, 18 Nov 2024 13:39:11 +0100 Subject: [PATCH] feat: split cmake var into name and value when it contains = --- src/ui/profile_editor.rs | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/ui/profile_editor.rs b/src/ui/profile_editor.rs index 9cfca7a..348df30 100644 --- a/src/ui/profile_editor.rs +++ b/src/ui/profile_editor.rs @@ -48,6 +48,21 @@ pub struct ProfileEditorInit { pub profile: Profile, } +/// This parses a var (either env var or cmake var) and if it contains +/// a '=' char, it assumes it's key and value, otherwise it's just going to +/// be the key and the value is gonna be an empty string +fn parse_var(input: &str) -> (String, String) { + if input.contains('=') { + let mut sp = input.split('='); + ( + sp.next().unwrap().to_string(), + sp.next().unwrap().to_string(), + ) + } else { + (input.to_string(), "".to_string()) + } +} + #[relm4::component(pub)] impl SimpleComponent for ProfileEditor { type Init = ProfileEditorInit; @@ -428,33 +443,23 @@ impl SimpleComponent for ProfileEditor { } Self::Input::AddEnvVar(var) => { let mut prof = self.profile.borrow_mut(); - if !prof.environment.contains_key(&var) { - let (name, value) = if var.contains('=') { - let mut sp = var.split('='); - ( - sp.next().unwrap().to_string(), - sp.next().unwrap().to_string(), - ) - } else { - (var, "".to_string()) - }; + let (name, value) = parse_var(&var); + if !prof.environment.contains_key(&name) { prof.environment.insert(name.clone(), value.clone()); self.env_rows .guard() .push_back(EnvVarModelInit { name, value }); } } - Self::Input::AddXrServiceCmakeFlag(name) => { + Self::Input::AddXrServiceCmakeFlag(var) => { let mut prof = self.profile.borrow_mut(); + let (name, value) = parse_var(&var); if !prof.xrservice_cmake_flags.contains_key(&name) { prof.xrservice_cmake_flags - .insert(name.clone(), "".to_string()); + .insert(name.clone(), value.clone()); self.xrservice_cmake_flags_rows .guard() - .push_back(EnvVarModelInit { - name, - value: "".to_string(), - }); + .push_back(EnvVarModelInit { name, value }); } } }