paiagram/
export.rs

1use bevy::prelude::*;
2use std::error::Error;
3
4use crate::rw_data::write::write_file;
5
6pub mod graphviz;
7pub mod typst_diagram;
8pub mod typst_timetable;
9
10pub trait ExportObject<S = ()> {
11    /// Export contents to a Vec<u8> buffer, with optional parameters
12    fn export_to_buffer(
13        &mut self,
14        world: &mut World,
15        buffer: &mut Vec<u8>,
16        input: S,
17    ) -> Result<(), Box<dyn Error>>;
18    /// Export contents and save them on disk, with optional parameters
19    fn export_to_file(&mut self, world: &mut World, input: S) -> Result<(), Box<dyn Error>> {
20        let mut buffer = Vec::new();
21        self.export_to_buffer(world, &mut buffer, input)?;
22        let mut filename = String::new();
23        filename.push_str(self.filename().as_ref());
24        filename.push_str(self.extension().as_ref());
25        bevy::tasks::IoTaskPool::get()
26            .spawn(async move {
27                if let Err(e) = write_file(buffer, filename).await {
28                    error!("Error while writing file: {:?}", e)
29                }
30            })
31            .detach();
32        Ok(())
33    }
34    fn filename(&self) -> impl AsRef<str> {
35        "exported_file"
36    }
37    fn extension(&self) -> impl AsRef<str> {
38        ".paiagram"
39    }
40}