use crate::delta::DeltaReport;
use crate::merge::CrapEntry;
use crate::score::Severity;
use anyhow::{Result, bail};
use std::io::Write;
mod github;
mod human;
mod json;
mod links;
mod markdown;
mod per_crate;
mod pr_comment;
mod sarif;
mod summary;
mod types;
#[cfg(test)]
mod test_support;
pub use json::{DELTA_SCHEMA_URL, Envelope, REPORT_SCHEMA_URL, SCHEMA_VERSION};
pub use links::SourceLinks;
pub use summary::{render_delta_summary, render_summary};
#[derive(Debug, Clone, Copy)]
pub enum Format {
Human,
Json,
GitHub,
Markdown,
PrComment,
Sarif,
}
pub fn render(
entries: &[CrapEntry],
threshold: f64,
format: Format,
links: Option<&SourceLinks>,
out: &mut dyn Write,
) -> Result<()> {
match format {
Format::Json => json::render_json(entries, out),
Format::Human => human::render_human(entries, threshold, out),
Format::GitHub => github::render_github(entries, threshold, out),
Format::Markdown => markdown::render_markdown(entries, threshold, links, out),
Format::PrComment => pr_comment::render_pr_comment(entries, threshold, links, out),
Format::Sarif => sarif::render_sarif(entries, threshold, out),
}
}
pub fn render_delta(
report: &DeltaReport,
threshold: f64,
format: Format,
links: Option<&SourceLinks>,
out: &mut dyn Write,
) -> Result<()> {
match format {
Format::Json => json::render_delta_json(report, out),
Format::Human => human::render_delta_human(report, threshold, out),
Format::GitHub => github::render_delta_github(report, threshold, out),
Format::Markdown => markdown::render_delta_markdown(report, threshold, links, out),
Format::PrComment => pr_comment::render_delta_pr_comment(report, threshold, links, out),
Format::Sarif => bail!(
"--format sarif is incompatible with --baseline; use --format json for delta output"
),
}
}
pub(crate) fn write_pr_comment_marker(out: &mut dyn Write) -> Result<()> {
writeln!(out, "<!-- cargo-crap-report -->")?;
writeln!(out)?;
Ok(())
}
#[must_use]
pub fn crappy_count(
entries: &[CrapEntry],
threshold: f64,
) -> usize {
entries
.iter()
.filter(|e| Severity::classify(e.crap, threshold) == Severity::Crappy)
.count()
}
#[cfg(test)]
mod tests {
use super::*;
use test_support::sample;
#[test]
fn crappy_count_respects_threshold() {
assert_eq!(crappy_count(&sample(), 30.0), 1);
assert_eq!(crappy_count(&sample(), 200.0), 0);
}
}