mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-08-07 14:06:57 +02:00
83 lines
2.6 KiB
Rust
83 lines
2.6 KiB
Rust
use crate::*;
|
|
use ConnectName::*;
|
|
use ConnectScope::*;
|
|
use ConnectStatus::*;
|
|
|
|
#[derive(Clone, Debug, PartialEq)] pub enum ConnectName {
|
|
/** Exact match */
|
|
Exact(Arc<str>),
|
|
/** Match regular expression */
|
|
RegExp(Arc<str>),
|
|
}
|
|
|
|
#[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<ConnectName>,
|
|
pub scope: Option<ConnectScope>,
|
|
pub status: Arc<RwLock<Vec<(Port<Unowned>, Arc<str>, ConnectStatus)>>>,
|
|
pub info: Arc<str>,
|
|
}
|
|
|
|
impl Connect {
|
|
pub fn collect (exact: &[impl AsRef<str>], re: &[impl AsRef<str>], re_all: &[impl AsRef<str>])
|
|
-> Vec<Self>
|
|
{
|
|
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<str>) -> 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<str>) -> 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<str>) -> 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<str> {
|
|
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()
|
|
}
|
|
}
|