chore: first commit
This commit is contained in:
commit
1d5a517395
13 changed files with 2396 additions and 0 deletions
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