feat: add base database management

This commit is contained in:
Alexander Navarro 2025-05-07 14:36:26 -04:00
parent 1d5a517395
commit 1e3c235b78
11 changed files with 132 additions and 20 deletions

View file

@ -1,29 +1,23 @@
use serde::{de, Deserialize, Deserializer};
use chrono::{DateTime, Local};
use serde::{Deserialize, Deserializer, de};
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>,
pub struct DocumentPayload {
title: String,
summary: 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
published_date: DateTime<Local>,
location: String,
}
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"))
_ => return Err(de::Error::custom("wrong type")),
})
}
@ -31,15 +25,15 @@ fn str_to_bool<'de, D: Deserializer<'de>>(deserializer: D) -> Result<bool, D::Er
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"))
_ => 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::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"))
_ => return Err(de::Error::custom("wrong type")),
})
}