Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: better way to specify custom hosts #6

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions lib/src/config/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub struct HostsMut<'a> {
}

impl<'a> Iterator for HostsMut<'a> {
type Item = Result<UntypedHost<&'a mut Mapping>, Error>;
type Item = Result<&'a mut Mapping, Error>;

fn next(&mut self) -> Option<Self::Item> {
self.hosts
Expand All @@ -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>(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")
}
}
4 changes: 4 additions & 0 deletions lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
23 changes: 15 additions & 8 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,22 @@ pub fn generate<T: TryInto<FullConfig, Error = Error>>(

// 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");
Expand Down