Cotton code
129 lines · 4.5 KB · RustRaw
//! Typed facts for a database's sync page. Words belong to definitions.use serde::{Deserialize, Serialize};#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]#[serde(rename_all = "snake_case")]pub enum DeviceKind {    Phone,    Tablet,    Mac,    Linux,    Windows,    Other,}impl DeviceKind {    pub fn of_platform(platform: &str) -> Self {        match platform {            "ios" | "iphone" | "phone" | "android" => Self::Phone,            "ipad" | "ipados" => Self::Tablet,            "mac" | "macos" => Self::Mac,            "linux" => Self::Linux,            "windows" => Self::Windows,            _ => Self::Other,        }    }}#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]#[serde(rename_all = "snake_case")]pub enum Keeps {    RecordsOnly,    RecordsAndMedia,    WholeFile,}#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]#[serde(rename_all = "snake_case")]pub enum DoingNow {    Idle,    PullingChanges,    FetchingBytes,    Sending,    CatchingUp,    Paused,}#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct Device {    /// The device credential id, not the approval row id.    pub id: String,    pub name: String,    pub kind: DeviceKind,    pub serving: bool,}#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]pub struct Behind {    /// Incoming receipts still owed by the serving copy, never local writes.    pub changes: Option<u64>,    /// True when the exchange capped its count; false when it is exact.    pub at_least: bool,    pub oldest_age_seconds: Option<u64>,}#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct DeviceCopy {    pub device: Device,    /// Last observation of this device, not the time this read ran.    pub as_of: Option<String>,    /// Retention policy; it does not promise that every requested byte arrived.    pub keeps: Option<Keeps>,    pub behind: Behind,    pub doing_now: Option<DoingNow>,    pub last_heard_at: Option<String>,    /// None means no reachability evidence, not an unreachable device.    pub reachable: Option<bool>,    pub unreachable_since: Option<String>,}#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct ThisDevice {    pub id: Option<String>,    /// Last exchange known to be level, not merely the latest successful pull.    pub synced_at: Option<String>,    pub changes_waiting: u64,    pub storage_bytes: Option<u64>,    /// What the keep-fetch sweep last stored as still wanted; `None` until it    /// has judged this database (or on a device that keeps every original).    pub bytes_wanted: Option<u64>,}#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct SyncState {    pub database: String,    /// When this copy last heard from its serving copy (now on the server).    pub as_of: Option<String>,    pub read_at: String,    pub devices: Vec<DeviceCopy>,    pub this_device: ThisDevice,}/// The shared notice reading, also used before a join knows the Mac's name.pub fn silence_words(device: &str) -> String {    crate::definitions::board::notice_words().get("sync_not_answering")        .and_then(serde_json::Value::as_str).expect("shipped sync reading")        .replace("{device}", device)}/// The serving copy's observed silence, resolved with the reading's words.pub fn not_answering_line(devices: &[DeviceCopy], template: &str) -> Option<String> {    devices.iter().find(|copy| copy.device.serving && copy.reachable == Some(false))        .map(|copy| template.replace("{device}", if copy.device.name.trim().is_empty() { "The Mac" } else { copy.device.name.trim() }))}#[cfg(test)]mod word_guards {    use super::*;    /// RULE: A silent serving copy is named from the local reading; unknown reachability never claims silence and transport diagnostics cannot become the line.    #[test]    fn silence_reads_the_serving_device_name_and_definition_words() {        let mut copies = vec![DeviceCopy { device: Device {id:"example".into(),name:"Example Mac".into(),kind:DeviceKind::Mac,serving:true},as_of:None,keeps:None,behind:Behind::default(),doing_now:None,last_heard_at:None,reachable:Some(false),unreachable_since:None }];        assert_eq!(not_answering_line(&copies, "Waiting for {device}"), Some("Waiting for Example Mac".into()));        copies[0].reachable = None;        assert_eq!(not_answering_line(&copies, "Waiting for {device}"), None);        copies[0].reachable = Some(true);        assert_eq!(not_answering_line(&copies, "Waiting for {device}"), None);    }}