From 9c703673fad5302b602266733f81dcc1f3f1fe25 Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Fri, 21 Jun 2024 18:01:41 +0200 Subject: [PATCH] Remove toml store --- crates/store/src/lib.rs | 1 - crates/store/src/toml_store.rs | 111 --------------------------------- src/config.rs | 6 -- src/main.rs | 9 +-- 4 files changed, 1 insertion(+), 126 deletions(-) delete mode 100644 crates/store/src/toml_store.rs diff --git a/crates/store/src/lib.rs b/crates/store/src/lib.rs index bbd448a..bf460f5 100644 --- a/crates/store/src/lib.rs +++ b/crates/store/src/lib.rs @@ -4,6 +4,5 @@ pub mod event; pub mod sqlite_store; pub mod store; pub mod timestamps; -pub mod toml_store; pub use error::Error; pub use store::CalendarStore; diff --git a/crates/store/src/toml_store.rs b/crates/store/src/toml_store.rs deleted file mode 100644 index 55b79a1..0000000 --- a/crates/store/src/toml_store.rs +++ /dev/null @@ -1,111 +0,0 @@ -use crate::event::Event; -use crate::{calendar::Calendar, CalendarStore, Error}; -use anyhow::anyhow; -use async_trait::async_trait; -use serde::{Deserialize, Serialize}; -use std::collections::{hash_map::Entry, HashMap}; -use tokio::{fs::File, io::AsyncWriteExt}; - -#[derive(Debug, Deserialize, Serialize)] -pub struct TomlCalendarStore { - calendars: HashMap, - events: HashMap>, - path: Option, -} - -impl TomlCalendarStore { - pub fn new(path: String) -> Self { - TomlCalendarStore { - calendars: HashMap::new(), - events: HashMap::new(), - path: Some(path), - } - } - - pub fn test() -> Self { - TomlCalendarStore { - calendars: HashMap::new(), - events: HashMap::new(), - path: None, - } - } - - pub async fn save(&self) -> anyhow::Result<()> { - let output = toml::to_string_pretty(&self)?; - if let Some(path) = &self.path { - let mut file = File::create(path).await?; - file.write_all(output.as_bytes()).await?; - } - Ok(()) - } -} - -#[async_trait] -impl CalendarStore for TomlCalendarStore { - async fn get_calendar(&self, id: &str) -> Result { - Ok(self.calendars.get(id).ok_or(Error::NotFound)?.clone()) - } - - async fn get_calendars(&self, user: &str) -> Result, Error> { - Ok(self - .calendars - .values() - .filter(|Calendar { owner, .. }| owner == user) - .cloned() - .collect()) - } - - async fn insert_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error> { - match self.calendars.entry(cid) { - Entry::Occupied(_) => Err(anyhow!("calendar already exists").into()), - Entry::Vacant(v) => { - v.insert(calendar); - self.save().await.unwrap(); - Ok(()) - } - } - } - - async fn update_calendar(&mut self, cid: String, calendar: Calendar) -> Result<(), Error> { - if let None = self.calendars.remove(&cid) { - // No old calendar to update, for consistency reasons throw an error - return Err(Error::NotFound); - } - self.calendars.insert(cid, calendar); - Ok(()) - } - - async fn delete_calendar(&mut self, cid: &str) -> Result<(), Error> { - self.events.remove(cid); - self.save().await.unwrap(); - Ok(()) - } - - async fn get_events(&self, cid: &str) -> Result, Error> { - if let Some(events) = self.events.get(cid) { - Ok(events.values().cloned().collect()) - } else { - Ok(Vec::new()) - } - } - - async fn get_event(&self, cid: &str, uid: &str) -> Result { - let events = self.events.get(cid).ok_or(anyhow!("not found"))?; - Ok(events.get(uid).ok_or(Error::NotFound)?.clone()) - } - - async fn upsert_event(&mut self, cid: String, uid: String, ics: String) -> Result<(), Error> { - let events = self.events.entry(cid).or_default(); - events.insert(uid.clone(), Event::from_ics(uid, ics)?); - self.save().await.unwrap(); - Ok(()) - } - - async fn delete_event(&mut self, cid: &str, uid: &str) -> Result<(), Error> { - if let Some(events) = self.events.get_mut(cid) { - events.remove(uid); - self.save().await?; - } - Ok(()) - } -} diff --git a/src/config.rs b/src/config.rs index 15416fb..24c2be3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,11 +7,6 @@ pub struct HttpConfig { pub port: u16, } -#[derive(Debug, Deserialize, Serialize)] -pub struct TomlCalendarStoreConfig { - pub db_path: String, -} - #[derive(Debug, Deserialize, Serialize)] pub struct SqliteCalendarStoreConfig { pub db_url: String, @@ -20,7 +15,6 @@ pub struct SqliteCalendarStoreConfig { #[derive(Debug, Deserialize, Serialize)] #[serde(tag = "backend", rename_all = "snake_case")] pub enum CalendarStoreConfig { - Toml(TomlCalendarStoreConfig), Sqlite(SqliteCalendarStoreConfig), } diff --git a/src/main.rs b/src/main.rs index 8c2b3c9..72c040c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,10 +3,9 @@ use actix_web::HttpServer; use anyhow::Result; use app::make_app; use clap::Parser; -use config::{CalendarStoreConfig, SqliteCalendarStoreConfig, TomlCalendarStoreConfig}; +use config::{CalendarStoreConfig, SqliteCalendarStoreConfig}; use rustical_auth::AuthProvider; use rustical_store::sqlite_store::{create_db_pool, SqliteCalendarStore}; -use rustical_store::toml_store::TomlCalendarStore; use rustical_store::CalendarStore; use std::fs; use std::sync::Arc; @@ -29,12 +28,6 @@ async fn get_cal_store( config: &CalendarStoreConfig, ) -> Result>> { let cal_store: Arc> = match &config { - CalendarStoreConfig::Toml(TomlCalendarStoreConfig { db_path }) => { - Arc::new(RwLock::new(match fs::read_to_string(db_path) { - Ok(content) => toml::from_str::(&content).unwrap(), - Err(_) => TomlCalendarStore::new(db_path.to_string()), - })) - } CalendarStoreConfig::Sqlite(SqliteCalendarStoreConfig { db_url }) => { let db = create_db_pool(db_url, migrate).await?; Arc::new(RwLock::new(SqliteCalendarStore::new(db)))