use std::collections::BTreeMap;
use crate::error::SyncResult;
use crate::record::RawRecord;
#[derive(Debug, Clone, PartialEq)]
pub struct ItemHeader {
pub id: String,
pub created_at_ms: i64,
pub updated_at_ms: i64,
pub origin_device: Option<String>,
}
pub trait RecordMapper: Send + Sync {
type Item: Clone + Send + Sync;
fn record_type(&self) -> &str;
fn header(&self, item: &Self::Item) -> ItemHeader;
fn record_name(&self, id: &str) -> String {
id.to_string()
}
fn encode(&self, item: &Self::Item, record: &mut RawRecord);
fn decode(&self, record: &RawRecord) -> Result<Self::Item, String>;
fn tombstone_keep_fields(&self) -> &[&str] {
&[]
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ApplyOutcome {
Applied,
Deferred(String),
}
pub trait LocalStore<T>: Send + Sync {
fn all(&self) -> SyncResult<Vec<T>>;
fn remove(&self, id: &str) -> SyncResult<()>;
fn apply_remote(&self, items: &[T]) -> SyncResult<Vec<ApplyOutcome>>;
}
pub trait ConflictPolicy<T>: Send + Sync {
fn upload_local_over_remote(
&self,
local: &ItemHeader,
_local_item: &T,
remote: &ItemHeader,
_remote_item: &T,
) -> bool {
local.updated_at_ms > remote.updated_at_ms
}
fn accept_remote_over_local(&self, local: &ItemHeader, remote: &ItemHeader) -> bool {
remote.updated_at_ms > local.updated_at_ms
}
}
pub struct LastWriterWins;
impl<T> ConflictPolicy<T> for LastWriterWins {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SavePolicy {
Overwrite,
IfServerUnchanged,
}
#[derive(Debug, Default)]
pub struct ChangePage {
pub records: Vec<RawRecord>,
pub deleted_record_names: Vec<String>,
pub new_token: Option<Vec<u8>>,
pub more_coming: bool,
}
#[derive(Debug, Clone)]
pub struct RecordOutcome {
pub record_name: String,
pub result: SyncResult<()>,
pub server_record: Option<RawRecord>,
}
pub trait Backend: Send + Sync {
fn prepare(&self) -> SyncResult<()>;
fn fetch_changes(&self, since: Option<&[u8]>) -> SyncResult<ChangePage>;
fn fetch_record(&self, record_name: &str) -> SyncResult<Option<RawRecord>>;
fn save_records(
&self,
records: &[RawRecord],
policy: SavePolicy,
) -> SyncResult<Vec<RecordOutcome>>;
fn max_batch(&self) -> usize {
200
}
}
pub trait AuthProvider: Send + Sync {
fn enabled(&self) -> bool {
true
}
fn availability(&self) -> SyncResult<()>;
fn generation(&self) -> u64 {
0
}
fn account_scope(&self) -> Option<String> {
None
}
fn bearer(&self) -> SyncResult<Option<String>> {
Ok(None)
}
}
pub struct AlwaysAvailable;
impl AuthProvider for AlwaysAvailable {
fn availability(&self) -> SyncResult<()> {
Ok(())
}
}
pub trait Clock: Send + Sync {
fn now_ms(&self) -> i64;
}
pub struct SystemClock;
impl Clock for SystemClock {
fn now_ms(&self) -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as i64
}
}
pub trait SyncObserver: Send + Sync {
fn event(&self, name: &'static str, detail: &str);
}
pub struct NullObserver;
impl SyncObserver for NullObserver {
fn event(&self, _name: &'static str, _detail: &str) {}
}
pub type Extras = BTreeMap<String, serde_json::Value>;
pub trait PassHooks: Send + Sync {
fn outbound(&self, _extras: &mut Extras) -> Vec<RawRecord> {
Vec::new()
}
fn on_foreign_records(&self, _records: &[RawRecord], _extras: &mut Extras) -> Vec<RawRecord> {
Vec::new()
}
fn on_account_switch(&self, _from: &str, _to: &str) {}
}
pub struct NoHooks;
impl PassHooks for NoHooks {}