//! What the Apple readers DECIDE about one row, as pure functions: an Apple//! timestamp, and a handle (a number, an address or a label).//!//! Split from the readers so the decisions can be tested without a database//! and so there is exactly one place that answers each of them. The call//! history and the Photos library both go through here.use chrono::{TimeZone, Utc};use cotton_core::import::phone::promote_to_country;use cotton_core::messages::resolve_contact::{normalize_contact_handle, ContactHandleKind};/// Apple's epoch: 2001-01-01T00:00:00Z, in milliseconds since the Unix one.pubconst APPLE_EPOCH_MS: i64 = 978_307_200_000;/// Anything above this many nanoseconds-since-2001 would be a date past the/// year 2100 if it were SECONDS, so a value this large is nanoseconds. Modern/// macOS writes nanoseconds; files old enough to hold seconds still exist, and/// the magnitude is the only thing that distinguishes them.const NANOSECOND_FLOOR: i64 = 1_000_000_000_000;/// A `message.date` as an ISO-8601 UTC instant, or `None` when the row has one/// this reader must not file.////// `0` is a real value in these files and it does NOT mean 2001 — it means the/// row never got a date. A message stamped at the Apple epoch would be a/// message from before the iPhone existed, so it is dropped rather than filed/// twenty-five years ago.pubfn apple_date_to_iso(raw: Option<i64>) -> Option<String> {let value = raw?;if value <= 0 {returnNone; }let ms = if value > NANOSECOND_FLOOR { value / 1_000_000 } else { value.checked_mul(1000)? };let at = Utc .timestamp_millis_opt(APPLE_EPOCH_MS.checked_add(ms)?) .single()?;Some(crate::seams::iso(at))}/// How a handle identifies somebody, once it has been looked at.#[derive(Debug, Clone, PartialEq, Eq)]pubenumHandleKind {Phone,Email,/// A shortcode or an alphanumeric sender id — an identity nobody can be.Label,}#[derive(Debug, Clone, PartialEq, Eq)]pubstructMessageHandle {pub kind: HandleKind,pub value: String,pub raw: String,}/// The default country for a bare national number.////// The canonical normaliser deliberately never GUESSES a country (a wrong guess/// files somebody's messages under a stranger), so the promotion is stated/// rather than inferred. India, because that is where this Mac and its owner's/// numbers are; when the Mac learns a workspace's own country this becomes/// that.pubconst DEFAULT_COUNTRY_CODE: &str = "91";/// What a `handle.id` is.////// Three answers, and the third one matters more than it looks: most of the/// handles in a real Messages file are neither a phone nor an email — they are/// bank alerts, OTP senders and delivery services, whose sender id is a word./// Forcing one of those into a phone-shaped contact would file a bank under a/// number that is not a number; they become a titled thread with no contact/// instead, which is exactly what the import engine's label-only path is for.pubfn read_handle(raw: Option<&str>, country_code: &str) -> Option<MessageHandle> {let trimmed = raw?.trim();if trimmed.is_empty() {returnNone; }if trimmed.contains('@') {returnSome(match normalize_contact_handle(ContactHandleKind::Email, trimmed) {Some(handle) => MessageHandle { kind: HandleKind::Email, value: handle.value, raw: trimmed.to_string(), },None => MessageHandle { kind: HandleKind::Label, value: trimmed.to_string(), raw: trimmed.to_string(), }, }, ); }let promoted = promote_to_country(trimmed, Some(country_code));match normalize_contact_handle(ContactHandleKind::Phone, &promoted) {Some(handle) => Some(MessageHandle { kind: HandleKind::Phone, value: handle.value, raw: trimmed.to_string(), }),None => Some(MessageHandle { kind: HandleKind::Label, value: trimmed.to_string(), raw: trimmed.to_string(), }), }}#[cfg(test)]#[path = "apple_schema_tests.rs"]mod tests;