//! The old normaliser against the new one, on one database file — for the //! gate on a COPY. Prints counts only, per table, never a number: //! //! `cargo run -p cotton-store --example phone-compare -- /path/to/copy.db` //! //! - same: the new normaliser gives the stored form byte for byte; //! - changed, folded: it differs only by the trunk 0 after a country code or //! a leading 00, which the phone fold carries; //! - changed, other: any other difference (the gate wants 0). //! //! Read-only: the file is opened without write access. Refuses anything under //! `~/CottonDB` or `/Applications`. use cotton_store::protocol::person_handles::{normalise_handle, HandleKind}; use cotton_store::protocol::phone::{normalize_phone, refold_stored_phone, strip_presentation}; use rusqlite::{Connection, OpenFlags}; /// The normaliser before 22 Oct 2026, and the handle form it gave. fn before_phone(raw: &str) -> Option { let stripped = strip_presentation(raw); let digits = stripped.strip_prefix('+').unwrap_or(&stripped); ((8..=15).contains(&digits.len()) && digits.bytes().all(|b| b.is_ascii_digit())).then(|| digits.to_string()) } fn before_handle(raw: &str) -> Option { let trimmed = raw.trim(); if trimmed.is_empty() { return None; } Some(before_phone(trimmed).unwrap_or_else(|| trimmed.to_lowercase().chars().filter(|c| !c.is_whitespace()).collect())) } /// Whether `new` is `old` less a leading 00 and/or one 0 just after a country /// code of one to three digits — the difference the fold carries. fn trunk_or_prefix(old: &str, new: &str) -> bool { let rest = old.strip_prefix("00").unwrap_or(old); if rest == new { return true; } (1..=3).any(|cc| rest.len() > cc && &rest[cc..cc + 1] == "0" && format!("{}{}", &rest[..cc], &rest[cc + 1..]) == new) } #[derive(Default)] struct Tally { same: usize, folded: usize, other: usize, } impl Tally { fn add(&mut self, old: Option<&str>, new: Option<&str>) { match (old, new) { (a, b) if a == b => self.same += 1, (Some(a), Some(b)) if trunk_or_prefix(a, b) => self.folded += 1, _ => self.other += 1, } } } fn main() { let Some(path) = std::env::args().nth(1) else { eprintln!("usage: phone-compare /path/to/copy.db"); std::process::exit(2); }; let real = std::path::Path::new(&path).canonicalize().unwrap_or_else(|e| panic!("{path}: {e}")); let home = std::env::var("HOME").unwrap_or_default(); if real.starts_with(format!("{home}/CottonDB")) || real.starts_with("/Applications") { eprintln!("{}: not a copy — refused", real.display()); std::process::exit(2); } let db = Connection::open_with_flags(&real, OpenFlags::SQLITE_OPEN_READ_ONLY).expect("opens read-only"); let texts = |sql: &str| -> Vec<(String, String)> { let mut statement = db.prepare(sql).expect(sql); let rows = statement .query_map([], |row| Ok((row.get::<_, String>(0)?, row.get::<_, Option>(1)?.unwrap_or_default()))) .expect(sql); rows.map(|row| row.expect(sql)).collect() }; // A handle: what the old and the new normaliser make of what was typed. let mut handles = Tally::default(); let mut stored_differs = 0; for (value, normalised) in texts("SELECT value, normalised FROM cotton_person_handles WHERE kind = 'phone'") { let old = before_handle(&value); if old.as_deref() != Some(normalised.as_str()) { stored_differs += 1; } handles.add(old.as_deref(), normalise_handle(HandleKind::Phone, &value).as_deref()); } // A contact or a contact handle holds only the stored digits: the new // normaliser on them, then what the fold makes of them. let stored = |sql: &str| { let mut tally = Tally::default(); for (value, _) in texts(sql) { let new = normalize_phone(&value).map(|normal| refold_stored_phone(&normal).unwrap_or(normal)); tally.add(Some(value.as_str()), new.as_deref()); } tally }; let contacts = stored("SELECT phone_e164, '' FROM cotton_msg_contacts WHERE phone_e164 IS NOT NULL AND phone_e164 <> ''"); let contact_handles = stored("SELECT value, '' FROM cotton_msg_contact_handles WHERE kind = 'phone'"); for (table, tally) in [ ("cotton_person_handles", &handles), ("cotton_msg_contacts", &contacts), ("cotton_msg_contact_handles", &contact_handles), ] { println!("{table}: same {} / changed, folded {} / changed, other {}", tally.same, tally.folded, tally.other); } println!("cotton_person_handles stored differently from the old normaliser's own form: {stored_differs}"); let gate = handles.other + contacts.other + contact_handles.other == 0; println!("gate: {}", if gate { "MET" } else { "NOT MET" }); }