use std::fmt::Display;
use comfy_table::{
Attribute, Cell, ContentArrangement, Table, modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL,
};
use miette::{Diagnostic, GraphicalReportHandler, Severity};
use mit_lint::{Lint, Lints};
use thiserror::Error;
use crate::mit::Authors;
pub fn success(success: &str, tip: &str) {
let mut out = String::new();
GraphicalReportHandler::default()
.render_report(
&mut out,
&Success {
success: success.to_string(),
help: Some(tip),
},
)
.unwrap();
println!("{out}");
}
#[derive(Error, Debug)]
#[error("{success}")]
struct Success<'a> {
success: String,
help: Option<&'a str>,
}
impl Diagnostic for Success<'_> {
fn severity(&self) -> Option<Severity> {
Some(Severity::Advice)
}
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.help
.map(|x| Box::new(x.to_string()) as Box<dyn Display>)
}
}
#[derive(Error, Debug)]
#[error("{warning}")]
struct Warning<'a> {
warning: String,
help: Option<&'a str>,
}
impl Diagnostic for Warning<'_> {
fn severity(&self) -> Option<Severity> {
Some(Severity::Warning)
}
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.help
.map(|x| Box::new(x.to_string()) as Box<dyn Display>)
}
}
pub fn warning(warning: &str, tip: Option<&str>) {
let mut out = String::new();
GraphicalReportHandler::default()
.render_report(
&mut out,
&Warning {
warning: warning.to_string(),
help: tip,
},
)
.unwrap();
eprintln!("{out}");
}
pub fn to_be_piped(output: &str) {
println!("{output}");
}
pub fn lint_table(list: &Lints, enabled: &Lints) {
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec!["Lint", "Status"]);
let enabled_set: std::collections::HashSet<Lint> = enabled.clone().into_iter().collect();
let rows: Table = list.clone().into_iter().fold(table, |mut table, lint| {
table.add_row(vec![
lint.name(),
if enabled_set.contains(&lint) {
"enabled"
} else {
"disabled"
},
]);
table
});
println!("{rows}");
}
#[must_use]
pub fn author_table(authors: &Authors<'_>) -> String {
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec!["Initial", "Name", "Email", "Signing Key"]);
let rows: Table = authors
.authors
.iter()
.fold(table, |mut table, (initial, author)| {
table.add_row(vec![
Cell::new(initial),
Cell::new(author.name()),
Cell::new(author.email()),
author.signingkey().map_or_else(
|| Cell::new("None".to_string()).add_attributes(vec![Attribute::Italic]),
Cell::new,
),
]);
table
});
format!("{rows}")
}
#[cfg(test)]
mod tests {
use miette::{Diagnostic, Severity};
use super::{Success, Warning};
#[test]
fn success_has_advice_severity() {
let success = Success {
success: "done".into(),
help: Some("tip"),
};
assert_eq!(
success.severity(),
Some(Severity::Advice),
"Expected success to have Advice severity"
);
}
#[test]
fn success_returns_help_when_present() {
let success = Success {
success: "done".into(),
help: Some("use --force"),
};
assert!(
success.help().is_some(),
"Expected help to be present when one is provided"
);
}
#[test]
fn success_returns_no_help_when_absent() {
let success = Success {
success: "done".into(),
help: None,
};
assert!(
success.help().is_none(),
"Expected help to be absent when none is provided"
);
}
#[test]
fn warning_has_warning_severity() {
let warning = Warning {
warning: "careful".into(),
help: Some("tip"),
};
assert_eq!(
warning.severity(),
Some(Severity::Warning),
"Expected warning to have Warning severity"
);
}
#[test]
fn warning_returns_help_when_present() {
let warning = Warning {
warning: "careful".into(),
help: Some("check config"),
};
assert!(
warning.help().is_some(),
"Expected help to be present when one is provided"
);
}
#[test]
fn warning_returns_no_help_when_absent() {
let warning = Warning {
warning: "careful".into(),
help: None,
};
assert!(
warning.help().is_none(),
"Expected help to be absent when none is provided"
);
}
}