refactor: Simplify UpstreamCategory

There's no need to hardcode the prefix string since
it can be derived from the RuleCodePrefix.
This commit is contained in:
Martin Fischer 2023-02-03 05:06:02 +01:00 committed by Charlie Marsh
parent a3ffaa5d9b
commit 0f8f250bea
3 changed files with 14 additions and 14 deletions

View File

@ -15,7 +15,7 @@ pub fn linter(format: HelpFormat) {
.upstream_categories()
.unwrap()
.iter()
.map(|UpstreamCategory(prefix, ..)| prefix)
.map(|UpstreamCategory(prefix, ..)| prefix.as_ref())
.join("/"),
prefix => prefix.to_string(),
};
@ -31,7 +31,7 @@ pub fn linter(format: HelpFormat) {
categories: linter_info.upstream_categories().map(|cats| {
cats.iter()
.map(|UpstreamCategory(prefix, name, ..)| LinterCategoryInfo {
prefix,
prefix: prefix.as_ref(),
name,
})
.collect()

View File

@ -53,7 +53,7 @@ pub fn main(args: &Args) -> Result<()> {
.upstream_categories()
.unwrap()
.iter()
.map(|UpstreamCategory(prefix, ..)| prefix)
.map(|UpstreamCategory(prefix, ..)| prefix.as_ref())
.join(", "),
prefix => prefix.to_string(),
};
@ -94,11 +94,11 @@ pub fn main(args: &Args) -> Result<()> {
}
if let Some(categories) = linter.upstream_categories() {
for UpstreamCategory(prefix, name, selector) in categories {
table_out.push_str(&format!("#### {name} ({prefix})"));
for UpstreamCategory(prefix, name) in categories {
table_out.push_str(&format!("#### {name} ({})", prefix.as_ref()));
table_out.push('\n');
table_out.push('\n');
generate_table(&mut table_out, selector);
generate_table(&mut table_out, prefix);
}
} else {
generate_table(&mut table_out, &linter);

View File

@ -644,21 +644,21 @@ pub trait RuleNamespace: Sized {
fn url(&self) -> Option<&'static str>;
}
/// The prefix, name and selector for an upstream linter category.
pub struct UpstreamCategory(pub &'static str, pub &'static str, pub RuleCodePrefix);
/// The prefix and name for an upstream linter category.
pub struct UpstreamCategory(pub RuleCodePrefix, pub &'static str);
impl Linter {
pub fn upstream_categories(&self) -> Option<&'static [UpstreamCategory]> {
match self {
Linter::Pycodestyle => Some(&[
UpstreamCategory("E", "Error", RuleCodePrefix::E),
UpstreamCategory("W", "Warning", RuleCodePrefix::W),
UpstreamCategory(RuleCodePrefix::E, "Error"),
UpstreamCategory(RuleCodePrefix::W, "Warning"),
]),
Linter::Pylint => Some(&[
UpstreamCategory("PLC", "Convention", RuleCodePrefix::PLC),
UpstreamCategory("PLE", "Error", RuleCodePrefix::PLE),
UpstreamCategory("PLR", "Refactor", RuleCodePrefix::PLR),
UpstreamCategory("PLW", "Warning", RuleCodePrefix::PLW),
UpstreamCategory(RuleCodePrefix::PLC, "Convention"),
UpstreamCategory(RuleCodePrefix::PLE, "Error"),
UpstreamCategory(RuleCodePrefix::PLR, "Refactor"),
UpstreamCategory(RuleCodePrefix::PLW, "Warning"),
]),
_ => None,
}