-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable prefix-based check code selection
- Loading branch information
1 parent
2e63bb6
commit 6ba213c
Showing
9 changed files
with
941 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//! Generate a the CodePrefix enum. | ||
use std::collections::{BTreeMap, BTreeSet}; | ||
|
||
use codegen::{Scope, Type, Variant}; | ||
use itertools::Itertools; | ||
use strum::IntoEnumIterator; | ||
|
||
use ruff::checks::CheckCode; | ||
|
||
fn main() { | ||
// Build up a map from prefix to matching CheckCodes. | ||
let mut prefix_to_codes: BTreeMap<String, BTreeSet<CheckCode>> = Default::default(); | ||
for check_code in CheckCode::iter() { | ||
let as_ref = check_code.as_ref().to_string(); | ||
for i in 1..=as_ref.len() { | ||
let prefix = as_ref[..i].to_string(); | ||
let entry = prefix_to_codes | ||
.entry(prefix) | ||
.or_insert_with(|| Default::default()); | ||
entry.insert(check_code.clone()); | ||
} | ||
} | ||
|
||
let mut scope = Scope::new(); | ||
|
||
// Create the `CodePrefix` definition. | ||
let mut gen = scope | ||
.new_enum("CodePrefix") | ||
.vis("pub") | ||
.derive("EnumString") | ||
.derive("Debug") | ||
.derive("PartialEq") | ||
.derive("Eq") | ||
.derive("Clone") | ||
.derive("Serialize") | ||
.derive("Deserialize"); | ||
for (prefix, _) in &prefix_to_codes { | ||
gen = gen.push_variant(Variant::new(prefix.to_string())); | ||
} | ||
|
||
// Create the `match` statement, to map from definition to relevant codes. | ||
let mut gen = scope | ||
.new_impl("CodePrefix") | ||
.new_fn("codes") | ||
.arg_ref_self() | ||
.ret(Type::new("Vec<CheckCode>")) | ||
.vis("pub") | ||
.line("match self {"); | ||
for (prefix, codes) in prefix_to_codes { | ||
gen = gen.line(format!( | ||
"CodePrefix::{prefix} => vec![{}],", | ||
codes | ||
.iter() | ||
.map(|code| format!("CheckCode::{}", code.as_ref())) | ||
.join(", ") | ||
)); | ||
} | ||
gen.line("}"); | ||
|
||
println!("//! File automatically generated by examples/generate_prefix_map.rs."); | ||
println!(); | ||
println!("use serde::{{Deserialize, Serialize}};"); | ||
println!("use strum_macros::EnumString;"); | ||
println!(); | ||
println!("use crate::checks::CheckCode;"); | ||
println!(); | ||
println!("{}", scope.to_string()); | ||
} |
Oops, something went wrong.