1#![forbid(unsafe_code)]
4
5pub mod html;
6pub mod json;
7pub mod junit;
8pub mod markdown;
9pub mod ndjson;
10pub mod sarif;
11pub mod terminal;
12
13use rewrit_model::Report;
14use std::path::Path;
15use thiserror::Error;
16
17#[derive(Debug, Error)]
18pub enum ReportError {
19 #[error("unsupported report kind: {0}")]
20 UnsupportedKind(String),
21 #[error("failed to serialize report: {0}")]
22 Serialize(#[from] serde_json::Error),
23 #[error("failed to write report {path}: {source}")]
24 Write {
25 path: String,
26 #[source]
27 source: std::io::Error,
28 },
29}
30
31pub fn render(kind: &str, report: &Report) -> Result<String, ReportError> {
32 match kind {
33 "terminal" => Ok(terminal::render(report)),
34 "json" => json::render(report).map_err(ReportError::from),
35 "ndjson" => ndjson::render(report).map_err(ReportError::from),
36 "junit" => Ok(junit::render(report)),
37 "sarif" => sarif::render(report).map_err(ReportError::from),
38 "html" => Ok(html::render(report)),
39 "markdown" => Ok(markdown::render(report)),
40 other => Err(ReportError::UnsupportedKind(other.to_string())),
41 }
42}
43
44pub fn write(kind: &str, path: impl AsRef<Path>, report: &Report) -> Result<(), ReportError> {
45 let path = path.as_ref();
46 let rendered = render(kind, report)?;
47 if let Some(parent) = path.parent() {
48 std::fs::create_dir_all(parent).map_err(|source| ReportError::Write {
49 path: parent.display().to_string(),
50 source,
51 })?;
52 }
53 std::fs::write(path, rendered).map_err(|source| ReportError::Write {
54 path: path.display().to_string(),
55 source,
56 })
57}
58
59pub(crate) fn escape_xml(input: &str) -> String {
60 input
61 .replace('&', "&")
62 .replace('<', "<")
63 .replace('>', ">")
64 .replace('"', """)
65 .replace('\'', "'")
66}