diff --git a/crates/js_api/src/gui/mod.rs b/crates/js_api/src/gui/mod.rs index 2f87c83e8..cdaf5b03b 100644 --- a/crates/js_api/src/gui/mod.rs +++ b/crates/js_api/src/gui/mod.rs @@ -87,7 +87,7 @@ impl DotrainOrderGui { let gui = self .dotrain_order .dotrain_yaml() - .get_gui()? + .get_gui(Some(self.selected_deployment.clone()))? .ok_or(GuiError::GuiConfigNotFound)?; Ok(gui) } diff --git a/crates/settings/src/deployment.rs b/crates/settings/src/deployment.rs index f7ad9a202..f2140abff 100644 --- a/crates/settings/src/deployment.rs +++ b/crates/settings/src/deployment.rs @@ -8,7 +8,8 @@ use strict_yaml_rust::StrictYaml; use thiserror::Error; use typeshare::typeshare; use yaml::{ - context::Context, default_document, require_hash, require_string, YamlError, YamlParsableHash, + context::{Context, GuiContextTrait}, + default_document, require_hash, require_string, YamlError, YamlParsableHash, }; #[cfg(target_family = "wasm")] @@ -59,8 +60,6 @@ impl YamlParsableHash for Deployment { ) -> Result, YamlError> { let mut deployments = HashMap::new(); - let orders = Order::parse_all_from_yaml(documents.clone(), context)?; - for document in &documents { let document_read = document.read().map_err(|_| YamlError::ReadLockError)?; @@ -68,6 +67,16 @@ impl YamlParsableHash for Deployment { for (key_yaml, deployment_yaml) in deployments_hash { let deployment_key = key_yaml.as_str().unwrap_or_default().to_string(); + if let Some(context) = context { + if let Some(current_deployment) = context.get_current_deployment() { + if current_deployment != &deployment_key { + continue; + } + } + } + + let mut context = Context::from_context(context); + let order_key = require_string( deployment_yaml, Some("order"), @@ -75,12 +84,10 @@ impl YamlParsableHash for Deployment { "order string missing in deployment: {deployment_key}" )), )?; - let order = orders - .get(&order_key) - .ok_or_else(|| YamlError::KeyNotFound(order_key.clone()))? - .clone(); + context.add_current_order(order_key.clone()); - let mut context = Context::new(); + let order = + Order::parse_from_yaml(documents.clone(), &order_key, Some(&context))?; context.add_order(Arc::new(order.clone())); let scenario = Scenario::parse_from_yaml( diff --git a/crates/settings/src/gui.rs b/crates/settings/src/gui.rs index 9a3dc567f..77f1b7a95 100644 --- a/crates/settings/src/gui.rs +++ b/crates/settings/src/gui.rs @@ -1,7 +1,8 @@ use crate::{ yaml::{ - context::Context, default_document, get_hash_value, optional_hash, optional_string, - optional_vec, require_string, require_vec, YamlError, YamlParsableHash, YamlParseableValue, + context::{Context, GuiContextTrait}, + default_document, get_hash_value, optional_hash, optional_string, optional_vec, + require_string, require_vec, YamlError, YamlParsableHash, YamlParseableValue, }, Deployment, Token, TokenRef, }; @@ -349,7 +350,7 @@ impl YamlParseableValue for Gui { fn parse_from_yaml_optional( documents: Vec>>, - _: Option<&Context>, + context: Option<&Context>, ) -> Result, YamlError> { let mut gui_res: Option = None; let mut gui_deployments_res: HashMap = HashMap::new(); @@ -397,7 +398,15 @@ impl YamlParseableValue for Gui { for (deployment_name, deployment_yaml) in deployments { let deployment_name = deployment_name.as_str().unwrap_or_default().to_string(); - let mut context = Context::new(); + if let Some(context) = context { + if let Some(current_deployment) = context.get_current_deployment() { + if current_deployment != &deployment_name { + continue; + } + } + } + + let mut context = Context::from_context(context); let select_tokens = match optional_vec(deployment_yaml, "select-tokens") { Some(tokens) => Some( diff --git a/crates/settings/src/order.rs b/crates/settings/src/order.rs index 0bed2e978..9d2b5f348 100644 --- a/crates/settings/src/order.rs +++ b/crates/settings/src/order.rs @@ -10,7 +10,7 @@ use strict_yaml_rust::StrictYaml; use thiserror::Error; use typeshare::typeshare; use yaml::{ - context::{Context, SelectTokensContext}, + context::{Context, GuiContextTrait, SelectTokensContext}, default_document, optional_string, require_hash, require_string, require_vec, YamlError, YamlParsableHash, }; @@ -324,6 +324,14 @@ impl YamlParsableHash for Order { for (key_yaml, order_yaml) in orders_hash { let order_key = key_yaml.as_str().unwrap_or_default().to_string(); + if let Some(context) = context { + if let Some(current_order) = context.get_current_order() { + if current_order != &order_key { + continue; + } + } + } + let mut network: Option> = None; let deployer = match optional_string(order_yaml, "deployer") { diff --git a/crates/settings/src/yaml/context.rs b/crates/settings/src/yaml/context.rs index bc2cd88eb..2eb528a13 100644 --- a/crates/settings/src/yaml/context.rs +++ b/crates/settings/src/yaml/context.rs @@ -2,10 +2,17 @@ use crate::{Order, OrderIO, Token}; use std::sync::Arc; use thiserror::Error; +#[derive(Debug, Clone, Default)] +pub struct GuiContext { + pub current_deployment: Option, + pub current_order: Option, +} + #[derive(Debug, Clone, Default)] pub struct Context { pub order: Option>, pub select_tokens: Option>, + pub gui_context: Option, } #[derive(Error, Debug, PartialEq)] @@ -101,14 +108,45 @@ impl OrderContext for Context { } } +pub trait GuiContextTrait { + fn get_current_deployment(&self) -> Option<&String>; + + fn get_current_order(&self) -> Option<&String>; +} + +impl GuiContextTrait for Context { + fn get_current_deployment(&self) -> Option<&String> { + self.gui_context + .as_ref() + .and_then(|gui_context| gui_context.current_deployment.as_ref()) + } + + fn get_current_order(&self) -> Option<&String> { + self.gui_context + .as_ref() + .and_then(|gui_context| gui_context.current_order.as_ref()) + } +} + impl Context { pub fn new() -> Self { Self { order: None, select_tokens: None, + gui_context: None, } } + pub fn from_context(context: Option<&Context>) -> Self { + let mut new_context = Self::new(); + if let Some(context) = context { + new_context.order.clone_from(&context.order); + new_context.select_tokens.clone_from(&context.select_tokens); + new_context.gui_context.clone_from(&context.gui_context); + } + new_context + } + pub fn add_order(&mut self, order: Arc) -> &mut Self { self.order = Some(order); self @@ -119,6 +157,22 @@ impl Context { self } + pub fn add_current_deployment(&mut self, deployment: String) -> &mut Self { + self.gui_context = Some(GuiContext { + current_deployment: Some(deployment), + current_order: None, + }); + self + } + + pub fn add_current_order(&mut self, order: String) -> &mut Self { + self.gui_context = Some(GuiContext { + current_deployment: None, + current_order: Some(order), + }); + self + } + fn resolve_path(&self, path: &str) -> Result { let parts: Vec<&str> = path.split('.').collect(); diff --git a/crates/settings/src/yaml/dotrain.rs b/crates/settings/src/yaml/dotrain.rs index 4986623f8..7475b21ac 100644 --- a/crates/settings/src/yaml/dotrain.rs +++ b/crates/settings/src/yaml/dotrain.rs @@ -75,8 +75,12 @@ impl DotrainYaml { Deployment::parse_from_yaml(self.documents.clone(), key, None) } - pub fn get_gui(&self) -> Result, YamlError> { - Gui::parse_from_yaml_optional(self.documents.clone(), None) + pub fn get_gui(&self, current_deployment: Option) -> Result, YamlError> { + let mut context = Context::new(); + if let Some(deployment) = current_deployment { + context.add_current_deployment(deployment); + } + Gui::parse_from_yaml_optional(self.documents.clone(), Some(&context)) } } @@ -351,7 +355,7 @@ mod tests { "order1" ); - let gui = dotrain_yaml.get_gui().unwrap().unwrap(); + let gui = dotrain_yaml.get_gui(None).unwrap().unwrap(); assert_eq!(gui.name, "Test gui"); assert_eq!(gui.description, "Test description"); assert_eq!(gui.deployments.len(), 1); @@ -577,7 +581,7 @@ mod tests { fn test_handlebars() { let dotrain_yaml = DotrainYaml::new(vec![HANDLEBARS_YAML.to_string()], false).unwrap(); - let gui = dotrain_yaml.get_gui().unwrap().unwrap(); + let gui = dotrain_yaml.get_gui(None).unwrap().unwrap(); let deployment = gui.deployments.get("deployment1").unwrap(); assert_eq!( @@ -668,7 +672,7 @@ orders: ); let dotrain_yaml = DotrainYaml::new(vec![missing_input_token_yaml], false).unwrap(); - let error = dotrain_yaml.get_gui().unwrap_err(); + let error = dotrain_yaml.get_gui(None).unwrap_err(); assert_eq!( error, YamlError::ParseError( @@ -678,7 +682,7 @@ orders: ); let dotrain_yaml = DotrainYaml::new(vec![missing_output_token_yaml], false).unwrap(); - let error = dotrain_yaml.get_gui().unwrap_err(); + let error = dotrain_yaml.get_gui(None).unwrap_err(); assert_eq!( error, YamlError::ParseError(