From ecb7ea9f11f77c25ff9830e9b267cf2055d24a67 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Mon, 8 May 2023 08:52:03 +0100 Subject: [PATCH] Improved code --- src/main.rs | 56 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index c4eaf91..1c269af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,4 @@ -use std::{cell::RefCell, env, fs, io::BufRead, collections::HashMap}; -use std::vec::Vec; -use std::{cell::RefMut, rc::Rc}; +use std::{cell::{RefMut, RefCell}, rc::Rc, fs, vec::Vec, env, io::BufRead, collections::HashMap}; use libspa::ReadableDict; use pipewire::{types::ObjectType, Context, MainLoop}; @@ -40,6 +38,24 @@ struct LinkDef { port_out: Rc, } +#[derive(Debug)] +struct Pair { + node: String, + port: String, +} + +impl PortDef { + fn is_pair(&self, pair: &Pair) -> bool { + self.name.eq(&pair.port) && self.node.name.eq(&pair.node) + } + fn to_pair(&self) -> Pair { + Pair { + node: self.node.name.to_string(), + port: self.name.to_string() + } + } +} + struct AppState { ports: Vec>, nodes: Vec>, @@ -105,8 +121,8 @@ impl AppState { search(&self.nodes, |a| a.id == id) } - fn get_port_by_name(&self, name: String) -> Option> { - search(&self.ports, |a| a.name.eq(&name)) + fn get_port_by_pair(&self, pair: &Pair) -> Option> { + search(&self.ports, |a| a.name.eq(&pair.port) && a.node.name.eq(&pair.node)) } fn try_add_port(&mut self, id: u32, name: String, node_id: u32) -> bool { @@ -145,7 +161,7 @@ impl AppState { true } - fn create_links(&mut self, port_name: String, core: Rc) { + fn create_links(&mut self, pair: Pair, core: Rc) { #[derive(Debug)] struct TempLink { port_in: Option>, @@ -154,10 +170,10 @@ impl AppState { self.link_def .iter() - .filter(|link| (link.port_in.name.eq(&port_name) || link.port_out.name.eq(&port_name))) + .filter(|link| (link.port_in.is_pair(&pair) || link.port_out.is_pair(&pair))) .map(|a| TempLink { - port_in: self.get_port_by_name(a.port_in.name.to_string()), - port_out: self.get_port_by_name(a.port_out.name.to_string()), + port_in: self.get_port_by_pair(&a.port_in.to_pair()), + port_out: self.get_port_by_pair(&a.port_out.to_pair()), }).filter(|a| a.port_in.is_some() && a.port_out.is_some()).for_each(|a| { let port_in = a.port_in.unwrap(); let port_out = a.port_out.unwrap(); @@ -226,12 +242,13 @@ fn deal_with_port( if let (Some(name), Some(node_id)) = (props.get("port.name"), props.get("node.id")) { if let Ok(node_id) = node_id.parse::() { if state.try_add_port(port.id, name.to_string(), node_id) { + let node_name = state.get_node(node_id).unwrap().name.to_string(); println!( "Got port {} for {}", name, - state.get_node(node_id).unwrap().name + node_name ); - state.create_links(name.to_string(), core) + state.create_links(Pair {node: node_name.to_string(), port: name.to_string() }, core) } } else { println!("Clould not parse {}'s node.id({})", name, node_id) @@ -284,20 +301,24 @@ fn parse_file(path: std::path::PathBuf, get_names: bool) -> Result port.to_owned(), None => { let port = Rc::new(PortDef { node: node_out.clone(), name: caps["port_out"].to_string() } ); - port_def.insert(caps["port_out"].to_string(), port.clone()); + port_def.insert(port_out_store, port.clone()); port }, }; - let port_in = match port_def.get_mut(&caps["port_in"]) { + let port_in_store = format!("{} {}", &caps["node_in"], &caps["port_in"]); + + let port_in = match port_def.get_mut(&port_in_store) { Some(port) => port.to_owned(), None => { let port = Rc::new(PortDef { node: node_in.clone(), name: caps["port_in"].to_string() } ); - port_def.insert(caps["port_in"].to_string(), port.clone()); + port_def.insert(port_in_store, port.clone()); port }, }; @@ -305,7 +326,7 @@ fn parse_file(path: std::path::PathBuf, get_names: bool) -> Result Result>>(); let port_def = port_def.values().cloned().collect::>>(); + println!("node_def {:?}", node_def); + println!("port_def {:?}", port_def); + Ok(AppState::new(node_def, link_def, port_def, get_names)) }