chore: first commit

This commit is contained in:
Alexander Navarro 2025-05-06 20:24:41 -04:00
commit 1d5a517395
13 changed files with 2396 additions and 0 deletions

45
src/readwise.rs Normal file
View 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"))
})
}