Skip to content

Commit

Permalink
Refactor populate_connection_ids
Browse files Browse the repository at this point in the history
Co-authored-by: Atanas Dinov <[email protected]>
Signed-off-by: Koen de Laat <[email protected]>
  • Loading branch information
koendelaat and atanasdinov committed Nov 6, 2024
1 parent 68418be commit cd7f3b3
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions src/generate_conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,34 @@ fn populate_connection_ids(
interfaces: &mut [Interface],
config: &NetworkConfig,
) -> anyhow::Result<()> {
config
.iter()
.map(|(f, content)| {
let mut c = Ini::new();
c.read(content.to_string()).map_err(|e| anyhow!(e))?;
Ok((f, c))
})
.filter(|x| {
!x.as_ref()
.is_ok_and(|(_, c)| c.get("connection", "type").is_some_and(|t| t == "loopback"))
})
.try_for_each(|x: anyhow::Result<(&String, Ini)>| {
let (f, c) = x?;
let interface_name = c
.get("connection", "interface-name")
.ok_or_else(|| anyhow!("No interface-name found in connection file: {}", f))?;
let connection_id = c
.get("connection", "id")
.ok_or_else(|| anyhow!("No connection id found in connection file: {}", f))?;
interfaces
.iter_mut()
.find(|x| x.logical_name == interface_name)
.ok_or_else(|| anyhow!("No matching interface found for connection file: {}", f))?
.connection_ids
.push(connection_id);
Ok(())
})
for (filename, content) in config {
let mut c = Ini::new();
c.read(content.to_string()).map_err(|e| anyhow!(e))?;

if c.get("connection", "type").is_some_and(|t| t == "loopback") {
continue;
}

let interface_name = c
.get("connection", "interface-name")
.ok_or_else(|| anyhow!("No interface-name found in connection file: {}", filename))?;
let connection_id = c
.get("connection", "id")
.ok_or_else(|| anyhow!("No connection id found in connection file: {}", filename))?;
interfaces
.iter_mut()
.find(|x| x.logical_name == interface_name)
.ok_or_else(|| {
anyhow!(
"No matching interface found for connection file: {}",
filename
)
})?
.connection_ids
.push(connection_id);
}

Ok(())
}

fn extract_interfaces(network_state: &NetworkState) -> Vec<Interface> {
Expand Down

0 comments on commit cd7f3b3

Please sign in to comment.