create missing files

This commit is contained in:
LastExceed 2025-03-20 06:39:14 +01:00
parent e028ce62b0
commit a97b6edaa4
2 changed files with 15 additions and 2 deletions

View File

@ -1,15 +1,26 @@
use std::fs;
use std::fs::File;
use std::io::ErrorKind;
use std::time::Duration;
use serde::de::DeserializeOwned;
use tap::Pipe;
pub mod logs;
pub mod booking;
pub mod index;
pub fn parse_toml_file<T: DeserializeOwned>(path: &str) -> T {
let file_content = fs::read_to_string(path).expect("failed to read .toml file");
toml::from_str(&file_content).expect("failed to parse toml")
match fs::read_to_string(path) {
Ok(s) => s,
Err(e) if e.kind() == ErrorKind::NotFound => {
File::create(path).expect("failed to create file");
String::new()
},
_ => panic!("error reading {path}")
}
.pipe_as_ref(toml::from_str)
.expect("failed to parse toml")
}
pub fn get_power_state(payload: &str) -> Result<String, &'static str> {

View File

@ -32,6 +32,7 @@ pub fn machinelog(machine: &str, booking: &Booking) -> io::Result<()> {
};
let file_writer = File::options()
.create(true)
.append(true)
.open("/root/machinelog.csv")?;
@ -47,6 +48,7 @@ pub fn machinelog(machine: &str, booking: &Booking) -> io::Result<()> {
pub fn log_start() -> io::Result<()> {
File::options()
.create(true)
.append(true)
.open("/root/machinelog_debug.csv")?
.write_all(format!("\n\n===== startup {} =====\n\n", Local::now()).as_bytes())