Skip to content

Commit

Permalink
Automatically write to src/checks_gen.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Nov 5, 2022
1 parent b335a6a commit 6bd5f48
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions examples/generate_check_code_prefix.rs
@@ -1,13 +1,16 @@
//! Generate the CheckCodePrefix enum.
//! Regenerate `src/checks_gen.rs` (namely, the `CheckCodePrefix` enum).

use std::collections::{BTreeMap, BTreeSet};
use std::fs::OpenOptions;
use std::io::{BufWriter, Write};

use anyhow::Result;
use codegen::{Scope, Type, Variant};
use itertools::Itertools;
use ruff::checks::CheckCode;
use strum::IntoEnumIterator;

fn main() {
fn main() -> Result<()> {
// 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() {
Expand Down Expand Up @@ -100,12 +103,24 @@ fn main() {
}
gen.line("}");

println!("//! File automatically generated by examples/generate_check_code_prefix.rs.");
println!();
println!("use serde::{{Serialize, Deserialize}};");
println!("use strum_macros::EnumString;");
println!();
println!("use crate::checks::CheckCode;");
println!();
println!("{}", scope.to_string());
// Write the output to `src/checks_gen.rs`.
let f = OpenOptions::new()
.write(true)
.truncate(true)
.open("src/checks_gen.rs")
.expect("unable to open file");
let mut f = BufWriter::new(f);
writeln!(
f,
"//! File automatically generated by examples/generate_check_code_prefix.rs."
)?;
writeln!(f)?;
writeln!(f, "use serde::{{Serialize, Deserialize}};")?;
writeln!(f, "use strum_macros::EnumString;")?;
writeln!(f)?;
writeln!(f, "use crate::checks::CheckCode;")?;
writeln!(f)?;
writeln!(f, "{}", scope.to_string())?;

Ok(())
}

0 comments on commit 6bd5f48

Please sign in to comment.