Skip to content

Commit 601a61f

Browse files
committed
Lintcheck: Refactor CrateSource
1 parent 279494e commit 601a61f

File tree

2 files changed

+33
-42
lines changed

2 files changed

+33
-42
lines changed

lintcheck/src/input.rs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,22 @@ struct TomlCrate {
3737

3838
/// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
3939
/// Once processed (downloaded/extracted/cloned/copied...), this will be translated into a `Crate`
40+
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Ord, PartialOrd)]
41+
pub struct CrateWithSource {
42+
pub name: String,
43+
pub source: CrateSource,
44+
pub options: Option<Vec<String>>,
45+
}
46+
4047
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Ord, PartialOrd)]
4148
pub enum CrateSource {
42-
CratesIo {
43-
name: String,
44-
version: String,
45-
options: Option<Vec<String>>,
46-
},
47-
Git {
48-
name: String,
49-
url: String,
50-
commit: String,
51-
options: Option<Vec<String>>,
52-
},
53-
Path {
54-
name: String,
55-
path: PathBuf,
56-
options: Option<Vec<String>>,
57-
},
49+
CratesIo { version: String },
50+
Git { url: String, commit: String },
51+
Path { path: PathBuf },
5852
}
5953

6054
/// Read a `lintcheck_crates.toml` file
61-
pub fn read_crates(toml_path: &Path) -> (Vec<CrateSource>, RecursiveOptions) {
55+
pub fn read_crates(toml_path: &Path) -> (Vec<CrateWithSource>, RecursiveOptions) {
6256
let toml_content: String =
6357
fs::read_to_string(toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
6458
let crate_list: SourceList =
@@ -71,23 +65,29 @@ pub fn read_crates(toml_path: &Path) -> (Vec<CrateSource>, RecursiveOptions) {
7165
let mut crate_sources = Vec::new();
7266
for tk in tomlcrates {
7367
if let Some(ref path) = tk.path {
74-
crate_sources.push(CrateSource::Path {
68+
crate_sources.push(CrateWithSource {
7569
name: tk.name.clone(),
76-
path: PathBuf::from(path),
70+
source: CrateSource::Path {
71+
path: PathBuf::from(path),
72+
},
7773
options: tk.options.clone(),
7874
});
7975
} else if let Some(ref version) = tk.version {
80-
crate_sources.push(CrateSource::CratesIo {
76+
crate_sources.push(CrateWithSource {
8177
name: tk.name.clone(),
82-
version: version.to_string(),
78+
source: CrateSource::CratesIo {
79+
version: version.to_string(),
80+
},
8381
options: tk.options.clone(),
8482
});
8583
} else if tk.git_url.is_some() && tk.git_hash.is_some() {
8684
// otherwise, we should have a git source
87-
crate_sources.push(CrateSource::Git {
85+
crate_sources.push(CrateWithSource {
8886
name: tk.name.clone(),
89-
url: tk.git_url.clone().unwrap(),
90-
commit: tk.git_hash.clone().unwrap(),
87+
source: CrateSource::Git {
88+
url: tk.git_url.clone().unwrap(),
89+
commit: tk.git_hash.clone().unwrap(),
90+
},
9191
options: tk.options.clone(),
9292
});
9393
} else {
@@ -117,7 +117,7 @@ pub fn read_crates(toml_path: &Path) -> (Vec<CrateSource>, RecursiveOptions) {
117117
(crate_sources, crate_list.recursive)
118118
}
119119

120-
impl CrateSource {
120+
impl CrateWithSource {
121121
/// Makes the sources available on the disk for clippy to check.
122122
/// Clones a git repo and checks out the specified commit or downloads a crate from crates.io or
123123
/// copies a local folder
@@ -139,8 +139,10 @@ impl CrateSource {
139139
retries += 1;
140140
}
141141
}
142-
match self {
143-
CrateSource::CratesIo { name, version, options } => {
142+
let name = &self.name;
143+
let options = &self.options;
144+
match &self.source {
145+
CrateSource::CratesIo { version } => {
144146
let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
145147
let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
146148

@@ -173,12 +175,7 @@ impl CrateSource {
173175
options: options.clone(),
174176
}
175177
},
176-
CrateSource::Git {
177-
name,
178-
url,
179-
commit,
180-
options,
181-
} => {
178+
CrateSource::Git { url, commit } => {
182179
let repo_path = {
183180
let mut repo_path = PathBuf::from(LINTCHECK_SOURCES);
184181
// add a -git suffix in case we have the same crate from crates.io and a git repo
@@ -219,7 +216,7 @@ impl CrateSource {
219216
options: options.clone(),
220217
}
221218
},
222-
CrateSource::Path { name, path, options } => {
219+
CrateSource::Path { path } => {
223220
fn is_cache_dir(entry: &DirEntry) -> bool {
224221
fs::read(entry.path().join("CACHEDIR.TAG"))
225222
.map(|x| x.starts_with(b"Signature: 8a477f597d28d172789f06886806bc55"))

lintcheck/src/main.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
3838
use std::{env, fs};
3939

4040
use cargo_metadata::Message;
41-
use input::{read_crates, CrateSource};
41+
use input::read_crates;
4242
use output::{ClippyCheckOutput, ClippyWarning, RustcIce};
4343
use rayon::prelude::*;
4444

@@ -292,13 +292,7 @@ fn lintcheck(config: LintcheckConfig) {
292292
.into_iter()
293293
.filter(|krate| {
294294
if let Some(only_one_crate) = &config.only {
295-
let name = match krate {
296-
CrateSource::CratesIo { name, .. }
297-
| CrateSource::Git { name, .. }
298-
| CrateSource::Path { name, .. } => name,
299-
};
300-
301-
name == only_one_crate
295+
krate.name == *only_one_crate
302296
} else {
303297
true
304298
}

0 commit comments

Comments
 (0)