agentmux_srv_core/backend/lan/
peer_words.rs1use super::discovery::LanInstance;
10
11pub(crate) fn advertised(info: &mdns_sd::ServiceInfo) -> (String, String, String, String) {
14 let token = |key: &str| info.get_property_val_str(key).and_then(crate::backend::host_os::sanitize_os).unwrap_or_default();
15 (token("os"), token("install"), build_token(info.get_property_val_str("build")), install_id_token(info.get_property_val_str("install_id")))
16}
17
18pub(crate) fn install_id_token(said: Option<&str>) -> String {
22 said.map(str::trim)
23 .filter(|s| s.len() == 26 && s.bytes().all(|b| b.is_ascii_lowercase() || (b'2'..=b'7').contains(&b)))
24 .map(str::to_string)
25 .unwrap_or_default()
26}
27
28pub(crate) fn build_token(said: Option<&str>) -> String {
31 said.map(str::trim)
32 .and_then(agentmux_common::build_kind::BuildKind::from_label)
33 .map(|k| k.label().to_string())
34 .unwrap_or_default()
35}
36
37pub(crate) fn build_words(build: &str) -> &'static str {
39 match build {
40 "release" => "release",
41 "local" => "local build",
42 "dev" => "dev build",
43 _ => "",
44 }
45}
46
47pub(crate) fn keep_known(known: &mut String, new: String) {
50 if !new.is_empty() {
51 *known = new;
52 }
53}
54
55pub fn install_words(install: &str) -> &'static str {
57 match install {
58 "windows-store" => "Microsoft Store",
59 "windows-setup" => "installed",
60 "windows-portable" | "linux-portable" => "portable",
61 "macos-app" => "app",
62 "linux-appimage" => "AppImage",
63 "linux-package" => "package",
64 _ => "",
65 }
66}
67
68pub fn describe_peer(p: &LanInstance) -> String {
73 let host = p.hostname.trim();
74 let name = if p.channel.is_empty() { host.to_string() } else { format!("{host}/{}", p.channel) };
75 let mut notes: Vec<&str> = Vec::new();
76 let kind = build_words(&p.build);
77 if !kind.is_empty() {
78 notes.push(kind);
79 }
80 let how = install_words(&p.install);
81 if !how.is_empty() {
82 notes.push(how);
83 }
84 if p.last_polled_ok > 0 && p.agents.is_empty() {
85 notes.push("no agents");
86 }
87 if notes.is_empty() {
88 name
89 } else {
90 format!("{name} ({})", notes.join(", "))
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::install_id_token;
97
98 #[test]
99 fn an_install_id_is_kept_only_when_it_is_shaped_like_one() {
100 let id = "mw3am46w5weex4a4fqrc3avnua";
101 assert_eq!(install_id_token(Some(id)), id);
102 assert_eq!(install_id_token(Some(&format!(" {id} "))), id, "trimmed");
103 for bad in ["", "short", "MW3AM46W5WEEX4A4FQRC3AVNUA", "mw3am46w5weex4a4fqrc3avnu1", "mw3am46w5weex4a4fqrc3avnuaa", "mw3am46w5weex4a4fqrc3avné"] {
104 assert_eq!(install_id_token(Some(bad)), "", "{bad:?}");
105 }
106 assert_eq!(install_id_token(None), "");
107 }
108}