Merge pull request #1063 from epage/cargo

feat(config): Load config from Cargo.toml
This commit is contained in:
Ed Page 2024-07-25 15:29:24 -05:00 committed by GitHub
commit d74fc5d53c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,8 +4,16 @@ use kstring::KString;
use crate::file_type_specifics; use crate::file_type_specifics;
pub const SUPPORTED_FILE_NAMES: &[&str] = pub const SUPPORTED_FILE_NAMES: &[&str] = &[
&["typos.toml", "_typos.toml", ".typos.toml", "pyproject.toml"]; "typos.toml",
"_typos.toml",
".typos.toml",
CARGO_TOML,
PYPROJECT_TOML,
];
const CARGO_TOML: &str = "Cargo.toml";
const PYPROJECT_TOML: &str = "pyproject.toml";
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
@ -20,6 +28,28 @@ pub struct Config {
pub overrides: EngineConfig, pub overrides: EngineConfig,
} }
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct CargoTomlConfig {
pub workspace: Option<CargoTomlPackage>,
pub package: Option<CargoTomlPackage>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct CargoTomlPackage {
pub metadata: CargoTomlMetadata,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct CargoTomlMetadata {
pub typos: Option<Config>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)] #[serde(default)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
@ -55,15 +85,31 @@ impl Config {
) )
})?; })?;
if path.file_name().unwrap() == "pyproject.toml" { if path.file_name().unwrap() == CARGO_TOML {
let config = toml::from_str::<PyprojectTomlConfig>(&s)?; let config = toml::from_str::<CargoTomlConfig>(&s)?;
let typos = config
.workspace
.and_then(|w| w.metadata.typos)
.or(config.package.and_then(|p| p.metadata.typos));
if config.tool.typos.is_none() { if let Some(typos) = typos {
log::debug!("No `tool.typos` section found in `pyproject.toml`, skipping"); Ok(Some(typos))
} else {
log::debug!(
"No `package.metadata.typos` section found in `{CARGO_TOML}`, skipping"
);
Ok(None) Ok(None)
}
} else if path.file_name().unwrap() == PYPROJECT_TOML {
let config = toml::from_str::<PyprojectTomlConfig>(&s)?;
if let Some(typos) = config.tool.typos {
Ok(Some(typos))
} else { } else {
Ok(config.tool.typos) log::debug!("No `tool.typos` section found in `{PYPROJECT_TOML}`, skipping");
Ok(None)
} }
} else { } else {
Self::from_toml(&s).map(Some) Self::from_toml(&s).map(Some)