use std::sync::Arc;
use crate::{
CollectionId, Href,
base::Storage,
sync::{TwoWaySync, mode::Mode},
};
#[derive(Debug, Clone)]
pub enum CollectionDescription {
Id {
id: CollectionId,
},
Href {
href: Href,
},
}
impl CollectionDescription {
pub(crate) fn alias(&self) -> String {
match self {
CollectionDescription::Id { id } => id.to_string(),
CollectionDescription::Href { href } => format!("href:{href}"),
}
}
}
#[derive(Debug, Clone)]
pub enum SyncedCollection {
Direct {
description: CollectionDescription,
},
Mapped {
alias: String,
a: CollectionDescription,
b: CollectionDescription,
},
}
impl SyncedCollection {
#[must_use]
pub fn direct(id: CollectionId) -> Self {
SyncedCollection::Direct {
description: CollectionDescription::Id { id },
}
}
}
#[derive(Clone)]
pub struct StoragePair {
pub(super) storage_a: Arc<dyn Storage>,
pub(super) storage_b: Arc<dyn Storage>,
pub(super) mappings: Arc<Vec<SyncedCollection>>,
pub(super) all_from_a: bool,
pub(super) all_from_b: bool,
pub(super) on_empty: OnEmpty,
pub(super) on_delete: OnDelete,
pub(super) mode: Arc<dyn Mode>,
}
impl StoragePair {
#[must_use]
pub fn new(storage_a: Arc<dyn Storage>, storage_b: Arc<dyn Storage>) -> StoragePair {
StoragePair {
storage_a,
storage_b,
mappings: Arc::default(),
all_from_a: false,
all_from_b: false,
on_empty: OnEmpty::Skip,
on_delete: OnDelete::Sync,
mode: Arc::new(TwoWaySync),
}
}
#[must_use]
pub fn with_mapping(mut self, mapping: SyncedCollection) -> Self {
let mut mappings = Arc::unwrap_or_clone(self.mappings);
mappings.push(mapping);
self.mappings = Arc::from(mappings);
self
}
#[must_use]
pub fn with_all_from_a(mut self) -> Self {
self.all_from_a = true;
self
}
#[must_use]
pub fn with_all_from_b(mut self) -> Self {
self.all_from_b = true;
self
}
#[must_use]
pub fn storage_a(&self) -> &Arc<dyn Storage> {
&self.storage_a
}
#[must_use]
pub fn storage_b(&self) -> &Arc<dyn Storage> {
&self.storage_b
}
#[must_use]
pub fn on_empty(mut self, action: OnEmpty) -> Self {
self.on_empty = action;
self
}
#[must_use]
pub fn on_delete(mut self, action: OnDelete) -> Self {
self.on_delete = action;
self
}
#[must_use]
pub fn with_mode(mut self, mode: Arc<dyn Mode>) -> Self {
self.mode = mode;
self
}
}
#[derive(Debug, PartialEq, Default, Clone, Copy)]
pub enum OnEmpty {
#[default]
Skip,
Sync,
}
#[derive(Debug, PartialEq, Default, Clone, Copy)]
pub enum OnDelete {
Skip,
#[default]
Sync,
}