//! QR payload strings. Rendering happens in the app; this crate only decides what the code says. /// Security of a Wi-Fi network as the `WIFI:` payload names it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum WifiSecurity { /// WPA, WPA2 or WPA3 (`T:WPA`). Wpa, Wep, /// Open network (`T:nopass`). None, } impl WifiSecurity { /// From the vault's `security` field (`WPA`, `WEP`, `nopass`); anything else counts as WPA. pub fn parse(s: &str) -> WifiSecurity { match s.trim().to_ascii_uppercase().as_str() { "WEP" => WifiSecurity::Wep, "NOPASS" | "NONE" | "OPEN" => WifiSecurity::None, _ => WifiSecurity::Wpa, } } fn token(self) -> &'static str { match self { WifiSecurity::Wpa => "WPA", WifiSecurity::Wep => "WEP", WifiSecurity::None => "nopass", } } } /// Escapes `\`, `;`, `,`, `:` and `"` with a backslash, as the `WIFI:` format requires. pub fn escape(s: &str) -> String { let mut out = String::with_capacity(s.len()); for c in s.chars() { if matches!(c, '\\' | ';' | ',' | ':' | '"') { out.push('\\'); } out.push(c); } out } /// `WIFI:T:;S:;P:;H:;;` — a phone camera joins from it. /// An open network gets no `P:` field. pub fn wifi_payload(ssid: &str, password: &str, security: WifiSecurity, hidden: bool) -> String { let mut p = format!("WIFI:T:{};S:{};", security.token(), escape(ssid)); if security != WifiSecurity::None { p.push_str(&format!("P:{};", escape(password))); } p.push_str(&format!("H:{hidden};;")); p } /// A QR code that shows a secret to a phone camera is the plain value, so it can be typed elsewhere. pub fn secret_payload(value: &str) -> String { value.to_owned() }