Skip to content

Commit f4288de

Browse files
committed
adding enum_dispatch
1 parent e4456ee commit f4288de

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ path = "src/lib.rs"
1313
clap = { version = "4.5.20", features = ["derive"] }
1414
clap_derive = "4.5.18"
1515
error-stack = "0.5.0"
16+
enum_dispatch = "0.3.13"
1617
fast-glob = "0.4.0"
1718
ignore = "0.4.23"
1819
itertools = "0.13.0"

src/cache/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
use crate::project::Error;
2+
use enum_dispatch::enum_dispatch;
23
use error_stack::Result;
4+
use file::GlobalCache;
5+
use noop::NoopCache;
36
use std::path::Path;
47

58
pub mod file;
69
pub mod noop;
710

11+
#[enum_dispatch]
12+
pub enum CacheType {
13+
GlobalCache,
14+
NoopCache,
15+
}
16+
17+
#[enum_dispatch(CacheType)]
818
pub trait Cache {
919
fn get_file_owner(&self, path: &Path) -> Result<Option<FileOwnerCacheEntry>, Error>;
1020
fn write_file_owner(&self, path: &Path, owner: Option<String>);

src/cli.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clap::{Parser, Subcommand};
22
use codeowners::{
3-
cache::{file::GlobalCache, noop::NoopCache, Cache},
3+
cache::{file::GlobalCache, noop::NoopCache, Cache, CacheType},
44
config::Config,
55
ownership::{FileOwner, Ownership},
66
project_builder::ProjectBuilder,
@@ -101,14 +101,15 @@ pub fn cli() -> Result<(), Error> {
101101
.attach_printable(format!("Can't open config file: {}", config_path.to_string_lossy()))?;
102102

103103
let config: Config = serde_yaml::from_reader(config_file).change_context(Error::Io)?;
104-
105-
let cache: &dyn Cache = if args.no_cache {
106-
&NoopCache::default() as &dyn Cache
104+
let cache: CacheType = if args.no_cache {
105+
NoopCache::default().into()
107106
} else {
108-
&GlobalCache::new(project_root.clone(), config.cache_directory.clone()).change_context(Error::Io)? as &dyn Cache
107+
GlobalCache::new(project_root.clone(), config.cache_directory.clone())
108+
.change_context(Error::Io)?
109+
.into()
109110
};
110111

111-
let mut project_builder = ProjectBuilder::new(&config, project_root.clone(), codeowners_file_path.clone(), !args.no_cache, cache);
112+
let mut project_builder = ProjectBuilder::new(&config, project_root.clone(), codeowners_file_path.clone(), !args.no_cache, &cache);
112113
let project = project_builder.build().change_context(Error::Io)?;
113114
let ownership = Ownership::build(project);
114115

src/common_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub mod tests {
1111
use tempfile::tempdir;
1212

1313
use crate::{
14-
cache::{noop::NoopCache, Cache},
14+
cache::{noop::NoopCache, CacheType},
1515
config::Config,
1616
ownership::Ownership,
1717
project_builder::ProjectBuilder,
@@ -115,13 +115,13 @@ pub mod tests {
115115
let config: Config = serde_yaml::from_reader(config_file)?;
116116

117117
let codeowners_file_path = &test_config.temp_dir_path.join(".github/CODEOWNERS");
118-
let cache: &dyn Cache = &NoopCache::default();
118+
let cache: CacheType = NoopCache::default().into();
119119
let mut builder = ProjectBuilder::new(
120120
&config,
121121
test_config.temp_dir_path.clone(),
122122
codeowners_file_path.clone(),
123123
false,
124-
cache,
124+
&cache,
125125
);
126126
let project = builder.build()?;
127127
let ownership = Ownership::build(project);
@@ -134,7 +134,7 @@ pub mod tests {
134134
test_config.temp_dir_path.clone(),
135135
codeowners_file_path.clone(),
136136
false,
137-
cache,
137+
&cache,
138138
);
139139
let project = builder.build()?;
140140
Ok(Ownership::build(project))

src/project_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rayon::iter::{IntoParallelIterator, ParallelIterator};
1010
use tracing::instrument;
1111

1212
use crate::{
13-
cache::Cache,
13+
cache::CacheType,
1414
config::Config,
1515
project::{deserializers, DirectoryCodeownersFile, Error, Package, PackageType, Project, ProjectFile, Team, VendoredGem},
1616
project_file_builder::ProjectFileBuilder,
@@ -38,7 +38,7 @@ pub struct ProjectBuilder<'a> {
3838
const INITIAL_VECTOR_CAPACITY: usize = 1000;
3939

4040
impl<'a> ProjectBuilder<'a> {
41-
pub fn new(config: &'a Config, base_path: PathBuf, codeowners_file_path: PathBuf, use_cache: bool, cache: &'a dyn Cache) -> Self {
41+
pub fn new(config: &'a Config, base_path: PathBuf, codeowners_file_path: PathBuf, use_cache: bool, cache: &'a CacheType) -> Self {
4242
let project_file_builder = ProjectFileBuilder::new(use_cache, cache);
4343
Self {
4444
project_file_builder,

src/project_file_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ use regex::Regex;
44
use std::path::{Path, PathBuf};
55

66
use crate::{
7-
cache::Cache,
7+
cache::{Cache, CacheType},
88
project::{Error, ProjectFile},
99
};
1010

1111
pub struct ProjectFileBuilder<'a> {
1212
use_cache: bool,
13-
global_cache: &'a dyn Cache,
13+
global_cache: &'a CacheType,
1414
}
1515

1616
lazy_static! {
1717
static ref TEAM_REGEX: Regex = Regex::new(r#"^(?:#|//) @team (.*)$"#).expect("error compiling regular expression");
1818
}
1919

2020
impl<'a> ProjectFileBuilder<'a> {
21-
pub fn new(use_cache: bool, global_cache: &'a dyn Cache) -> Self {
21+
pub fn new(use_cache: bool, global_cache: &'a CacheType) -> Self {
2222
Self { use_cache, global_cache }
2323
}
2424

0 commit comments

Comments
 (0)