Skip to content

Instantly share code, notes, and snippets.

@sxlijin
Created May 16, 2024 21:46
Show Gist options
  • Save sxlijin/b9309dcc46ccd3d7dcd64fc0be511cd0 to your computer and use it in GitHub Desktop.
Save sxlijin/b9309dcc46ccd3d7dcd64fc0be511cd0 to your computer and use it in GitHub Desktop.
rustfmt does not handle macros nicely example
fn remove_dir_safe(&self, output_path: &Path) -> Result<()> {
if !output_path.exists() {
return Ok(());
}
const MAX_UNKNOWN_FILES: usize = 5;
let mut unknown_files = vec![];
for entry in walkdir::WalkDir::new(output_path) {
if unknown_files.len() > MAX_UNKNOWN_FILES {
break;
}
let path = entry?.into_path();
if self.files.contains_key(&path) {
continue;
}
unknown_files.push(path);
}
match unknown_files.len() {
0 => (),
1 => anyhow::bail!(
"directory contains unmanaged file\n\n\
The output directory contains a file that BAML did not generate. Please remove it and re-run codegen.\n\n\
Output directory: {:?}\n\
File: {:?}",
output_path,
unknown_files[0]
),
n => if n < MAX_UNKNOWN_FILES {
anyhow::bail!(
"directory contains unmanaged files\n\n\
The output directory contains {n} files that BAML did not generate. Please remove them and re-run codegen.\n\n\
Output directory: {:?}\n\
Files:\n{:?}",
output_path,
unknown_files.iter().map(|p| format!(" - {}", p.display())).collect::<Vec<_>>().join("\n")
)
} else {
anyhow::bail!(
"directory contains unmanaged files\n\n\
The output directory contains at least {n} files that BAML did not generate. Please remove all files not generated by BAML and re-run codegen.\n\n\
Output directory: {:?}\n\
Files:\n{:?}",
output_path,
unknown_files.iter().map(|p| format!(" - {}", p.display())).collect::<Vec<_>>().join("\n")
)
},
}
std::fs::remove_dir_all(output_path)?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment