xtask/tasks/fmt/house_rules/
package_info.rsuse anyhow::Context;
use std::ffi::OsStr;
use std::path::Path;
use toml_edit::Item;
use toml_edit::Table;
pub fn check_package_info(f: &Path, fix: bool) -> anyhow::Result<()> {
if f.file_name() != Some(OsStr::new("Cargo.toml")) {
return Ok(());
}
let contents = fs_err::read_to_string(f)?;
let mut parsed = contents.parse::<toml_edit::DocumentMut>()?;
let mut excluded_from_workspace = false;
if let Some(metadata) = parsed
.get("package")
.and_then(|x| x.get("metadata"))
.and_then(|x| x.get("xtask"))
.and_then(|x| x.get("house-rules"))
{
let props = metadata.as_table().context("invalid metadata format")?;
for (k, v) in props.iter() {
if k == "excluded-from-workspace" {
excluded_from_workspace = v
.as_bool()
.context("invalid type for excluded-from-workspace (must be bool)")?;
}
}
}
if !parsed.contains_key("package") {
return Ok(());
}
let mut lints_table = Table::new();
lints_table.insert("workspace", Item::Value(true.into()));
let old_lints_table = parsed.insert("lints", Item::Table(lints_table.clone()));
let package = parsed
.get_mut("package")
.unwrap()
.as_table_mut()
.with_context(|| format!("invalid package section in {}", f.display()))?;
let mut rust_version_field = Table::new();
rust_version_field.set_dotted(true);
rust_version_field.insert("workspace", Item::Value(true.into()));
let old_rust_version = package.insert("rust-version", Item::Table(rust_version_field.clone()));
let mut edition_field = Table::new();
edition_field.set_dotted(true);
edition_field.insert("workspace", Item::Value(true.into()));
let old_edition_field = package.insert("edition", Item::Table(edition_field.clone()));
let invalid = package.remove("authors").is_some()
| package.remove("version").is_some()
| (!excluded_from_workspace
&& (old_lints_table.map(|o| o.to_string()) != Some(lints_table.to_string()))
| (old_rust_version.map(|o| o.to_string())
!= Some(rust_version_field.to_string()))
| (old_edition_field.map(|o| o.to_string()) != Some(edition_field.to_string())));
if invalid {
if !fix {
anyhow::bail!(
"invalid inclusion of package authors or version, or non-workspaced lints, rust-version, or edition, in {}",
f.display()
);
}
fs_err::write(f, parsed.to_string())?;
log::info!("fixed package section in {}", f.display());
}
Ok(())
}