diff --git a/lib/src/config/shadow.rs b/lib/src/config/shadow.rs index e76bbcf..65d77f1 100644 --- a/lib/src/config/shadow.rs +++ b/lib/src/config/shadow.rs @@ -129,7 +129,7 @@ pub struct HostsMut<'a> { } impl<'a> Iterator for HostsMut<'a> { - type Item = Result, Error>; + type Item = Result<&'a mut Mapping, Error>; fn next(&mut self) -> Option { self.hosts @@ -138,15 +138,6 @@ impl<'a> Iterator for HostsMut<'a> { .map(|(_, host)| { host.as_mapping_mut() .ok_or_else(|| Error::ExpectedOtherType("host".to_string())) - .map(UntypedHost) }) } } - -pub struct UntypedHost(T); - -impl<'a> UntypedHost<&'a mut Mapping> { - pub fn network_node_id_mut(&mut self) -> Option<&mut Value> { - self.0.get_mut("network_node_id") - } -} diff --git a/lib/src/error.rs b/lib/src/error.rs index 1acf1e8..0562ff9 100644 --- a/lib/src/error.rs +++ b/lib/src/error.rs @@ -51,4 +51,8 @@ pub enum Error { validator client" )] MissingValidatorCount, + #[error( + "You must specify a reliability and location, and no network_node_id in your custom hosts" + )] + InvalidShadowHost, } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index d12b911..3a92728 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -110,15 +110,22 @@ pub fn generate>( // postprocessing given shadow config values: overwrite string network ids for host in shadow_config.hosts_mut()? { - if let Some(node_id) = host?.network_node_id_mut() { - if let Some((location, reliability)) = node_id.as_str().and_then(|s| s.split_once('-')) - { - let node = network_graph.assign_network_node(location, reliability)?; - *node_id = Value::Number(node.id().into()); - } else { - return Err(Error::ExpectedOtherType("network_node_id".to_string())); - } + let mapping = host?; + if mapping.get("network_node_id").is_some() { + return Err(Error::InvalidShadowHost); } + let Value::String(location) = mapping.remove("location").ok_or(Error::InvalidShadowHost)? + else { + return Err(Error::ExpectedOtherType("location".to_string())); + }; + let Value::String(reliability) = mapping + .remove("reliability") + .ok_or(Error::InvalidShadowHost)? + else { + return Err(Error::ExpectedOtherType("reliability".to_string())); + }; + let node = network_graph.assign_network_node(&location, &reliability)?; + mapping.insert("network_node_id".into(), node.id().into()); } info!("Generating nodes");