Skip to content

Commit

Permalink
refactor(cli): Decouple walk and engine policies
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Page committed Apr 1, 2021
1 parent 47eb554 commit 13617fa
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,24 @@ impl<'s> ConfigEngine<'s> {
pub fn policy(&self, path: &std::path::Path) -> Policy<'_, '_> {
let dir = self.get_dir(path).expect("`walk()` should be called first");
Policy {
check_filenames: dir.check_filenames,
check_files: dir.check_files,
binary: dir.binary,
tokenizer: self.get_tokenizer(dir),
dict: self.get_dict(dir),
check_filenames: dir.default.check_filenames,
check_files: dir.default.check_files,
binary: dir.default.binary,
tokenizer: self.get_tokenizer(&dir.default),
dict: self.get_dict(&dir.default),
}
}

fn get_walk(&self, dir: &DirConfig) -> &crate::config::Walk {
self.walk.get(dir.walk)
}

fn get_tokenizer(&self, dir: &DirConfig) -> &typos::tokens::Tokenizer {
self.tokenizer.get(dir.tokenizer)
fn get_tokenizer(&self, file: &FileConfig) -> &typos::tokens::Tokenizer {
self.tokenizer.get(file.tokenizer)
}

fn get_dict(&self, dir: &DirConfig) -> &dyn typos::Dictionary {
self.dict.get(dir.dict)
fn get_dict(&self, file: &FileConfig) -> &dyn typos::Dictionary {
self.dict.get(file.dict)
}

fn get_dir(&self, path: &std::path::Path) -> Option<&DirConfig> {
Expand Down Expand Up @@ -141,14 +141,27 @@ impl<'s> ConfigEngine<'s> {
}

let config = self.load_config(cwd)?;

let crate::config::Config { files, default } = config;
let binary = default.binary();
let check_filename = default.check_filename();
let check_file = default.check_file();

let walk = self.walk.intern(files);
let default = self.init_file_config(default)?;

let dir = DirConfig { walk, default };

self.configs.insert(cwd.to_owned(), dir);
Ok(())
}

fn init_file_config(
&mut self,
engine: crate::config::EngineConfig,
) -> Result<FileConfig, anyhow::Error> {
let binary = engine.binary();
let check_filename = engine.check_filename();
let check_file = engine.check_file();
let crate::config::EngineConfig {
tokenizer, dict, ..
} = default;
} = engine;
let tokenizer_config =
tokenizer.unwrap_or_else(crate::config::TokenizerConfig::from_defaults);
let dict_config = dict.unwrap_or_else(crate::config::DictConfig::from_defaults);
Expand All @@ -175,20 +188,16 @@ impl<'s> ConfigEngine<'s> {
);

let dict = self.dict.intern(dict);
let walk = self.walk.intern(files);
let tokenizer = self.tokenizer.intern(tokenizer);

let dir = DirConfig {
walk,
let file = FileConfig {
check_filenames: check_filename,
check_files: check_file,
binary,
tokenizer,
dict,
};

self.configs.insert(cwd.to_owned(), dir);
Ok(())
Ok(file)
}
}

Expand Down Expand Up @@ -222,6 +231,10 @@ impl<T> Default for Intern<T> {

struct DirConfig {
walk: usize,
default: FileConfig,
}

struct FileConfig {
tokenizer: usize,
dict: usize,
check_filenames: bool,
Expand Down

0 comments on commit 13617fa

Please sign in to comment.