pub mod schema;
#[cfg(feature = "export-html")]
pub mod html;
#[cfg(feature = "export-json")]
pub mod json;
use std::fmt;
use std::io::Write;
use std::path::Path;
use crate::diff::ScanDiff;
use crate::export::{ExportError, ExportOptions};
#[cfg(feature = "export-html")]
pub use html::HtmlDiffExporter;
#[cfg(feature = "export-json")]
pub use json::JsonDiffExporter;
pub trait DiffExporter {
fn export(&self, diff: &ScanDiff, out: &mut dyn Write) -> Result<(), ExportError>;
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DiffFormat {
#[cfg(feature = "export-json")]
Json,
#[cfg(feature = "export-html")]
Html,
}
impl DiffFormat {
pub fn from_extension(extension: &str) -> Option<Self> {
match extension.to_ascii_lowercase().as_str() {
#[cfg(feature = "export-json")]
"json" => Some(DiffFormat::Json),
#[cfg(feature = "export-html")]
"html" | "htm" => Some(DiffFormat::Html),
_ => None,
}
}
pub fn from_path(path: &Path) -> Option<Self> {
path.extension()
.and_then(|extension| extension.to_str())
.and_then(Self::from_extension)
}
pub fn extension(self) -> &'static str {
match self {
#[cfg(feature = "export-json")]
DiffFormat::Json => "json",
#[cfg(feature = "export-html")]
DiffFormat::Html => "html",
}
}
pub fn all() -> &'static [DiffFormat] {
&[
#[cfg(feature = "export-json")]
DiffFormat::Json,
#[cfg(feature = "export-html")]
DiffFormat::Html,
]
}
pub fn exporter(self, options: ExportOptions) -> Box<dyn DiffExporter> {
let _ = &options;
match self {
#[cfg(feature = "export-json")]
DiffFormat::Json => Box::new(JsonDiffExporter::new(options)),
#[cfg(feature = "export-html")]
DiffFormat::Html => Box::new(HtmlDiffExporter::new(options)),
}
}
}
impl fmt::Display for DiffFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.extension())
}
}
pub fn export_to(
path: &Path,
diff: &ScanDiff,
out: &mut dyn Write,
options: ExportOptions,
) -> Option<Result<(), ExportError>> {
let format = DiffFormat::from_path(path)?;
Some(format.exporter(options).export(diff, out))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_advertised_format_writes_and_resolves_from_its_own_extension() {
let diff = ScanDiff::between(
&crate::export::fixture::report(),
&crate::export::fixture::report(),
);
for format in DiffFormat::all() {
assert_eq!(
DiffFormat::from_extension(format.extension()),
Some(*format),
"{format} does not resolve from its own extension"
);
let mut sink = Vec::new();
format
.exporter(ExportOptions::new())
.export(&diff, &mut sink)
.expect("an advertised format exports");
assert!(!sink.is_empty(), "{format} produced nothing at all");
}
}
#[test]
fn exporting_by_path_matches_exporting_by_format() {
let diff = ScanDiff::between(
&crate::export::fixture::report(),
&crate::export::fixture::report(),
);
for format in DiffFormat::all() {
let name = format!("changes.{}", format.extension());
let mut by_path = Vec::new();
export_to(Path::new(&name), &diff, &mut by_path, ExportOptions::new())
.expect("the extension names a format")
.expect("the export succeeds");
assert!(!by_path.is_empty());
}
let mut sink = Vec::new();
assert!(
export_to(
Path::new("changes.pdf"),
&diff,
&mut sink,
ExportOptions::new()
)
.is_none(),
"an unsupported extension must not quietly produce a file"
);
assert!(sink.is_empty());
}
}