From b31502fb373361026caa3ff8e12e78483586689f Mon Sep 17 00:00:00 2001 From: aleidk Date: Thu, 15 May 2025 11:38:00 -0400 Subject: [PATCH] refactor: move library into it's own crate --- .idea/readwise-bulk-upload.iml | 3 ++- Cargo.lock | 21 +++++++++++++++++++ Cargo.toml | 24 +++++----------------- cli/Cargo.toml | 22 ++++++++++++++++++++ {src => cli/src}/config.rs | 0 {src => cli/src}/error.rs | 3 +++ {src => cli/src}/lib.rs | 3 +-- {src => cli/src}/main.rs | 9 ++++++-- {src => cli/src}/readwise.rs | 2 +- lib_sync_core/Cargo.toml | 20 ++++++++++++++++++ lib_sync_core/src/error.rs | 24 ++++++++++++++++++++++ lib_sync_core/src/lib.rs | 19 +++++++++++++++++ {src => lib_sync_core/src}/task_manager.rs | 22 ++++++++++++-------- 13 files changed, 138 insertions(+), 34 deletions(-) create mode 100644 cli/Cargo.toml rename {src => cli/src}/config.rs (100%) rename {src => cli/src}/error.rs (88%) rename {src => cli/src}/lib.rs (74%) rename {src => cli/src}/main.rs (82%) rename {src => cli/src}/readwise.rs (96%) create mode 100644 lib_sync_core/Cargo.toml create mode 100644 lib_sync_core/src/error.rs create mode 100644 lib_sync_core/src/lib.rs rename {src => lib_sync_core/src}/task_manager.rs (89%) diff --git a/.idea/readwise-bulk-upload.iml b/.idea/readwise-bulk-upload.iml index cf84ae4..2b50503 100644 --- a/.idea/readwise-bulk-upload.iml +++ b/.idea/readwise-bulk-upload.iml @@ -2,7 +2,8 @@ - + + diff --git a/Cargo.lock b/Cargo.lock index dfdfe03..12b188e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -916,6 +916,26 @@ dependencies = [ "spin", ] +[[package]] +name = "lib_sync_core" +version = "0.1.0" +dependencies = [ + "chrono", + "clap", + "directories", + "figment", + "futures", + "serde", + "serde_json", + "sqlx", + "tabled", + "thiserror", + "tokio", + "tracing", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "libc" version = "0.2.172" @@ -1330,6 +1350,7 @@ dependencies = [ "directories", "figment", "futures", + "lib_sync_core", "serde", "serde_json", "sqlx", diff --git a/Cargo.toml b/Cargo.toml index 3f51301..37dcffc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,20 +1,6 @@ -[package] -name = "readwise-bulk-upload" -version = "0.1.0" -edition = "2024" +[workspace] +resolver = "3" -[dependencies] -thiserror = "2.0.12" -directories = "6.0.0" -tokio = { version = "1.45.0", features = ["default", "rt", "rt-multi-thread", "macros"] } -sqlx = { version = "0.8", features = [ "runtime-tokio", "sqlite", "chrono", "migrate" ] } -clap = { version = "4.5.37", features = ["derive"] } -serde = { version = "1.0.219", features = ["derive"] } -chrono = {version = "0.4.41", features = ["serde"]} -serde_json = "1.0.140" -tracing = "0.1.41" -tracing-subscriber = { version = "0.3.19" , features = ["env-filter"]} -figment = { version = "0.10.19", features = ["env"] } -tracing-core = "0.1.33" -tabled = "0.19.0" -futures = "0.3.31" \ No newline at end of file +members = [ + "cli", "lib_sync_core", +] diff --git a/cli/Cargo.toml b/cli/Cargo.toml new file mode 100644 index 0000000..410a10d --- /dev/null +++ b/cli/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "readwise-bulk-upload" +version = "0.1.0" +edition = "2024" + +[dependencies] +lib_sync_core = {path = "../lib_sync_core"} + +directories = "6.0.0" +tokio = { version = "1.45.0", features = ["default", "rt", "rt-multi-thread", "macros"] } +sqlx = { version = "0.8", features = [ "runtime-tokio", "sqlite", "chrono", "migrate" ] } +clap = { version = "4.5.37", features = ["derive"] } +serde = { version = "1.0.219", features = ["derive"] } +chrono = {version = "0.4.41", features = ["serde"]} +serde_json = "1.0.140" +tracing = "0.1.41" +tracing-subscriber = { version = "0.3.19" , features = ["env-filter"]} +figment = { version = "0.10.19", features = ["env"] } +tracing-core = "0.1.33" +tabled = "0.19.0" +futures = "0.3.31" +thiserror = "2.0.12" \ No newline at end of file diff --git a/src/config.rs b/cli/src/config.rs similarity index 100% rename from src/config.rs rename to cli/src/config.rs diff --git a/src/error.rs b/cli/src/error.rs similarity index 88% rename from src/error.rs rename to cli/src/error.rs index b55b32e..36572b2 100644 --- a/src/error.rs +++ b/cli/src/error.rs @@ -11,6 +11,9 @@ pub enum Error { #[error("{0}")] Unhandled(&'static str), + #[error(transparent)] + Sync(#[from] lib_sync_core::error::Error), + #[error(transparent)] Sqlx(#[from] sqlx::Error), diff --git a/src/lib.rs b/cli/src/lib.rs similarity index 74% rename from src/lib.rs rename to cli/src/lib.rs index f63d5eb..acba89d 100644 --- a/src/lib.rs +++ b/cli/src/lib.rs @@ -1,6 +1,5 @@ -mod error; -pub mod task_manager; pub mod config; pub mod readwise; +mod error; pub use error::*; \ No newline at end of file diff --git a/src/main.rs b/cli/src/main.rs similarity index 82% rename from src/main.rs rename to cli/src/main.rs index ff1f2ac..23acee5 100644 --- a/src/main.rs +++ b/cli/src/main.rs @@ -5,9 +5,10 @@ use figment::{ }; use readwise_bulk_upload::config::{Command, Config}; use readwise_bulk_upload::readwise::DocumentPayload; -use readwise_bulk_upload::task_manager::{TaskManager, TaskStatus}; +use lib_sync_core::task_manager::{TaskManager, TaskStatus}; use readwise_bulk_upload::{Error, Result}; use std::fs::File; +use directories::ProjectDirs; use tabled::Table; use tracing_subscriber; @@ -29,7 +30,11 @@ async fn main() -> Result<()> { } async fn run(command: &Command) -> Result<()> { - let task_manager = TaskManager::new().await?; + let project_dir = ProjectDirs::from("", "", env!("CARGO_PKG_NAME")) + .ok_or(lib_sync_core::error::Error::Unhandled("Could not get standard directories"))?; + + let task_manager = TaskManager::new(project_dir.data_dir()).await?; + match command { Command::LoadTasks { path } => { let file = File::open(path).map_err(|_| { diff --git a/src/readwise.rs b/cli/src/readwise.rs similarity index 96% rename from src/readwise.rs rename to cli/src/readwise.rs index e3d7a42..13b2f65 100644 --- a/src/readwise.rs +++ b/cli/src/readwise.rs @@ -1,4 +1,4 @@ -use crate::task_manager::TaskPayload; +use lib_sync_core::task_manager::TaskPayload; use chrono::{DateTime, Local}; use serde::{de, Deserialize, Deserializer, Serialize}; use serde_json::Value; diff --git a/lib_sync_core/Cargo.toml b/lib_sync_core/Cargo.toml new file mode 100644 index 0000000..c6d73f7 --- /dev/null +++ b/lib_sync_core/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "lib_sync_core" +version = "0.1.0" +edition = "2024" + +[dependencies] +directories = "6.0.0" +tokio = { version = "1.45.0", features = ["default", "rt", "rt-multi-thread", "macros"] } +sqlx = { version = "0.8", features = [ "runtime-tokio", "sqlite", "chrono", "migrate" ] } +clap = { version = "4.5.37", features = ["derive"] } +serde = { version = "1.0.219", features = ["derive"] } +chrono = {version = "0.4.41", features = ["serde"]} +serde_json = "1.0.140" +tracing = "0.1.41" +tracing-subscriber = { version = "0.3.19" , features = ["env-filter"]} +figment = { version = "0.10.19", features = ["env"] } +tracing-core = "0.1.33" +tabled = "0.19.0" +futures = "0.3.31" +thiserror = "2.0.12" diff --git a/lib_sync_core/src/error.rs b/lib_sync_core/src/error.rs new file mode 100644 index 0000000..93c8a0b --- /dev/null +++ b/lib_sync_core/src/error.rs @@ -0,0 +1,24 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum Error { + #[error("{0}")] + Exception(&'static str), + + #[error("{0}")] + Unhandled(&'static str), + + #[error(transparent)] + Io(#[from] tokio::io::Error), + + #[error(transparent)] + Sqlx(#[from] sqlx::Error), + + #[error(transparent)] + Migration(#[from] sqlx::migrate::MigrateError), + + #[error(transparent)] + ParseJson(#[from] serde_json::Error), +} + +pub type Result = std::result::Result; \ No newline at end of file diff --git a/lib_sync_core/src/lib.rs b/lib_sync_core/src/lib.rs new file mode 100644 index 0000000..810ba8e --- /dev/null +++ b/lib_sync_core/src/lib.rs @@ -0,0 +1,19 @@ +pub mod error; + +pub(crate) use error::*; +pub mod task_manager; + +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/src/task_manager.rs b/lib_sync_core/src/task_manager.rs similarity index 89% rename from src/task_manager.rs rename to lib_sync_core/src/task_manager.rs index 11401e2..57a2556 100644 --- a/src/task_manager.rs +++ b/lib_sync_core/src/task_manager.rs @@ -1,4 +1,4 @@ -use crate::Error; +use crate::error::Error; use chrono::Utc; use directories::ProjectDirs; use futures::{StreamExt, TryStreamExt}; @@ -7,6 +7,7 @@ use serde::Serialize; use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode}; use sqlx::{QueryBuilder, Sqlite, SqlitePool}; use std::fmt::Display; +use std::path::PathBuf; use tabled::Tabled; use tokio::fs; use tracing::{info, instrument}; @@ -79,23 +80,26 @@ impl _Task for T {} #[derive(Debug)] pub struct TaskManager { + base_path: PathBuf, pool: SqlitePool, } impl TaskManager { - pub async fn new() -> Result { + pub async fn new>(base_path: P) -> Result { + let base_path = base_path.into(); + let pool = Self::connect_database(base_path.clone()).await?; Ok(Self { - pool: Self::connect_database().await?, + base_path, + pool, }) } - async fn connect_database() -> crate::Result { - let project_dir = ProjectDirs::from("", "", env!("CARGO_PKG_NAME")) - .ok_or(Error::Unhandled("Could not get standard directories"))?; + async fn connect_database>(base_path: P) -> crate::Result { + let base_path = base_path.into(); - let database_file_path = project_dir.data_dir().join("db.sql"); + let database_file_path = base_path.join("db.sql"); - fs::create_dir_all(project_dir.data_dir()).await?; + fs::create_dir_all(base_path).await?; let opts = SqliteConnectOptions::new() .filename(database_file_path) @@ -104,7 +108,7 @@ impl TaskManager { let pool = SqlitePool::connect_with(opts).await?; - sqlx::migrate!("./migrations").run(&pool).await?; + sqlx::migrate!("../migrations").run(&pool).await?; Ok(pool) }