mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 19:22:26 +00:00
Switch db from json to toml
This commit is contained in:
@@ -49,15 +49,15 @@ pub trait CalendarStore: Send + Sync + 'static {
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct JsonCalendarStore {
|
||||
pub struct TomlCalendarStore {
|
||||
calendars: HashMap<String, Calendar>,
|
||||
events: HashMap<String, Event>,
|
||||
events: HashMap<String, HashMap<String, Event>>,
|
||||
path: String,
|
||||
}
|
||||
|
||||
impl JsonCalendarStore {
|
||||
impl TomlCalendarStore {
|
||||
pub fn new(path: String) -> Self {
|
||||
JsonCalendarStore {
|
||||
TomlCalendarStore {
|
||||
calendars: HashMap::new(),
|
||||
events: HashMap::new(),
|
||||
path,
|
||||
@@ -66,8 +66,8 @@ impl JsonCalendarStore {
|
||||
|
||||
pub async fn save(&self) -> Result<()> {
|
||||
let mut file = File::create(&self.path).await?;
|
||||
let json = serde_json::to_string_pretty(&self)?;
|
||||
file.write_all(json.as_bytes()).await?;
|
||||
let output = toml::to_string_pretty(&self)?;
|
||||
file.write_all(output.as_bytes()).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct JsonCalendarStoreConfig {
|
||||
pub struct TomlCalendarStoreConfig {
|
||||
pub db_path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(tag = "backend", rename_all = "snake_case")]
|
||||
pub enum CalendarStoreConfig {
|
||||
Json(JsonCalendarStoreConfig),
|
||||
Toml(TomlCalendarStoreConfig),
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@@ -6,11 +6,11 @@ use actix_web::middleware::{Logger, NormalizePath};
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use config::{CalendarStoreConfig, JsonCalendarStoreConfig};
|
||||
use config::{CalendarStoreConfig, TomlCalendarStoreConfig};
|
||||
use rustical_api::configure_api;
|
||||
use rustical_dav::{configure_dav, configure_well_known};
|
||||
use rustical_frontend::configure_frontend;
|
||||
use rustical_store::calendar::JsonCalendarStore;
|
||||
use rustical_store::calendar::TomlCalendarStore;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
mod config;
|
||||
@@ -30,10 +30,10 @@ async fn main() -> Result<()> {
|
||||
let config: Config = toml::from_str(&fs::read_to_string(&args.config_file)?)?;
|
||||
|
||||
let cal_store = Arc::new(RwLock::new(match &config.calendar_store {
|
||||
CalendarStoreConfig::Json(JsonCalendarStoreConfig { db_path }) => {
|
||||
CalendarStoreConfig::Toml(TomlCalendarStoreConfig { db_path }) => {
|
||||
match fs::read_to_string(db_path) {
|
||||
Ok(json) => serde_json::from_str::<JsonCalendarStore>(&json).unwrap(),
|
||||
Err(_) => JsonCalendarStore::new(db_path.to_string()),
|
||||
Ok(content) => toml::from_str::<TomlCalendarStore>(&content).unwrap(),
|
||||
Err(_) => TomlCalendarStore::new(db_path.to_string()),
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user