Skip to main content

rightkit_sync/
error.rs

1//! Error taxonomy shared by every adapter.
2
3#[derive(Debug, Clone, PartialEq)]
4pub enum SyncError {
5    /// Sync cannot run at all right now (signed out, restricted, entitlement
6    /// missing). Surfaced to the user; retried on the backoff cadence.
7    Unavailable(String),
8    /// Transient failure with a server- or policy-supplied delay.
9    RetryAfter { secs: f64, detail: String },
10    /// Transient failure without a delay hint.
11    Transient(String),
12    /// The backend invalidated the change token. The engine resets the token
13    /// once and replays the full zone; local data is never discarded.
14    TokenExpired,
15    /// The zone was deleted or never created. The engine calls `prepare`
16    /// again once and replays the full zone.
17    ZoneMissing,
18    /// Durable local state could not be written or read back.
19    Persistence(String),
20    /// Conditional save refused: the server record changed since the change
21    /// tag the write was based on. The outcome carries the server record.
22    Conflict,
23    /// The auth generation changed or sync was disabled mid-pass.
24    Cancelled,
25}
26
27impl SyncError {
28    pub fn retry_hint_secs(&self) -> Option<f64> {
29        match self {
30            Self::RetryAfter { secs, .. } if secs.is_finite() && *secs > 0.0 => Some(*secs),
31            _ => None,
32        }
33    }
34}
35
36impl std::fmt::Display for SyncError {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            Self::Unavailable(d) | Self::Transient(d) | Self::Persistence(d) => write!(f, "{d}"),
40            Self::RetryAfter { detail, .. } => write!(f, "{detail}"),
41            Self::TokenExpired => write!(f, "change token expired"),
42            Self::ZoneMissing => write!(f, "sync zone missing"),
43            Self::Cancelled => write!(f, "sync cancelled"),
44            Self::Conflict => write!(f, "server record changed"),
45        }
46    }
47}
48
49impl std::error::Error for SyncError {}
50
51pub type SyncResult<T> = Result<T, SyncError>;