Skip to content

Commit

Permalink
feat(config): Allow separating config from source
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Page committed Oct 30, 2020
1 parent 736db10 commit f0c24b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/reference.md
Expand Up @@ -8,7 +8,7 @@ Configuration is read from the following (in precedence order)

- Command line arguments
- File specified via `--config PATH`
- Search parents of specified file / directory for `typos.toml`
- Search parents of specified file / directory for one of `typos.toml`, `_typos.toml`, or `.typos.toml`

### Config Fields

Expand Down
21 changes: 10 additions & 11 deletions src/config.rs
Expand Up @@ -131,7 +131,7 @@ impl Config {
}

pub fn derive(cwd: &std::path::Path) -> Result<Self, anyhow::Error> {
if let Some(path) = find_project_file(cwd.to_owned(), "typos.toml") {
if let Some(path) = find_project_file(cwd, &["typos.toml", "_typos.toml", ".typos.toml"]) {
Self::from_file(&path)
} else {
Ok(Default::default())
Expand Down Expand Up @@ -435,18 +435,17 @@ impl FileSource for FileConfig {
}
}

fn find_project_file(dir: std::path::PathBuf, name: &str) -> Option<std::path::PathBuf> {
let mut file_path = dir;
file_path.push(name);
while !file_path.exists() {
file_path.pop(); // filename
let hit_bottom = !file_path.pop();
if hit_bottom {
return None;
fn find_project_file(dir: &std::path::Path, names: &[&str]) -> Option<std::path::PathBuf> {
for ancestor in dir.ancestors() {
let mut file_path = ancestor.join("placeholder");
for name in names {
file_path.set_file_name(name);
if file_path.exists() {
return Some(file_path);
}
}
file_path.push(name);
}
Some(file_path)
None
}

#[derive(Debug, Copy, Clone, serde::Serialize, serde::Deserialize)]
Expand Down

0 comments on commit f0c24b0

Please sign in to comment.