csvpp/target/excel/
compilation_target.rs

1use super::super::{file_backer_upper, CompilationTarget};
2use super::Excel;
3use crate::{Module, Result};
4use umya_spreadsheet as u;
5
6impl CompilationTarget for Excel<'_> {
7    fn write_backup(&self) -> Result<()> {
8        file_backer_upper::backup_file(self.compiler, &self.path)?;
9        Ok(())
10    }
11
12    fn write(&self, module: &Module) -> Result<()> {
13        let mut spreadsheet = self.open_spreadsheet()?;
14
15        self.create_worksheet(&mut spreadsheet)?;
16
17        // TODO: it would be nice to just return the worksheet rather than having a separate method
18        // to get it but I couldn't get the mutable references to work out.
19        let worksheet = self.get_worksheet_mut(&mut spreadsheet)?;
20
21        self.build_worksheet(module, worksheet);
22
23        u::writer::xlsx::write(&spreadsheet, self.path.clone()).map_err(|e| {
24            self.compiler.output_error(format!(
25                "Unable to write target file {}: {e}",
26                self.path.display()
27            ))
28        })?;
29
30        Ok(())
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::test_utils::*;
38
39    #[test]
40    fn write() {
41        let test_file = &TestSourceCode::new("xlsx", "foo,bar,baz");
42        let compiler = test_file.into();
43        let target = Excel::new(&compiler, test_file.output_file.clone());
44        let module = compiler.compile().unwrap();
45
46        assert!(target.write(&module).is_ok());
47    }
48}