use super::identity::NavigatorProfile;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TimezoneFacts {
pub timezone: &'static str,
pub country: &'static str,
pub languages: &'static [&'static str],
pub lat: f64,
pub lon: f64,
}
pub const TIMEZONE_FACTS: &[TimezoneFacts] = &[
TimezoneFacts {
timezone: "America/New_York",
country: "US",
languages: &["en"],
lat: 40.7128,
lon: -74.0060,
},
TimezoneFacts {
timezone: "America/Chicago",
country: "US",
languages: &["en"],
lat: 41.8781,
lon: -87.6298,
},
TimezoneFacts {
timezone: "America/Denver",
country: "US",
languages: &["en"],
lat: 39.7392,
lon: -104.9903,
},
TimezoneFacts {
timezone: "America/Phoenix",
country: "US",
languages: &["en"],
lat: 33.4484,
lon: -112.0740,
},
TimezoneFacts {
timezone: "America/Los_Angeles",
country: "US",
languages: &["en"],
lat: 37.7749,
lon: -122.4194,
},
TimezoneFacts {
timezone: "Europe/London",
country: "GB",
languages: &["en"],
lat: 51.5074,
lon: -0.1278,
},
TimezoneFacts {
timezone: "Europe/Berlin",
country: "DE",
languages: &["de", "en"],
lat: 52.5200,
lon: 13.4050,
},
TimezoneFacts {
timezone: "Europe/Paris",
country: "FR",
languages: &["fr"],
lat: 48.8566,
lon: 2.3522,
},
TimezoneFacts {
timezone: "Europe/Madrid",
country: "ES",
languages: &["es"],
lat: 40.4168,
lon: -3.7038,
},
TimezoneFacts {
timezone: "Europe/Amsterdam",
country: "NL",
languages: &["nl", "en"],
lat: 52.3676,
lon: 4.9041,
},
TimezoneFacts {
timezone: "Asia/Tokyo",
country: "JP",
languages: &["ja"],
lat: 35.6762,
lon: 139.6503,
},
TimezoneFacts {
timezone: "Asia/Shanghai",
country: "CN",
languages: &["zh"],
lat: 31.2304,
lon: 121.4737,
},
TimezoneFacts {
timezone: "Asia/Kolkata",
country: "IN",
languages: &["en", "hi"],
lat: 28.6139,
lon: 77.2090,
},
TimezoneFacts {
timezone: "Australia/Sydney",
country: "AU",
languages: &["en"],
lat: -33.8688,
lon: 151.2093,
},
TimezoneFacts {
timezone: "America/Toronto",
country: "CA",
languages: &["en", "fr"],
lat: 43.6532,
lon: -79.3832,
},
];
const MAX_COORD_DISTANCE_KM: f64 = 3000.0;
#[must_use]
pub fn timezone_facts(timezone: &str) -> Option<&'static TimezoneFacts> {
TIMEZONE_FACTS.iter().find(|f| f.timezone == timezone)
}
#[must_use]
fn locale_region(locale: &str) -> Option<String> {
locale
.split(['-', '_'])
.nth(1)
.filter(|r| r.len() == 2 && r.chars().all(|c| c.is_ascii_alphabetic()))
.map(|r| r.to_ascii_uppercase())
}
#[must_use]
fn language_subtag(tag: &str) -> String {
tag.split(['-', '_'])
.next()
.unwrap_or(tag)
.to_ascii_lowercase()
}
#[must_use]
fn haversine_km(lat1: f64, lon1: f64, lat2: f64, lon2: f64) -> f64 {
const EARTH_RADIUS_KM: f64 = 6371.0;
let (p1, p2) = (lat1.to_radians(), lat2.to_radians());
let (dlat, dlon) = ((lat2 - lat1).to_radians(), (lon2 - lon1).to_radians());
let a = (dlat / 2.0).sin().powi(2) + p1.cos() * p2.cos() * (dlon / 2.0).sin().powi(2);
2.0 * EARTH_RADIUS_KM * a.sqrt().asin()
}
#[derive(Debug, Clone, PartialEq)]
pub enum GeoIncoherence {
Country {
timezone_country: String,
persona_country: String,
},
LocaleCountry {
timezone_country: String,
locale_region: String,
},
Language {
primary_language: String,
timezone_languages: Vec<String>,
},
Coordinates {
distance_km: f64,
timezone: String,
},
}
#[derive(Debug, Clone, PartialEq)]
pub enum GeoCoherence {
Coherent,
Incoherent(Vec<GeoIncoherence>),
Unknown {
timezone: String,
},
}
impl GeoCoherence {
#[must_use]
pub const fn is_coherent(&self) -> bool {
matches!(self, Self::Coherent)
}
}
#[must_use]
pub fn identity_geo_coherence(identity: &NavigatorProfile) -> GeoCoherence {
let Some(facts) = timezone_facts(&identity.timezone) else {
return GeoCoherence::Unknown {
timezone: identity.timezone.clone(),
};
};
let mut problems = Vec::new();
if !identity.country.is_empty() {
let persona_country = identity.country.to_ascii_uppercase();
if persona_country != facts.country {
problems.push(GeoIncoherence::Country {
timezone_country: facts.country.to_string(),
persona_country,
});
}
}
if let Some(region) = locale_region(&identity.locale) {
if region != facts.country {
problems.push(GeoIncoherence::LocaleCountry {
timezone_country: facts.country.to_string(),
locale_region: region,
});
}
}
if let Some(primary) = identity.languages.first() {
let primary = language_subtag(primary);
if !facts.languages.iter().any(|l| *l == primary) {
problems.push(GeoIncoherence::Language {
primary_language: primary,
timezone_languages: facts.languages.iter().map(|l| (*l).to_string()).collect(),
});
}
}
let distance = haversine_km(identity.latitude, identity.longitude, facts.lat, facts.lon);
if distance > MAX_COORD_DISTANCE_KM {
problems.push(GeoIncoherence::Coordinates {
distance_km: distance,
timezone: facts.timezone.to_string(),
});
}
if problems.is_empty() {
GeoCoherence::Coherent
} else {
GeoCoherence::Incoherent(problems)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ObservedEgressGeo {
pub country: Option<String>,
pub timezone: Option<String>,
}
impl ObservedEgressGeo {
#[must_use]
pub fn is_empty(&self) -> bool {
self.country.is_none() && self.timezone.is_none()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EgressGeoMismatch {
Country {
persona_country: String,
egress_country: String,
},
Timezone {
persona_timezone: String,
egress_timezone: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GeoSelfProbe {
Coherent,
Incoherent(Vec<EgressGeoMismatch>),
Unmeasured,
}
impl GeoSelfProbe {
#[must_use]
pub const fn is_coherent(&self) -> bool {
matches!(self, Self::Coherent)
}
}
#[must_use]
pub fn persona_geo_self_probe(
identity: &NavigatorProfile,
observed: &ObservedEgressGeo,
) -> GeoSelfProbe {
let mut mismatches = Vec::new();
let mut compared = 0_usize;
if let Some(egress_country) = observed.country.as_deref() {
let persona_country = if identity.country.is_empty() {
timezone_facts(&identity.timezone).map(|f| f.country)
} else {
Some(identity.country.as_str())
};
if let Some(persona_country) = persona_country {
compared += 1;
let egress_country = egress_country.to_ascii_uppercase();
if persona_country != egress_country {
mismatches.push(EgressGeoMismatch::Country {
persona_country: persona_country.to_string(),
egress_country,
});
}
}
}
if let Some(egress_tz) = observed.timezone.as_deref() {
compared += 1;
if identity.timezone != egress_tz {
mismatches.push(EgressGeoMismatch::Timezone {
persona_timezone: identity.timezone.clone(),
egress_timezone: egress_tz.to_string(),
});
}
}
if compared == 0 {
GeoSelfProbe::Unmeasured
} else if mismatches.is_empty() {
GeoSelfProbe::Coherent
} else {
GeoSelfProbe::Incoherent(mismatches)
}
}
#[cfg(test)]
#[path = "geo_coherence/tests.rs"]
mod tests;