use ServerConfig::into_server instead of server::from_config

This commit is contained in:
Dustin J. Mitchell 2021-01-10 21:35:24 -05:00
parent 15ffc62279
commit b004b6cb93
6 changed files with 65 additions and 51 deletions

View file

@ -3,7 +3,7 @@
use crate::argparse::{Command, Subcommand}; use crate::argparse::{Command, Subcommand};
use config::Config; use config::Config;
use failure::{format_err, Fallible}; use failure::{format_err, Fallible};
use taskchampion::{server, Replica, ReplicaConfig, ServerConfig, Uuid}; use taskchampion::{Replica, ReplicaConfig, Server, ServerConfig, Uuid};
use termcolor::{ColorChoice, StandardStream}; use termcolor::{ColorChoice, StandardStream};
mod cmd; mod cmd;
@ -109,10 +109,10 @@ fn get_replica(settings: &Config) -> Fallible<Replica> {
} }
/// Get the server for this invocation /// Get the server for this invocation
fn get_server(settings: &Config) -> Fallible<Box<dyn server::Server>> { fn get_server(settings: &Config) -> Fallible<Box<dyn Server>> {
// if server_client_key and server_origin are both set, use // if server_client_key and server_origin are both set, use
// the remote server // the remote server
if let (Ok(client_key), Ok(origin)) = ( let config = if let (Ok(client_key), Ok(origin)) = (
settings.get_str("server_client_key"), settings.get_str("server_client_key"),
settings.get_str("server_origin"), settings.get_str("server_origin"),
) { ) {
@ -123,16 +123,17 @@ fn get_server(settings: &Config) -> Fallible<Box<dyn server::Server>> {
log::debug!("Using sync-server with origin {}", origin); log::debug!("Using sync-server with origin {}", origin);
log::debug!("Sync client ID: {}", client_key); log::debug!("Sync client ID: {}", client_key);
Ok(server::from_config(ServerConfig::Remote { ServerConfig::Remote {
origin, origin,
client_key, client_key,
encryption_secret: encryption_secret.as_bytes().to_vec(), encryption_secret: encryption_secret.as_bytes().to_vec(),
})?) }
} else { } else {
let server_dir = settings.get_str("server_dir")?.into(); let server_dir = settings.get_str("server_dir")?.into();
log::debug!("Using local sync-server at `{:?}`", server_dir); log::debug!("Using local sync-server at `{:?}`", server_dir);
Ok(server::from_config(ServerConfig::Local { server_dir })?) ServerConfig::Local { server_dir }
} };
Ok(config.into_server()?)
} }
/// Get a WriteColor implementation based on whether the output is a tty. /// Get a WriteColor implementation based on whether the output is a tty.

View file

@ -1,5 +1,5 @@
use std::io; use std::io;
use taskchampion::{server, storage, Replica, ServerConfig}; use taskchampion::{storage, Replica, Server, ServerConfig};
use tempdir::TempDir; use tempdir::TempDir;
pub(super) fn test_replica() -> Replica { pub(super) fn test_replica() -> Replica {
@ -7,10 +7,11 @@ pub(super) fn test_replica() -> Replica {
Replica::new(Box::new(storage)) Replica::new(Box::new(storage))
} }
pub(super) fn test_server(dir: &TempDir) -> Box<dyn server::Server> { pub(super) fn test_server(dir: &TempDir) -> Box<dyn Server> {
server::from_config(ServerConfig::Local { ServerConfig::Local {
server_dir: dir.path().to_path_buf(), server_dir: dir.path().to_path_buf(),
}) }
.into_server()
.unwrap() .unwrap()
} }

View file

@ -1,30 +1,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use uuid::Uuid;
/// The configuration required for a replica. Use with [`crate::Replica::from_config`]. /// The configuration required for a replica. Use with [`crate::Replica::from_config`].
pub struct ReplicaConfig { pub struct ReplicaConfig {
/// Path containing the task DB. /// Path containing the task DB.
pub taskdb_dir: PathBuf, 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 Key to identify and authenticate this replica to the server
client_key: Uuid,
/// Private encryption secret used to encrypt all data sent to the server. This can
/// be any suitably un-guessable string of bytes.
encryption_secret: Vec<u8>,
},
}

View file

@ -3,10 +3,14 @@
This crate implements the core of TaskChampion, the [replica](crate::Replica). This crate implements the core of TaskChampion, the [replica](crate::Replica).
# Replica
A TaskChampion replica is a local copy of a user's task data. As the name suggests, several A TaskChampion replica is a local copy of a user's task data. As the name suggests, several
replicas of the same data can exist (such as on a user's laptop and on their phone) and can replicas of the same data can exist (such as on a user's laptop and on their phone) and can
synchronize with one another. synchronize with one another.
Replicas are accessed using the [`Replica`](crate::replica) type.
# Task Storage # Task Storage
The [`storage`](crate::storage) module supports pluggable storage for a replica's data. The [`storage`](crate::storage) module supports pluggable storage for a replica's data.
@ -15,7 +19,10 @@ An implementation is provided, but users of this crate can provide their own imp
# Server # Server
Replica synchronization takes place against a server. Replica synchronization takes place against a server.
Create a server with [`ServerConfig`](crate::ServerConfig).
The [`server`](crate::server) module defines the interface a server must meet. The [`server`](crate::server) module defines the interface a server must meet.
Users can define their own server impelementations.
# See Also # See Also
@ -28,14 +35,15 @@ mod config;
mod errors; mod errors;
mod replica; mod replica;
pub mod server; pub mod server;
pub mod storage;
mod task; mod task;
mod taskdb; mod taskdb;
pub mod storage;
mod utils; mod utils;
mod workingset; mod workingset;
pub use config::{ReplicaConfig, ServerConfig}; pub use config::ReplicaConfig;
pub use replica::Replica; pub use replica::Replica;
pub use server::{Server, ServerConfig};
pub use task::{Priority, Status, Tag, Task, TaskMut}; pub use task::{Priority, Status, Tag, Task, TaskMut};
pub use workingset::WorkingSet; pub use workingset::WorkingSet;

View file

@ -0,0 +1,40 @@
use super::types::Server;
use super::{LocalServer, RemoteServer};
use failure::Fallible;
use std::path::PathBuf;
use uuid::Uuid;
/// The configuration for a replica's access to a sync server.
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 Key to identify and authenticate this replica to the server
client_key: Uuid,
/// Private encryption secret used to encrypt all data sent to the server. This can
/// be any suitably un-guessable string of bytes.
encryption_secret: Vec<u8>,
},
}
impl ServerConfig {
/// Get a server based on this configuration
pub fn into_server(self) -> Fallible<Box<dyn Server>> {
Ok(match self {
ServerConfig::Local { server_dir } => Box::new(LocalServer::new(server_dir)?),
ServerConfig::Remote {
origin,
client_key,
encryption_secret,
} => Box::new(RemoteServer::new(origin, client_key, encryption_secret)),
})
}
}

View file

@ -1,25 +1,12 @@
use crate::ServerConfig;
use failure::Fallible;
#[cfg(test)] #[cfg(test)]
pub(crate) mod test; pub(crate) mod test;
mod config;
mod local; mod local;
mod remote; mod remote;
mod types; mod types;
pub use config::ServerConfig;
pub use local::LocalServer; pub use local::LocalServer;
pub use remote::RemoteServer; pub use remote::RemoteServer;
pub use types::*; pub use types::*;
/// Create a new server based on the given configuration.
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_key,
encryption_secret,
} => Box::new(RemoteServer::new(origin, client_key, encryption_secret)),
})
}