Factor replica and sync configuration into simple owned structs

This commit is contained in:
Dustin J. Mitchell 2020-11-28 16:57:32 -05:00
parent 87596bb1f0
commit 8af7ba286d
16 changed files with 81 additions and 23 deletions

View file

@ -0,0 +1,26 @@
use std::path::PathBuf;
use uuid::Uuid;
/// The configuration required for a replica. Use with [`crate::Replica::from_config`].
pub struct ReplicaConfig {
/// Path containing the task DB.
pub taskdb_dir: PathBuf,
}
/// The configuration for a replica's access to a sync server. Use with
/// [`crate::server::from_config`].
pub enum ServerConfig {
/// A local task database, for situations with a single replica.
Local {
/// Path containing the server's DB
server_dir: PathBuf,
},
/// A remote taskchampion-sync-server instance
Remote {
/// Sync server "origin"; a URL with schema and hostname but no path or trailing `/`
origin: String,
/// Client ID to identify this replica to the server
client_id: Uuid,
},
}

View file

@ -23,6 +23,7 @@ for more information about the design and usage of the tool.
*/
mod config;
mod errors;
mod replica;
pub mod server;
@ -31,6 +32,7 @@ mod taskdb;
pub mod taskstorage;
mod utils;
pub use config::{ReplicaConfig, ServerConfig};
pub use replica::Replica;
pub use task::Priority;
pub use task::Status;

View file

@ -1,8 +1,9 @@
use crate::config::ReplicaConfig;
use crate::errors::Error;
use crate::server::Server;
use crate::task::{Status, Task};
use crate::taskdb::TaskDB;
use crate::taskstorage::{Operation, TaskMap, TaskStorage};
use crate::taskstorage::{KVStorage, Operation, TaskMap, TaskStorage};
use chrono::Utc;
use failure::Fallible;
use std::collections::HashMap;
@ -21,6 +22,11 @@ impl Replica {
}
}
pub fn from_config(config: ReplicaConfig) -> Fallible<Replica> {
let storage = Box::new(KVStorage::new(config.taskdb_dir)?);
Ok(Replica::new(storage))
}
#[cfg(test)]
pub fn new_inmemory() -> Replica {
Replica::new(Box::new(crate::taskstorage::InMemoryStorage::new()))
@ -140,7 +146,7 @@ impl Replica {
}
/// Synchronize this replica against the given server.
pub fn sync(&mut self, server: &mut dyn Server) -> Fallible<()> {
pub fn sync(&mut self, server: &mut Box<dyn Server>) -> Fallible<()> {
self.taskdb.sync(server)
}

View file

@ -25,7 +25,7 @@ pub struct LocalServer<'t> {
impl<'t> LocalServer<'t> {
/// A test server has no notion of clients, signatures, encryption, etc.
pub fn new(directory: &Path) -> Fallible<LocalServer> {
pub fn new<P: AsRef<Path>>(directory: P) -> Fallible<LocalServer<'t>> {
let mut config = Config::default(directory);
config.bucket("versions", None);
config.bucket("numbers", None);

View file

@ -1,3 +1,6 @@
use crate::ServerConfig;
use failure::Fallible;
#[cfg(test)]
pub(crate) mod test;
@ -8,3 +11,12 @@ mod types;
pub use local::LocalServer;
pub use remote::RemoteServer;
pub use types::*;
pub fn from_config(config: ServerConfig) -> Fallible<Box<dyn Server>> {
Ok(match config {
ServerConfig::Local { server_dir } => Box::new(LocalServer::new(server_dir)?),
ServerConfig::Remote { origin, client_id } => {
Box::new(RemoteServer::new(origin, client_id))
}
})
}

View file

@ -164,7 +164,7 @@ impl TaskDB {
}
/// Sync to the given server, pulling remote changes and pushing local changes.
pub fn sync(&mut self, server: &mut dyn Server) -> Fallible<()> {
pub fn sync(&mut self, server: &mut Box<dyn Server>) -> Fallible<()> {
let mut txn = self.storage.txn()?;
// retry synchronizing until the server accepts our version (this allows for races between
@ -542,7 +542,7 @@ mod tests {
#[test]
fn test_sync() {
let mut server = TestServer::new();
let mut server: Box<dyn Server> = Box::new(TestServer::new());
let mut db1 = newdb();
db1.sync(&mut server).unwrap();
@ -602,7 +602,7 @@ mod tests {
#[test]
fn test_sync_create_delete() {
let mut server = TestServer::new();
let mut server: Box<dyn Server> = Box::new(TestServer::new());
let mut db1 = newdb();
db1.sync(&mut server).unwrap();
@ -692,7 +692,7 @@ mod tests {
// and delete operations that results in a task existing in one TaskDB but not existing in
// another. So, the generated sequences focus on a single task UUID.
fn transform_sequences_of_operations(action_sequence in action_sequence_strategy()) {
let mut server = TestServer::new();
let mut server: Box<dyn Server> = Box::new(TestServer::new());
let mut dbs = [newdb(), newdb(), newdb()];
for (action, db) in action_sequence {

View file

@ -2,11 +2,13 @@ use failure::Fallible;
use std::collections::HashMap;
use uuid::Uuid;
#[cfg(test)]
mod inmemory;
mod kv;
mod operation;
pub use self::kv::KVStorage;
#[cfg(test)]
pub use inmemory::InMemoryStorage;
pub use operation::Operation;