diff --git a/.idea/readwise-bulk-upload.iml b/.idea/readwise-bulk-upload.iml index cf84ae4..cc58e9c 100644 --- a/.idea/readwise-bulk-upload.iml +++ b/.idea/readwise-bulk-upload.iml @@ -2,7 +2,9 @@ - + + + diff --git a/Cargo.lock b/Cargo.lock index dfdfe03..5daa196 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,6 +279,27 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" +[[package]] +name = "cli" +version = "0.1.0" +dependencies = [ + "chrono", + "clap", + "directories", + "figment", + "futures", + "lib_sync_core", + "serde", + "serde_json", + "sqlx", + "tabled", + "thiserror", + "tokio", + "tracing", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "colorchoice" version = "1.0.3" @@ -916,6 +937,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" @@ -1321,26 +1362,6 @@ dependencies = [ "getrandom 0.2.16", ] -[[package]] -name = "readwise-bulk-upload" -version = "0.1.0" -dependencies = [ - "chrono", - "clap", - "directories", - "figment", - "futures", - "serde", - "serde_json", - "sqlx", - "tabled", - "thiserror", - "tokio", - "tracing", - "tracing-core", - "tracing-subscriber", -] - [[package]] name = "redox_syscall" version = "0.5.12" @@ -2219,6 +2240,10 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "web" +version = "0.1.0" + [[package]] name = "whoami" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index 3f51301..df1befe 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", "web", +] diff --git a/cli/Cargo.toml b/cli/Cargo.toml new file mode 100644 index 0000000..c056a52 --- /dev/null +++ b/cli/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "cli" +version = "0.1.0" +edition = "2024" + +[[bin]] +name = "readwise" +path = "bin/readwise/main.rs" + +[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/readwise.rs b/cli/bin/readwise/external_interface.rs similarity index 96% rename from src/readwise.rs rename to cli/bin/readwise/external_interface.rs index e3d7a42..13b2f65 100644 --- a/src/readwise.rs +++ b/cli/bin/readwise/external_interface.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/src/main.rs b/cli/bin/readwise/main.rs similarity index 59% rename from src/main.rs rename to cli/bin/readwise/main.rs index ff1f2ac..bdfc584 100644 --- a/src/main.rs +++ b/cli/bin/readwise/main.rs @@ -1,15 +1,18 @@ use clap::{CommandFactory, Parser}; +use directories::ProjectDirs; use figment::{ - providers::{Env, Serialized}, Figment, + providers::{Env, Serialized}, }; -use readwise_bulk_upload::config::{Command, Config}; -use readwise_bulk_upload::readwise::DocumentPayload; -use readwise_bulk_upload::task_manager::{TaskManager, TaskStatus}; -use readwise_bulk_upload::{Error, Result}; +use lib_sync_core::task_manager::{TaskManager, TaskStatus}; +use cli::config::{Command, Config}; +use cli::{Error, Result}; use std::fs::File; use tabled::Table; use tracing_subscriber; +use crate::external_interface::DocumentPayload; + +mod external_interface; #[tokio::main] async fn main() -> Result<()> { @@ -29,7 +32,12 @@ async fn main() -> Result<()> { } async fn run(command: &Command) -> Result<()> { - let task_manager = TaskManager::new().await?; + let project_dir = ProjectDirs::from("", "", "synchronizator_readwise").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(|_| { @@ -41,20 +49,23 @@ async fn run(command: &Command) -> Result<()> { let documents: Vec = serde_json::from_reader(file)?; - task_manager.load_tasks(documents).await?; } Command::Query => { - let tasks = task_manager.get_tasks::(None, Some(25)).await?; + let tasks = task_manager + .get_tasks::(None, Some(25)) + .await?; println!("{}", Table::new(tasks)); } Command::Run => { - task_manager.run_tasks::(|task| { - println!("{}", task.get_key()); - - TaskStatus::Completed - }).await?; + task_manager + .run_tasks::(|task| { + println!("{}", task.get_key()); + + TaskStatus::Completed + }) + .await?; } Command::None => { Config::command().print_help()?; 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 52% rename from src/lib.rs rename to cli/src/lib.rs index f63d5eb..dabf39f 100644 --- a/src/lib.rs +++ b/cli/src/lib.rs @@ -1,6 +1,4 @@ -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/cli/src/main.rs b/cli/src/main.rs new file mode 100644 index 0000000..75c0be7 --- /dev/null +++ b/cli/src/main.rs @@ -0,0 +1,23 @@ +use clap::Parser; +use figment::{ + providers::{Env, Serialized}, + Figment, +}; +use cli::config::Config; +use cli::Result; +use tracing_subscriber; + +#[tokio::main] +async fn main() -> Result<()> { + let cli = Config::parse(); + let args: Config = Figment::new() + .merge(Serialized::defaults(&cli)) + .merge(Env::prefixed("APP_")) + .extract()?; + + tracing_subscriber::fmt() + .with_max_level(args.log_level()) + .init(); + + Ok(()) +} 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) } diff --git a/web/Cargo.toml b/web/Cargo.toml new file mode 100644 index 0000000..52dfac4 --- /dev/null +++ b/web/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "web" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/web/src/main.rs b/web/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/web/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}