mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 03:32:15 +00:00
Remove toml store
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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<String, Calendar>,
|
||||
events: HashMap<String, HashMap<String, Event>>,
|
||||
path: Option<String>,
|
||||
}
|
||||
|
||||
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<Calendar, Error> {
|
||||
Ok(self.calendars.get(id).ok_or(Error::NotFound)?.clone())
|
||||
}
|
||||
|
||||
async fn get_calendars(&self, user: &str) -> Result<Vec<Calendar>, 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<Vec<Event>, 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<Event, Error> {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Arc<RwLock<dyn CalendarStore>>> {
|
||||
let cal_store: Arc<RwLock<dyn CalendarStore>> = match &config {
|
||||
CalendarStoreConfig::Toml(TomlCalendarStoreConfig { db_path }) => {
|
||||
Arc::new(RwLock::new(match fs::read_to_string(db_path) {
|
||||
Ok(content) => toml::from_str::<TomlCalendarStore>(&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)))
|
||||
|
||||
Reference in New Issue
Block a user