1use crate::target::{Csv, Excel, GoogleSheets, OpenDocument};
4use crate::{CompilationTarget, Compiler, Error, Result};
5use std::path;
6
7mod display;
8mod try_from;
9
10type GoogleSheetID = String;
11
12#[derive(Clone, Debug, PartialEq)]
13pub enum Output {
14 Csv(path::PathBuf),
15 Excel(path::PathBuf),
16 OpenDocument(path::PathBuf),
17 GoogleSheets(GoogleSheetID),
18}
19
20impl Output {
21 pub(crate) fn compilation_target<'a>(
22 &'a self,
23 compiler: &'a Compiler,
24 ) -> Result<Box<dyn CompilationTarget + 'a>> {
25 Ok(match self {
26 Self::Csv(path) => Box::new(Csv::new(compiler, path)),
27 Self::Excel(path) => Box::new(Excel::new(compiler, path)),
28 Self::GoogleSheets(sheet_id) => Box::new(GoogleSheets::new(compiler, sheet_id)?),
29 Self::OpenDocument(path) => Box::new(OpenDocument::new(compiler, path)),
30 })
31 }
32
33 pub(crate) fn into_error<M: Into<String>>(self, message: M) -> Error {
34 Error::TargetWriteError {
35 message: message.into(),
36 output: self,
37 }
38 }
39
40 fn from_filename<P>(path: &P) -> Result<Self>
41 where
42 P: AsRef<path::Path> + Into<path::PathBuf>,
43 {
44 let path = path.as_ref();
45 let ext = path.extension().ok_or(Error::InitError(
46 "Output filename must end with .csv, .xlsx or .ods".to_string(),
47 ))?;
48
49 let path = path.into();
50 if Csv::supports_extension(ext) {
51 Ok(Self::Csv(path))
52 } else if Excel::supports_extension(ext) {
53 Ok(Self::Excel(path))
54 } else if OpenDocument::supports_extension(ext) {
55 Ok(Self::OpenDocument(path))
56 } else {
57 Err(Error::InitError(format!(
58 "{} is an unsupported extension: only .csv, .xlsx or .ods are supported.",
59 ext.to_str().unwrap()
60 )))
61 }
62 }
63
64 fn from_google_sheet_id(sheet_id: String) -> Result<Self> {
65 if sheet_id.chars().all(char::is_alphanumeric) {
66 Ok(Self::GoogleSheets(sheet_id))
67 } else {
68 Err(Error::InitError(
69 "The GOOGLE_SHEET_ID must be all letters and digits.".to_string(),
70 ))
71 }
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 }