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

@@ -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)]
pub(crate) mod test;
mod config;
mod local;
mod remote;
mod types;
pub use config::ServerConfig;
pub use local::LocalServer;
pub use remote::RemoteServer;
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)),
})
}