Skip to content

Commit

Permalink
Add inline colorization to some diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Shadle committed Oct 15, 2020
1 parent 2aab6f8 commit 9136a1b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 8 deletions.
130 changes: 122 additions & 8 deletions src/bans/diags.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
diag::{CfgCoord, Check, Diag, Diagnostic, KrateCoord, Label, Pack, Severity},
diag::{CfgCoord, Check, Diag, Diagnostic, FileId, KrateCoord, Label, Pack, Severity},
Krate,
};

Expand Down Expand Up @@ -260,7 +260,7 @@ impl<'a> Into<Diag> for ExactFeaturesMismatch<'a> {
.with_notes(
self.not_allowed
.iter()
.map(|na| format!("'{}' feature was enabled bot not explicitly allowed", na))
.map(|na| format!("'{}' feature was enabled but not explicitly allowed", na))
.collect(),
)
.into()
Expand All @@ -269,34 +269,109 @@ impl<'a> Into<Diag> for ExactFeaturesMismatch<'a> {

pub(crate) struct FeaturesNotExplicitlyAllowed<'a> {
pub(crate) not_allowed: &'a [&'a str],
pub(crate) allowed: Vec<CfgCoord>,
pub(crate) enabled_features: &'a [&'a str],
pub(crate) parent: &'a Krate,
pub(crate) dep_name: &'a str,
pub(crate) colorize: bool,
}

impl<'a> Into<Diag> for FeaturesNotExplicitlyAllowed<'a> {
fn into(self) -> Diag {
use std::fmt::Write;

let mut note = String::with_capacity(100);
write!(&mut note, "Enabled features: ").unwrap();

if self.colorize {
note.push('[');

for enabled in self.enabled_features {
if self
.not_allowed
.iter()
.find(|na| **na == *enabled)
.is_some()
{
write!(&mut note, "{}, ", ansi_term::Color::Red.paint(*enabled)).unwrap();
} else {
write!(&mut note, "{}, ", ansi_term::Color::Green.paint(*enabled)).unwrap();
}
}

note.truncate(note.len() - 2);
note.push(']');
} else {
write!(
&mut note,
"[{}]",
crate::diag::to_string(self.enabled_features)
)
.unwrap();
}

Diagnostic::new(Severity::Error)
.with_message(format!(
"features declared by '{}' for '{}' were not explicitly allowed",
self.parent, self.dep_name
))
.with_code("B013")
.with_notes(vec![format!(
"Features: {}",
crate::diag::to_string(self.not_allowed)
)])
.with_labels(
self.allowed
.into_iter()
.map(|cc| cc.into_label().with_message("feature allowed here"))
.collect(),
)
.with_notes(vec![note])
.into()
}
}

pub(crate) struct FeaturesExplicitlyDenied<'a> {
pub(crate) found_denied: Vec<CfgCoord>,
pub(crate) cfg_file_id: FileId,
pub(crate) found_denied: Vec<&'a crate::cfg::Spanned<String>>,
pub(crate) enabled_features: &'a [&'a str],
pub(crate) parent: &'a Krate,
pub(crate) dep_name: &'a str,
pub(crate) colorize: bool,
}

impl<'a> Into<Diag> for FeaturesExplicitlyDenied<'a> {
fn into(self) -> Diag {
use std::fmt::Write;

let mut note = String::with_capacity(100);
write!(&mut note, "Enabled features: ").unwrap();

if self.colorize {
note.push('[');

for enabled in self.enabled_features {
if self
.found_denied
.iter()
.find(|fd| fd.value == *enabled)
.is_some()
{
write!(&mut note, "{}, ", ansi_term::Color::Red.paint(*enabled)).unwrap();
} else {
write!(&mut note, "{}, ", enabled).unwrap();
}
}

note.truncate(note.len() - 2);
note.push(']');
} else {
write!(
&mut note,
"[{}]",
crate::diag::to_string(self.enabled_features)
)
.unwrap();
}

let cfg_file_id = self.cfg_file_id;

Diagnostic::new(Severity::Error)
.with_message(format!(
"features declared by '{}' for '{}' were explicitly denied",
Expand All @@ -306,9 +381,48 @@ impl<'a> Into<Diag> for FeaturesExplicitlyDenied<'a> {
.with_labels(
self.found_denied
.into_iter()
.map(|fd| fd.into_label())
.map(|fd| {
Label::primary(cfg_file_id, fd.span.clone())
.with_message("feature denied here")
})
.collect(),
)
.with_notes(vec![note])
.into()
}
}

pub(crate) struct UnableToGetDefaultFeatures<'a> {
pub(crate) parent_krate: &'a Krate,
pub(crate) dep: &'a krates::cm::Dependency,
}

impl<'a> Into<Diag> for UnableToGetDefaultFeatures<'a> {
fn into(self) -> Diag {
Diagnostic::new(Severity::Warning)
.with_message(format!(
"unable to get default features for '{}' used by '{}'",
self.dep.name, self.parent_krate,
))
.with_code("B015")
.into()
}
}
// pub(crate) struct EnabledFeatures<'a> {
// pub(crate) enabled_features: &'a [&'a str],
// pub(crate) parent: &'a Krate,
// pub(crate) dep_name: &'a str,
// }

// impl<'a> Into<Diag> for EnabledFeatures<'a> {
// fn into(self) -> Diag {
// Diagnostic::new(Severity::Note)
// .with_message(format!(
// "enabled features for '{}' from '{}'",
// self.dep_name, self.parent
// ))
// .with_code("B015")
// .with_notes(vec![crate::diag::to_string(self.enabled_features)])
// .into()
// }
// }
11 changes: 11 additions & 0 deletions src/cargo-deny/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ pub(crate) fn cmd(
let audit_compatible_output =
args.audit_compatible_output && log_ctx.format == crate::Format::Json;

let colorize = log_ctx.format == crate::Format::Human
&& match log_ctx.color {
crate::Color::Auto => atty::is(atty::Stream::Stderr),
crate::Color::Always => true,
crate::Color::Never => false,
};

rayon::scope(|s| {
// Asynchronously displays messages sent from the checks
s.spawn(|_| {
Expand All @@ -354,6 +361,7 @@ pub(crate) fn cmd(
krates: &krates,
krate_spans: &krate_spans,
serialize_extra,
colorize,
};

s.spawn(move |_| {
Expand Down Expand Up @@ -405,6 +413,7 @@ pub(crate) fn cmd(
krates: &krates,
krate_spans: &krate_spans,
serialize_extra,
colorize,
};

s.spawn(|_| {
Expand All @@ -426,6 +435,7 @@ pub(crate) fn cmd(
krates: &krates,
krate_spans: &krate_spans,
serialize_extra,
colorize,
};

s.spawn(|_| {
Expand All @@ -446,6 +456,7 @@ pub(crate) fn cmd(
krates: &krates,
krate_spans: &krate_spans,
serialize_extra,
colorize,
};

s.spawn(move |_| {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,6 @@ pub struct CheckCtx<'ctx, T> {
/// Requests for additional information the check can provide to be
/// serialized to the diagnostic
pub serialize_extra: bool,
/// Allows for ANSI colorization of diagnostic content
pub colorize: bool,
}

0 comments on commit 9136a1b

Please sign in to comment.