pub mod html;
pub mod pdf;
pub mod typst;
use std::{
fs::File,
io::{BufWriter, Result, Write},
path::Path,
};
use crate::Screenplay;
pub trait Exporter {
fn file_extension(&self) -> &'static str;
fn export(&self, screenplay: &Screenplay, writer: &mut dyn Write) -> Result<()>;
}
pub trait ExporterExt: Exporter {
fn export_to_stdout(&self, screenplay: &Screenplay) -> Result<()> {
let stdout = std::io::stdout();
let mut lock = stdout.lock();
self.export(screenplay, &mut lock)
}
fn export_to_file(&self, screenplay: &Screenplay, path: impl AsRef<Path>) -> Result<()> {
let file = File::create(path)?;
let mut w = BufWriter::new(file);
self.export(screenplay, &mut w)
}
fn export_with_extension(
&self,
screenplay: &Screenplay,
dir: impl AsRef<Path>,
base: &str,
) -> Result<()> {
let path = dir
.as_ref()
.join(base)
.with_extension(self.file_extension());
self.export_to_file(screenplay, &path)
}
fn export_to_string(&self, screenplay: &Screenplay) -> Result<String> {
let mut buf = Vec::new();
self.export(screenplay, &mut buf)?;
Ok(String::from_utf8_lossy(&buf).into_owned())
}
}
impl<T: Exporter + ?Sized> ExporterExt for T {}