chore: first commit
This commit is contained in:
commit
1d5a517395
13 changed files with 2396 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
||||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/readwise-bulk-upload.iml" filepath="$PROJECT_DIR$/.idea/readwise-bulk-upload.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
11
.idea/readwise-bulk-upload.iml
generated
Normal file
11
.idea/readwise-bulk-upload.iml
generated
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="EMPTY_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
2214
Cargo.lock
generated
Normal file
2214
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
[package]
|
||||||
|
name = "readwise-bulk-upload"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[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" ] }
|
||||||
|
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"
|
||||||
13
src/config.rs
Normal file
13
src/config.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
#[derive(Debug, Parser)]
|
||||||
|
pub struct Args {
|
||||||
|
path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Args {
|
||||||
|
pub fn path(&self) -> &PathBuf {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
}
|
||||||
24
src/error.rs
Normal file
24
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}")]
|
||||||
|
Runtime(String),
|
||||||
|
|
||||||
|
#[error("{0}")]
|
||||||
|
Unhandled(&'static str),
|
||||||
|
|
||||||
|
#[error(transparent)]
|
||||||
|
Sqlx(#[from] sqlx::Error),
|
||||||
|
|
||||||
|
#[error(transparent)]
|
||||||
|
Io(#[from] tokio::io::Error),
|
||||||
|
|
||||||
|
#[error(transparent)]
|
||||||
|
ParseJson(#[from] serde_json::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
6
src/lib.rs
Normal file
6
src/lib.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
mod error;
|
||||||
|
pub mod sql;
|
||||||
|
pub mod config;
|
||||||
|
pub mod readwise;
|
||||||
|
|
||||||
|
pub use error::*;
|
||||||
23
src/main.rs
Normal file
23
src/main.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
use std::fs::File;
|
||||||
|
use clap::Parser;
|
||||||
|
use readwise_bulk_upload::config::Args;
|
||||||
|
use readwise_bulk_upload::readwise::Document;
|
||||||
|
use readwise_bulk_upload::sql::get_database;
|
||||||
|
use readwise_bulk_upload::{Error, Result};
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
let args = Args::parse();
|
||||||
|
|
||||||
|
let file = File::open(args.path())
|
||||||
|
.map_err(|_| Error::Runtime(format!(
|
||||||
|
r#"The file "{}" could not be open"#,
|
||||||
|
args.path().display()
|
||||||
|
)))?;
|
||||||
|
|
||||||
|
let documents: Vec<Document> = serde_json::from_reader(file)?;
|
||||||
|
|
||||||
|
let db = get_database().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
45
src/readwise.rs
Normal file
45
src/readwise.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
use serde::{de, Deserialize, Deserializer};
|
||||||
|
use chrono::{DateTime, Local};
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct Document {
|
||||||
|
#[serde(deserialize_with = "str_to_int")]
|
||||||
|
id: u64,
|
||||||
|
title: String ,
|
||||||
|
note: Option<String>,
|
||||||
|
excerpt: Option<String>,
|
||||||
|
url: String,
|
||||||
|
folder: String,
|
||||||
|
#[serde(deserialize_with = "single_or_vec")]
|
||||||
|
tags: Vec<String>,
|
||||||
|
created: DateTime<Local>,
|
||||||
|
cover: Option<String>,
|
||||||
|
#[serde(deserialize_with = "str_to_bool")]
|
||||||
|
favorite: bool
|
||||||
|
}
|
||||||
|
|
||||||
|
fn str_to_int<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u64, D::Error> {
|
||||||
|
Ok(match Value::deserialize(deserializer)? {
|
||||||
|
Value::String(s) => s.parse().map_err(de::Error::custom)?,
|
||||||
|
Value::Number(num) => num.as_u64().ok_or(de::Error::custom("Invalid number"))?,
|
||||||
|
_ => return Err(de::Error::custom("wrong type"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn str_to_bool<'de, D: Deserializer<'de>>(deserializer: D) -> Result<bool, D::Error> {
|
||||||
|
Ok(match Value::deserialize(deserializer)? {
|
||||||
|
Value::String(s) => s.parse().map_err(de::Error::custom)?,
|
||||||
|
Value::Bool(b) => b,
|
||||||
|
_ => return Err(de::Error::custom("wrong type"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn single_or_vec<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<String>, D::Error> {
|
||||||
|
Ok(match Value::deserialize(deserializer)? {
|
||||||
|
Value::String(s) => vec!(s.parse().map_err(de::Error::custom)?),
|
||||||
|
Value::Array(arr) => arr.into_iter().map(|a| a.to_string()).collect(),
|
||||||
|
Value::Null => Vec::new(),
|
||||||
|
_ => return Err(de::Error::custom("wrong type"))
|
||||||
|
})
|
||||||
|
}
|
||||||
23
src/sql.rs
Normal file
23
src/sql.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
use crate::Error;
|
||||||
|
use directories::ProjectDirs;
|
||||||
|
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode};
|
||||||
|
use sqlx::SqlitePool;
|
||||||
|
use tokio::fs;
|
||||||
|
|
||||||
|
pub async fn get_database() -> crate::Result<SqlitePool> {
|
||||||
|
let project_dir = ProjectDirs::from("", "", env!("CARGO_PKG_NAME"))
|
||||||
|
.ok_or(Error::Unhandled("Could not get standard directories"))?;
|
||||||
|
|
||||||
|
let database_file_path = project_dir.data_dir().join("db.sql");
|
||||||
|
|
||||||
|
fs::create_dir_all(project_dir.data_dir()).await?;
|
||||||
|
|
||||||
|
let opts = SqliteConnectOptions::new()
|
||||||
|
.filename(database_file_path)
|
||||||
|
.create_if_missing(true)
|
||||||
|
.journal_mode(SqliteJournalMode::Wal);
|
||||||
|
|
||||||
|
let pool = SqlitePool::connect_with(opts).await?;
|
||||||
|
|
||||||
|
Ok(pool)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue