mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2025-07-03 01:24:17 +00:00
use the tokio fs in id store
außerdem "readed_string" also bitte
This commit is contained in:
parent
2b469d5b78
commit
c91ec8076e
@ -3,8 +3,8 @@ use std::{
|
|||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
error::Error,
|
error::Error,
|
||||||
fmt::Display,
|
fmt::Display,
|
||||||
fs,
|
|
||||||
};
|
};
|
||||||
|
use tokio::fs;
|
||||||
|
|
||||||
/// Represents the ID that is stored on the Tally
|
/// Represents the ID that is stored on the Tally
|
||||||
#[derive(PartialEq, Eq, Deserialize, Serialize, Hash, Clone, PartialOrd, Ord)]
|
#[derive(PartialEq, Eq, Deserialize, Serialize, Hash, Clone, PartialOrd, Ord)]
|
||||||
@ -37,9 +37,9 @@ impl IDStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creats a new `IDStore` from a json file
|
/// Creats a new `IDStore` from a json file
|
||||||
pub fn new_from_json(filepath: &str) -> Result<Self, Box<dyn Error>> {
|
pub async fn new_from_json(filepath: &str) -> Result<Self, Box<dyn Error>> {
|
||||||
let readed_string = fs::read_to_string(filepath)?;
|
let read_string = fs::read_to_string(filepath).await?;
|
||||||
Ok(serde_json::from_str(&readed_string)?)
|
Ok(serde_json::from_str(&read_string)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a new id for the current day
|
/// Add a new id for the current day
|
||||||
@ -66,9 +66,9 @@ impl IDStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the store to a json file
|
/// Writes the store to a json file
|
||||||
pub fn export_json(&self, filepath: &str) -> Result<(), Box<dyn Error>> {
|
pub async fn export_json(&self, filepath: &str) -> Result<(), Box<dyn Error>> {
|
||||||
// Serialize it to a JSON string and safe it in filepath file
|
fs::write(filepath, serde_json::to_string(&self)?).await?;
|
||||||
Ok(fs::write(filepath, serde_json::to_string(&self)?)?)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Export the store to a csv file.
|
/// Export the store to a csv file.
|
||||||
|
@ -58,7 +58,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
let raw_store = if fs::try_exists(STORE_PATH).await? {
|
let raw_store = if fs::try_exists(STORE_PATH).await? {
|
||||||
info!("Loading data from file");
|
info!("Loading data from file");
|
||||||
IDStore::new_from_json(STORE_PATH)?
|
IDStore::new_from_json(STORE_PATH).await?
|
||||||
} else {
|
} else {
|
||||||
info!("No data file found. Creating empty one.");
|
info!("No data file found. Creating empty one.");
|
||||||
IDStore::new()
|
IDStore::new()
|
||||||
@ -77,7 +77,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
info!("Added new id to current day");
|
info!("Added new id to current day");
|
||||||
// TODO: trigger the buzzer
|
// TODO: trigger the buzzer
|
||||||
|
|
||||||
if let Err(e) = channel_store.lock().await.export_json(STORE_PATH) {
|
if let Err(e) = channel_store.lock().await.export_json(STORE_PATH).await {
|
||||||
error!("Failed to save id store to file: {}", e);
|
error!("Failed to save id store to file: {}", e);
|
||||||
// TODO: How to handle a failure to save ?
|
// TODO: How to handle a failure to save ?
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user