mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 11:42:25 +00:00
TOML store, optional path for unit testing
This commit is contained in:
@@ -10,7 +10,7 @@ use tokio::{fs::File, io::AsyncWriteExt};
|
|||||||
pub struct TomlCalendarStore {
|
pub struct TomlCalendarStore {
|
||||||
calendars: HashMap<String, Calendar>,
|
calendars: HashMap<String, Calendar>,
|
||||||
events: HashMap<String, HashMap<String, Event>>,
|
events: HashMap<String, HashMap<String, Event>>,
|
||||||
path: String,
|
path: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TomlCalendarStore {
|
impl TomlCalendarStore {
|
||||||
@@ -18,14 +18,24 @@ impl TomlCalendarStore {
|
|||||||
TomlCalendarStore {
|
TomlCalendarStore {
|
||||||
calendars: HashMap::new(),
|
calendars: HashMap::new(),
|
||||||
events: HashMap::new(),
|
events: HashMap::new(),
|
||||||
path,
|
path: Some(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test() -> Self {
|
||||||
|
TomlCalendarStore {
|
||||||
|
calendars: HashMap::new(),
|
||||||
|
events: HashMap::new(),
|
||||||
|
path: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn save(&self) -> Result<()> {
|
pub async fn save(&self) -> Result<()> {
|
||||||
let mut file = File::create(&self.path).await?;
|
|
||||||
let output = toml::to_string_pretty(&self)?;
|
let output = toml::to_string_pretty(&self)?;
|
||||||
file.write_all(output.as_bytes()).await?;
|
if let Some(path) = &self.path {
|
||||||
|
let mut file = File::create(path).await?;
|
||||||
|
file.write_all(output.as_bytes()).await?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user