Improve error handling, logging
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::{fs::File, io::Write};
|
||||
|
||||
use anyhow::Context;
|
||||
use docx_rs::{Docx, Paragraph, Run, RunFonts, Table, TableCell, TableRow};
|
||||
|
||||
use crate::{
|
||||
@@ -16,45 +17,51 @@ pub enum FileType {
|
||||
}
|
||||
|
||||
pub trait Export {
|
||||
fn export(&self, file_type: FileType, config: &UserConfig, tracker: &WorkloadTracker);
|
||||
fn export(
|
||||
&self,
|
||||
file_type: FileType,
|
||||
config: &UserConfig,
|
||||
tracker: &WorkloadTracker,
|
||||
) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
impl Export for MonthlySchedule {
|
||||
fn export(&self, file_type: FileType, config: &UserConfig, tracker: &WorkloadTracker) {
|
||||
fn export(
|
||||
&self,
|
||||
file_type: FileType,
|
||||
config: &UserConfig,
|
||||
tracker: &WorkloadTracker,
|
||||
) -> anyhow::Result<()> {
|
||||
match file_type {
|
||||
FileType::Txt => self.export_as_txt(config, tracker),
|
||||
FileType::Docx => self.export_as_doc(config),
|
||||
FileType::Txt => self.export_as_txt(config, tracker)?,
|
||||
FileType::Docx => self.export_as_doc(config)?,
|
||||
};
|
||||
|
||||
// TODO: make this env var from a config file? Option to change this in-app
|
||||
let env_path = "rota/me/";
|
||||
println!(
|
||||
"exported type {:?}. Saved at folder path {}",
|
||||
file_type, env_path
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl MonthlySchedule {
|
||||
pub fn export_as_txt(&self, config: &UserConfig, tracker: &WorkloadTracker) -> String {
|
||||
let file = File::create("schedule.txt").unwrap();
|
||||
pub fn export_as_txt(
|
||||
&self,
|
||||
config: &UserConfig,
|
||||
tracker: &WorkloadTracker,
|
||||
) -> anyhow::Result<()> {
|
||||
let file = File::create("rota.txt")?;
|
||||
let mut writer = std::io::BufWriter::new(file);
|
||||
|
||||
writer
|
||||
.write_all(self.pretty_print(config).as_bytes())
|
||||
.expect("Failed to write schedule");
|
||||
writer.write_all(self.pretty_print(config).as_bytes())?;
|
||||
|
||||
writer
|
||||
.write_all(self.report(config, tracker).as_bytes())
|
||||
.expect("Failed to write report");
|
||||
writer.write_all(self.report(config, tracker).as_bytes())?;
|
||||
|
||||
writer.flush().expect("Failed to flush buffer");
|
||||
"ok".to_string()
|
||||
writer.flush()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn export_as_doc(&self, config: &UserConfig) -> String {
|
||||
let path = std::path::Path::new("./schedule.docx");
|
||||
let file = std::fs::File::create(path).unwrap();
|
||||
pub fn export_as_doc(&self, config: &UserConfig) -> anyhow::Result<()> {
|
||||
let path = std::path::Path::new("rota.docx");
|
||||
let file = std::fs::File::create(path)?;
|
||||
|
||||
let header = Table::new(vec![
|
||||
TableRow::new(vec![TableCell::new().add_paragraph(
|
||||
@@ -101,7 +108,7 @@ impl MonthlySchedule {
|
||||
.iter()
|
||||
.find(|r| Some(&r.id) == slot_first_res_id)
|
||||
.map(|r| r.name.as_str())
|
||||
.unwrap();
|
||||
.unwrap_or("-");
|
||||
|
||||
let res_name_2 = if day.is_open_shift() {
|
||||
let slot_second = Slot::new(Day(d), ShiftPosition::Second);
|
||||
@@ -153,18 +160,23 @@ impl MonthlySchedule {
|
||||
|
||||
doc = doc.add_table(residents_table);
|
||||
|
||||
doc.build().pack(file).unwrap();
|
||||
doc.build().pack(file)?;
|
||||
|
||||
"just a string".to_string()
|
||||
tauri_plugin_opener::open_path(path, None::<&str>)
|
||||
.context("Created file but failed to open it")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use anyhow::Ok;
|
||||
use rstest::{fixture, rstest};
|
||||
|
||||
use crate::{
|
||||
config::UserConfig,
|
||||
export::{Export, FileType},
|
||||
resident::Resident,
|
||||
schedule::MonthlySchedule,
|
||||
scheduler::Scheduler,
|
||||
@@ -203,13 +215,29 @@ mod tests {
|
||||
WorkloadTracker::default()
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
pub fn test_export_as_txt(
|
||||
mut schedule: MonthlySchedule,
|
||||
mut tracker: WorkloadTracker,
|
||||
scheduler: Scheduler,
|
||||
) -> anyhow::Result<()> {
|
||||
assert!(scheduler.run(&mut schedule, &mut tracker)?);
|
||||
|
||||
schedule.export(FileType::Txt, &scheduler.config, &tracker)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
pub fn test_export_as_doc(
|
||||
mut schedule: MonthlySchedule,
|
||||
mut tracker: WorkloadTracker,
|
||||
scheduler: Scheduler,
|
||||
) {
|
||||
scheduler.run(&mut schedule, &mut tracker);
|
||||
schedule.export_as_doc(&scheduler.config);
|
||||
) -> anyhow::Result<()> {
|
||||
assert!(scheduler.run(&mut schedule, &mut tracker)?);
|
||||
|
||||
schedule.export(FileType::Docx, &scheduler.config, &tracker)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user