refactor: move library into it's own crate
This commit is contained in:
parent
91d702088d
commit
b31502fb37
13 changed files with 138 additions and 34 deletions
3
.idea/readwise-bulk-upload.iml
generated
3
.idea/readwise-bulk-upload.iml
generated
|
|
@ -2,7 +2,8 @@
|
|||
<module type="EMPTY_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/cli/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/lib_sync_core/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
|
|
|
|||
21
Cargo.lock
generated
21
Cargo.lock
generated
|
|
@ -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",
|
||||
|
|
|
|||
24
Cargo.toml
24
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"
|
||||
members = [
|
||||
"cli", "lib_sync_core",
|
||||
]
|
||||
|
|
|
|||
22
cli/Cargo.toml
Normal file
22
cli/Cargo.toml
Normal file
|
|
@ -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"
|
||||
|
|
@ -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),
|
||||
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
mod error;
|
||||
pub mod task_manager;
|
||||
pub mod config;
|
||||
pub mod readwise;
|
||||
mod error;
|
||||
|
||||
pub use error::*;
|
||||
|
|
@ -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(|_| {
|
||||
|
|
@ -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;
|
||||
20
lib_sync_core/Cargo.toml
Normal file
20
lib_sync_core/Cargo.toml
Normal file
|
|
@ -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"
|
||||
24
lib_sync_core/src/error.rs
Normal file
24
lib_sync_core/src/error.rs
Normal file
|
|
@ -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<T> = std::result::Result<T, Error>;
|
||||
19
lib_sync_core/src/lib.rs
Normal file
19
lib_sync_core/src/lib.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<T: DeserializeOwned + Send + Unpin + 'static + Display> _Task for T {}
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct TaskManager {
|
||||
base_path: PathBuf,
|
||||
pool: SqlitePool,
|
||||
}
|
||||
|
||||
impl TaskManager {
|
||||
pub async fn new() -> Result<TaskManager, Error> {
|
||||
pub async fn new<P: Into<PathBuf>>(base_path: P) -> Result<TaskManager, Error> {
|
||||
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<SqlitePool> {
|
||||
let project_dir = ProjectDirs::from("", "", env!("CARGO_PKG_NAME"))
|
||||
.ok_or(Error::Unhandled("Could not get standard directories"))?;
|
||||
async fn connect_database<P: Into<PathBuf>>(base_path: P) -> crate::Result<SqlitePool> {
|
||||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue