Skip to content

Commit

Permalink
feat(config): Support for pyproject.toml files (#790)
Browse files Browse the repository at this point in the history
This PR adds support for parsing `pyproject.toml` config files. The convention
for these files is to put any tooling related configuration into the
`tool.NAME` section, so in this case, `tool.typos`. I have verified that the
changes are pulled correctly, even if the `tool.typos` section is not present.

Closes #361
  • Loading branch information
dosisod authored Sep 6, 2023
1 parent 65d2fb6 commit b856b96
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
12 changes: 9 additions & 3 deletions crates/typos-cli/src/bin/typos-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi
if let Some(path) = args.custom_config.as_ref() {
let custom = typos_cli::config::Config::from_file(path)
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
overrides.update(&custom);
if let Some(custom) = custom {
overrides.update(&custom);
}
}
overrides.update(&args.config.to_config());
engine.set_overrides(overrides);
Expand Down Expand Up @@ -119,7 +121,9 @@ fn run_type_list(args: &args::Args) -> proc_exit::ExitResult {
if let Some(path) = args.custom_config.as_ref() {
let custom = typos_cli::config::Config::from_file(path)
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
overrides.update(&custom);
if let Some(custom) = custom {
overrides.update(&custom);
}
}
overrides.update(&args.config.to_config());
engine.set_overrides(overrides);
Expand Down Expand Up @@ -154,7 +158,9 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
if let Some(path) = args.custom_config.as_ref() {
let custom = typos_cli::config::Config::from_file(path)
.with_code(proc_exit::sysexits::CONFIG_ERR)?;
overrides.update(&custom);
if let Some(custom) = custom {
overrides.update(&custom);
}
}
overrides.update(&args.config.to_config());
engine.set_overrides(overrides);
Expand Down
66 changes: 47 additions & 19 deletions crates/typos-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use kstring::KString;

use crate::file_type_specifics;

pub const SUPPORTED_FILE_NAMES: &[&str] = &["typos.toml", "_typos.toml", ".typos.toml"];
pub const SUPPORTED_FILE_NAMES: &[&str] =
&["typos.toml", "_typos.toml", ".typos.toml", "pyproject.toml"];

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
Expand All @@ -19,26 +20,54 @@ 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 PyprojectTomlConfig {
pub tool: PyprojectTomlTool,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct PyprojectTomlTool {
pub typos: Option<Config>,
}

impl Config {
pub fn from_dir(cwd: &std::path::Path) -> Result<Option<Self>, anyhow::Error> {
let config = if let Some(path) = find_project_file(cwd, SUPPORTED_FILE_NAMES) {
log::debug!("Loading {}", path.display());
Some(Self::from_file(&path)?)
} else {
None
};
Ok(config)
for file in find_project_files(cwd, SUPPORTED_FILE_NAMES) {
log::debug!("Loading {}", file.display());
if let Some(config) = Self::from_file(&file)? {
return Ok(Some(config));
}
}

Ok(None)
}

pub fn from_file(path: &std::path::Path) -> Result<Self, anyhow::Error> {
pub fn from_file(path: &std::path::Path) -> Result<Option<Self>, anyhow::Error> {
let s = std::fs::read_to_string(path).map_err(|err| {
let kind = err.kind();
std::io::Error::new(
kind,
format!("could not read config at `{}`", path.display()),
)
})?;
Self::from_toml(&s)

if path.file_name().unwrap() == "pyproject.toml" {
let config = toml::from_str::<PyprojectTomlConfig>(&s)?;

if config.tool.typos.is_none() {
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)
}
}

pub fn from_toml(data: &str) -> Result<Self, anyhow::Error> {
Expand Down Expand Up @@ -455,15 +484,14 @@ impl DictConfig {
}
}

fn find_project_file(dir: &std::path::Path, names: &[&str]) -> Option<std::path::PathBuf> {
let mut file_path = dir.join("placeholder");
for name in names {
file_path.set_file_name(name);
if file_path.exists() {
return Some(file_path);
}
}
None
fn find_project_files<'a>(
dir: &'a std::path::Path,
names: &'a [&'a str],
) -> impl Iterator<Item = std::path::PathBuf> + 'a {
names
.iter()
.map(|name| dir.join(name))
.filter(|path| path.exists())
}

impl PartialEq for DictConfig {
Expand Down

0 comments on commit b856b96

Please sign in to comment.