From 626b4f71dbda8fcb3a40174785a53d48f2d1d639 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 25 Jul 2024 13:29:57 -0500 Subject: [PATCH 1/3] refactor(config): Centralize pyproject.toml value --- crates/typos-cli/src/config.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/typos-cli/src/config.rs b/crates/typos-cli/src/config.rs index 5e30f7f..19e3b55 100644 --- a/crates/typos-cli/src/config.rs +++ b/crates/typos-cli/src/config.rs @@ -5,7 +5,9 @@ use kstring::KString; use crate::file_type_specifics; pub const SUPPORTED_FILE_NAMES: &[&str] = - &["typos.toml", "_typos.toml", ".typos.toml", "pyproject.toml"]; + &["typos.toml", "_typos.toml", ".typos.toml", PYPROJECT_TOML]; + +const PYPROJECT_TOML: &str = "pyproject.toml"; #[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(deny_unknown_fields)] @@ -55,11 +57,11 @@ impl Config { ) })?; - if path.file_name().unwrap() == "pyproject.toml" { + if path.file_name().unwrap() == PYPROJECT_TOML { let config = toml::from_str::(&s)?; if config.tool.typos.is_none() { - log::debug!("No `tool.typos` section found in `pyproject.toml`, skipping"); + log::debug!("No `tool.typos` section found in `{PYPROJECT_TOML}`, skipping"); Ok(None) } else { From 95f556a9185e31d2ea83ae7d69501b0db5ae5622 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 25 Jul 2024 13:40:48 -0500 Subject: [PATCH 2/3] refactor(config): Be more direct --- crates/typos-cli/src/config.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/typos-cli/src/config.rs b/crates/typos-cli/src/config.rs index 19e3b55..577b990 100644 --- a/crates/typos-cli/src/config.rs +++ b/crates/typos-cli/src/config.rs @@ -60,12 +60,12 @@ impl Config { if path.file_name().unwrap() == PYPROJECT_TOML { let config = toml::from_str::(&s)?; - if config.tool.typos.is_none() { + if let Some(typos) = config.tool.typos { + Ok(Some(typos)) + } else { log::debug!("No `tool.typos` section found in `{PYPROJECT_TOML}`, skipping"); Ok(None) - } else { - Ok(config.tool.typos) } } else { Self::from_toml(&s).map(Some) From d5453cc46cd302f57db956ba2aa078294cfcf5ec Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 25 Jul 2024 13:44:27 -0500 Subject: [PATCH 3/3] feat(config): Load config from Cargo.toml Fixes #1060 --- crates/typos-cli/src/config.rs | 50 ++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/crates/typos-cli/src/config.rs b/crates/typos-cli/src/config.rs index 577b990..34a1994 100644 --- a/crates/typos-cli/src/config.rs +++ b/crates/typos-cli/src/config.rs @@ -4,9 +4,15 @@ use kstring::KString; use crate::file_type_specifics; -pub const SUPPORTED_FILE_NAMES: &[&str] = - &["typos.toml", "_typos.toml", ".typos.toml", PYPROJECT_TOML]; +pub const SUPPORTED_FILE_NAMES: &[&str] = &[ + "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)] @@ -22,6 +28,28 @@ pub struct Config { 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, + pub package: Option, +} + +#[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, +} + #[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(default)] #[serde(rename_all = "kebab-case")] @@ -57,7 +85,23 @@ impl Config { ) })?; - if path.file_name().unwrap() == PYPROJECT_TOML { + if path.file_name().unwrap() == CARGO_TOML { + let config = toml::from_str::(&s)?; + let typos = config + .workspace + .and_then(|w| w.metadata.typos) + .or(config.package.and_then(|p| p.metadata.typos)); + + if let Some(typos) = typos { + Ok(Some(typos)) + } else { + log::debug!( + "No `package.metadata.typos` section found in `{CARGO_TOML}`, skipping" + ); + + Ok(None) + } + } else if path.file_name().unwrap() == PYPROJECT_TOML { let config = toml::from_str::(&s)?; if let Some(typos) = config.tool.typos {