use crate::*; use ConnectName::*; use ConnectScope::*; use ConnectStatus::*; #[derive(Clone, Debug, PartialEq)] pub enum ConnectName { /** Exact match */ Exact(Arc), /** Match regular expression */ RegExp(Arc), } #[derive(Clone, Copy, Debug, PartialEq)] pub enum ConnectScope { One, All } #[derive(Clone, Copy, Debug, PartialEq)] pub enum ConnectStatus { Missing, Disconnected, Connected, Mismatch, } /// Port connection manager. /// /// ``` /// let connect = tek::Connect::default(); /// ``` #[derive(Clone, Debug, Default)] pub struct Connect { pub name: Option, pub scope: Option, pub status: Arc, Arc, ConnectStatus)>>>, pub info: Arc, } impl Connect { pub fn collect (exact: &[impl AsRef], re: &[impl AsRef], re_all: &[impl AsRef]) -> Vec { let mut connections = vec![]; for port in exact.iter() { connections.push(Self::exact(port)) } for port in re.iter() { connections.push(Self::regexp(port)) } for port in re_all.iter() { connections.push(Self::regexp_all(port)) } connections } /// Connect to this exact port pub fn exact (name: impl AsRef) -> Self { let info = format!("=:{}", name.as_ref()).into(); let name = Some(Exact(name.as_ref().into())); Self { name, scope: Some(One), status: Arc::new(RwLock::new(vec![])), info } } pub fn regexp (name: impl AsRef) -> Self { let info = format!("~:{}", name.as_ref()).into(); let name = Some(RegExp(name.as_ref().into())); Self { name, scope: Some(One), status: Arc::new(RwLock::new(vec![])), info } } pub fn regexp_all (name: impl AsRef) -> Self { let info = format!("+:{}", name.as_ref()).into(); let name = Some(RegExp(name.as_ref().into())); Self { name, scope: Some(All), status: Arc::new(RwLock::new(vec![])), info } } pub fn info (&self) -> Arc { format!(" ({}) {} {}", { let status = self.status.read().unwrap(); let mut ok = 0; for (_, _, state) in status.iter() { if *state == Connected { ok += 1 } } format!("{ok}/{}", status.len()) }, match self.scope { None => "x", Some(One) => " ", Some(All) => "*", }, match &self.name { None => format!("x"), Some(Exact(name)) => format!("= {name}"), Some(RegExp(name)) => format!("~ {name}"), }).into() } }