use StorageConfig instead of ReplicaConfig

This commit is contained in:
Dustin J. Mitchell 2021-01-10 21:48:28 -05:00
parent b004b6cb93
commit 02d9c577ab
6 changed files with 30 additions and 21 deletions

View file

@ -3,7 +3,7 @@
use crate::argparse::{Command, Subcommand};
use config::Config;
use failure::{format_err, Fallible};
use taskchampion::{Replica, ReplicaConfig, Server, ServerConfig, Uuid};
use taskchampion::{Replica, Server, ServerConfig, StorageConfig, Uuid};
use termcolor::{ColorChoice, StandardStream};
mod cmd;
@ -104,8 +104,8 @@ pub(crate) fn invoke(command: Command, settings: Config) -> Fallible<()> {
fn get_replica(settings: &Config) -> Fallible<Replica> {
let taskdb_dir = settings.get_str("data_dir")?.into();
log::debug!("Replica data_dir: {:?}", taskdb_dir);
let replica_config = ReplicaConfig { taskdb_dir };
Ok(Replica::from_config(replica_config)?)
let storage_config = StorageConfig::OnDisk { taskdb_dir };
Ok(Replica::new(storage_config.into_storage()?))
}
/// Get the server for this invocation

View file

@ -1,7 +0,0 @@
use std::path::PathBuf;
/// The configuration required for a replica. Use with [`crate::Replica::from_config`].
pub struct ReplicaConfig {
/// Path containing the task DB.
pub taskdb_dir: PathBuf,
}

View file

@ -31,7 +31,6 @@ for more information about the design and usage of the tool.
*/
mod config;
mod errors;
mod replica;
pub mod server;
@ -41,9 +40,9 @@ mod taskdb;
mod utils;
mod workingset;
pub use config::ReplicaConfig;
pub use replica::Replica;
pub use server::{Server, ServerConfig};
pub use storage::StorageConfig;
pub use task::{Priority, Status, Tag, Task, TaskMut};
pub use workingset::WorkingSet;

View file

@ -1,9 +1,8 @@
use crate::config::ReplicaConfig;
use crate::errors::Error;
use crate::server::Server;
use crate::storage::{Operation, Storage, TaskMap};
use crate::task::{Status, Task};
use crate::taskdb::TaskDB;
use crate::storage::{KVStorage, Operation, TaskMap, Storage};
use crate::workingset::WorkingSet;
use chrono::Utc;
use failure::Fallible;
@ -36,13 +35,6 @@ impl Replica {
}
}
/// Construct a new replica from a configuration object. This is the common way
/// to create a new object.
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::storage::InMemoryStorage::new()))

View file

@ -0,0 +1,23 @@
use super::{InMemoryStorage, KVStorage, Storage};
use failure::Fallible;
use std::path::PathBuf;
/// The configuration required for a replica's storage.
pub enum StorageConfig {
/// Store the data on disk. This is the common choice.
OnDisk {
/// Path containing the task DB.
taskdb_dir: PathBuf,
},
/// Store the data in memory. This is only useful for testing.
InMemory,
}
impl StorageConfig {
pub fn into_storage(self) -> Fallible<Box<dyn Storage>> {
Ok(match self {
StorageConfig::OnDisk { taskdb_dir } => Box::new(KVStorage::new(taskdb_dir)?),
StorageConfig::InMemory => Box::new(InMemoryStorage::new()),
})
}
}

View file

@ -2,11 +2,13 @@ use failure::Fallible;
use std::collections::HashMap;
use uuid::Uuid;
mod config;
mod inmemory;
mod kv;
mod operation;
pub use self::kv::KVStorage;
pub use config::StorageConfig;
pub use inmemory::InMemoryStorage;
pub use operation::Operation;