Skip to main content

serialplugin_core/cmux/
path.rs

1//! Composite path helpers for CMUX virtual channels.
2
3/// Build a managed virtual port path for `dlci` on `physical`.
4pub fn mux_path(physical: &str, dlci: u8) -> String {
5    format!("{physical}#dlci={dlci}")
6}
7
8/// Data DLCIs are 1..=63 (6-bit address field; 0 is the control channel).
9pub fn is_data_dlci(dlci: u8) -> bool {
10    (1..=63).contains(&dlci)
11}
12
13/// Parse `physical#dlci=N` → `(physical, dlci)`.
14pub fn parse_mux_path(path: &str) -> Option<(&str, u8)> {
15    let (base, dlci_str) = path.rsplit_once("#dlci=")?;
16    dlci_str
17        .parse()
18        .ok()
19        .filter(|&d| is_data_dlci(d))
20        .map(|dlci| (base, dlci))
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn mux_path_roundtrip() {
29        let p = mux_path("/dev/ttyUSB0", 1);
30        assert_eq!(parse_mux_path(&p), Some(("/dev/ttyUSB0", 1)));
31        assert_eq!(parse_mux_path("/dev/ttyUSB0#dlci=0"), None);
32        assert_eq!(parse_mux_path("/dev/ttyUSB0#dlci=65"), None);
33    }
34}