Skip to content

Commit

Permalink
refactor commands
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Mar 29, 2024
1 parent 09d260d commit 3383d2b
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 56 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ pace_cli = { workspace = true }
pace_core = { workspace = true, features = ["cli"] }
pace_error = { workspace = true }
pace_service = { workspace = true }
pace_storage = { workspace = true }
pace_time = { workspace = true, features = ["cli", "db"] }
serde = { workspace = true }
serde_derive = { workspace = true }
Expand All @@ -136,6 +135,7 @@ assert_cmd = { workspace = true }
insta = { workspace = true, features = ["toml"] }
insta-cmd = { workspace = true }
once_cell = { workspace = true }
pace_storage = { workspace = true }
predicates = { workspace = true }
rstest = { workspace = true }
similar-asserts = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ description = "pace-service - service support library for pace"
getset = { workspace = true }
pace_core = { workspace = true, features = ["db"] }
pace_error = { workspace = true }
pace_storage = { workspace = true }
pace_time = { workspace = true, features = ["rusqlite"] }
tracing = { workspace = true }
typed-builder = { workspace = true }
Expand Down
47 changes: 47 additions & 0 deletions crates/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,50 @@
pub mod activity_store;

pub mod activity_tracker;

use std::sync::Arc;

use pace_core::prelude::{ActivityLogStorageKind, ActivityStorage, DatabaseEngineKind, PaceConfig};
use pace_error::{DatabaseStorageErrorKind, PaceResult};
use tracing::debug;

use pace_storage::storage::{
file::TomlActivityStorage, in_memory::InMemoryActivityStorage, sqlite::SqliteActivityStorage,
};

/// Get the storage backend from the configuration.
///
/// # Arguments
///
/// * `config` - The application configuration.
///
/// # Errors
///
/// This function should return an error if the storage backend cannot be created or is not supported.
///
/// # Returns
///
/// The storage backend.
pub fn get_storage_from_config(config: &PaceConfig) -> PaceResult<Arc<dyn ActivityStorage>> {
let storage: Arc<dyn ActivityStorage> = match config.storage().storage() {
ActivityLogStorageKind::File { location } => Arc::new(TomlActivityStorage::new(location)?),
ActivityLogStorageKind::Database { kind, url } => match kind {
DatabaseEngineKind::Sqlite => {
debug!("Connecting to database: {}", url);

Arc::new(SqliteActivityStorage::new(url.clone())?)
}
engine => {
return Err(
DatabaseStorageErrorKind::UnsupportedDatabaseEngine(engine.to_string()).into(),
)
}
},
ActivityLogStorageKind::InMemory => Arc::new(InMemoryActivityStorage::new()),
_ => return Err(DatabaseStorageErrorKind::StorageNotImplemented.into()),
};

debug!("Using storage backend: {:?}", storage);

Ok(storage)
}
47 changes: 0 additions & 47 deletions crates/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,3 @@ pub mod entities;
pub mod migration;
pub mod query;
pub mod storage;

use std::sync::Arc;

use pace_core::prelude::{ActivityLogStorageKind, ActivityStorage, DatabaseEngineKind, PaceConfig};
use pace_error::{DatabaseStorageErrorKind, PaceResult};
use tracing::debug;

use crate::storage::{
file::TomlActivityStorage, in_memory::InMemoryActivityStorage, sqlite::SqliteActivityStorage,
};

/// Get the storage backend from the configuration.
///
/// # Arguments
///
/// * `config` - The application configuration.
///
/// # Errors
///
/// This function should return an error if the storage backend cannot be created or is not supported.
///
/// # Returns
///
/// The storage backend.
pub fn get_storage_from_config(config: &PaceConfig) -> PaceResult<Arc<dyn ActivityStorage>> {
let storage: Arc<dyn ActivityStorage> = match config.storage().storage() {
ActivityLogStorageKind::File { location } => Arc::new(TomlActivityStorage::new(location)?),
ActivityLogStorageKind::Database { kind, url } => match kind {
DatabaseEngineKind::Sqlite => {
debug!("Connecting to database: {}", url);

Arc::new(SqliteActivityStorage::new(url.clone())?)
}
engine => {
return Err(
DatabaseStorageErrorKind::UnsupportedDatabaseEngine(engine.to_string()).into(),
)
}
},
ActivityLogStorageKind::InMemory => Arc::new(InMemoryActivityStorage::new()),
_ => return Err(DatabaseStorageErrorKind::StorageNotImplemented.into()),
};

debug!("Using storage backend: {:?}", storage);

Ok(storage)
}
2 changes: 1 addition & 1 deletion src/commands/adjust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};
use clap::Parser;
use pace_cli::commands::adjust::AdjustCommandOptions;
use pace_storage::get_storage_from_config;
use pace_service::get_storage_from_config;

use crate::prelude::PACE_APP;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/begin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};
use clap::Parser;
use pace_cli::commands::begin::BeginCommandOptions;
use pace_storage::get_storage_from_config;
use pace_service::get_storage_from_config;

use crate::prelude::PACE_APP;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};
use clap::Parser;
use pace_cli::commands::end::EndCommandOptions;
use pace_storage::get_storage_from_config;
use pace_service::get_storage_from_config;

use crate::prelude::PACE_APP;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/hold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};

use clap::Parser;
use pace_cli::commands::hold::HoldCommandOptions;
use pace_storage::get_storage_from_config;
use pace_service::get_storage_from_config;

use crate::prelude::PACE_APP;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};
use clap::Parser;
use pace_cli::commands::now::NowCommandOptions;
use pace_storage::get_storage_from_config;
use pace_service::get_storage_from_config;

use crate::prelude::PACE_APP;

Expand Down
2 changes: 1 addition & 1 deletion src/commands/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};
use clap::Parser;

use pace_cli::commands::reflect::ReflectCommandOptions;
use pace_storage::get_storage_from_config;
use pace_service::get_storage_from_config;

use crate::prelude::PACE_APP;

Expand Down
3 changes: 1 addition & 2 deletions src/commands/resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use pace_core::{
prelude::{ActivityQuerying, ActivityReadOps, ActivityStateManagement, SyncStorage},
};
use pace_error::UserMessage;
use pace_service::activity_store::ActivityStore;
use pace_storage::get_storage_from_config;
use pace_service::{activity_store::ActivityStore, get_storage_from_config};
use pace_time::{date_time::PaceDateTime, time_zone::PaceTimeZoneKind, Validate};

use crate::prelude::PACE_APP;
Expand Down

0 comments on commit 3383d2b

Please sign in to comment.