Lintcheck: Refactor CrateSource

This commit is contained in:
xFrednet 2024-07-09 23:51:31 +02:00
parent 279494eced
commit 601a61fe27
No known key found for this signature in database
GPG Key ID: F5C59D0E669E5302
2 changed files with 33 additions and 42 deletions

View File

@ -37,28 +37,22 @@ struct TomlCrate {
/// Represents an archive we download from crates.io, or a git repo, or a local repo/folder /// Represents an archive we download from crates.io, or a git repo, or a local repo/folder
/// Once processed (downloaded/extracted/cloned/copied...), this will be translated into a `Crate` /// Once processed (downloaded/extracted/cloned/copied...), this will be translated into a `Crate`
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Ord, PartialOrd)]
pub struct CrateWithSource {
pub name: String,
pub source: CrateSource,
pub options: Option<Vec<String>>,
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Ord, PartialOrd)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Ord, PartialOrd)]
pub enum CrateSource { pub enum CrateSource {
CratesIo { CratesIo { version: String },
name: String, Git { url: String, commit: String },
version: String, Path { path: PathBuf },
options: Option<Vec<String>>,
},
Git {
name: String,
url: String,
commit: String,
options: Option<Vec<String>>,
},
Path {
name: String,
path: PathBuf,
options: Option<Vec<String>>,
},
} }
/// Read a `lintcheck_crates.toml` file /// Read a `lintcheck_crates.toml` file
pub fn read_crates(toml_path: &Path) -> (Vec<CrateSource>, RecursiveOptions) { pub fn read_crates(toml_path: &Path) -> (Vec<CrateWithSource>, RecursiveOptions) {
let toml_content: String = let toml_content: String =
fs::read_to_string(toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display())); fs::read_to_string(toml_path).unwrap_or_else(|_| panic!("Failed to read {}", toml_path.display()));
let crate_list: SourceList = let crate_list: SourceList =
@ -71,23 +65,29 @@ pub fn read_crates(toml_path: &Path) -> (Vec<CrateSource>, RecursiveOptions) {
let mut crate_sources = Vec::new(); let mut crate_sources = Vec::new();
for tk in tomlcrates { for tk in tomlcrates {
if let Some(ref path) = tk.path { if let Some(ref path) = tk.path {
crate_sources.push(CrateSource::Path { crate_sources.push(CrateWithSource {
name: tk.name.clone(), name: tk.name.clone(),
path: PathBuf::from(path), source: CrateSource::Path {
path: PathBuf::from(path),
},
options: tk.options.clone(), options: tk.options.clone(),
}); });
} else if let Some(ref version) = tk.version { } else if let Some(ref version) = tk.version {
crate_sources.push(CrateSource::CratesIo { crate_sources.push(CrateWithSource {
name: tk.name.clone(), name: tk.name.clone(),
version: version.to_string(), source: CrateSource::CratesIo {
version: version.to_string(),
},
options: tk.options.clone(), options: tk.options.clone(),
}); });
} else if tk.git_url.is_some() && tk.git_hash.is_some() { } else if tk.git_url.is_some() && tk.git_hash.is_some() {
// otherwise, we should have a git source // otherwise, we should have a git source
crate_sources.push(CrateSource::Git { crate_sources.push(CrateWithSource {
name: tk.name.clone(), name: tk.name.clone(),
url: tk.git_url.clone().unwrap(), source: CrateSource::Git {
commit: tk.git_hash.clone().unwrap(), url: tk.git_url.clone().unwrap(),
commit: tk.git_hash.clone().unwrap(),
},
options: tk.options.clone(), options: tk.options.clone(),
}); });
} else { } else {
@ -117,7 +117,7 @@ pub fn read_crates(toml_path: &Path) -> (Vec<CrateSource>, RecursiveOptions) {
(crate_sources, crate_list.recursive) (crate_sources, crate_list.recursive)
} }
impl CrateSource { impl CrateWithSource {
/// Makes the sources available on the disk for clippy to check. /// Makes the sources available on the disk for clippy to check.
/// Clones a git repo and checks out the specified commit or downloads a crate from crates.io or /// Clones a git repo and checks out the specified commit or downloads a crate from crates.io or
/// copies a local folder /// copies a local folder
@ -139,8 +139,10 @@ impl CrateSource {
retries += 1; retries += 1;
} }
} }
match self { let name = &self.name;
CrateSource::CratesIo { name, version, options } => { let options = &self.options;
match &self.source {
CrateSource::CratesIo { version } => {
let extract_dir = PathBuf::from(LINTCHECK_SOURCES); let extract_dir = PathBuf::from(LINTCHECK_SOURCES);
let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS); let krate_download_dir = PathBuf::from(LINTCHECK_DOWNLOADS);
@ -173,12 +175,7 @@ impl CrateSource {
options: options.clone(), options: options.clone(),
} }
}, },
CrateSource::Git { CrateSource::Git { url, commit } => {
name,
url,
commit,
options,
} => {
let repo_path = { let repo_path = {
let mut repo_path = PathBuf::from(LINTCHECK_SOURCES); let mut repo_path = PathBuf::from(LINTCHECK_SOURCES);
// add a -git suffix in case we have the same crate from crates.io and a git repo // add a -git suffix in case we have the same crate from crates.io and a git repo
@ -219,7 +216,7 @@ impl CrateSource {
options: options.clone(), options: options.clone(),
} }
}, },
CrateSource::Path { name, path, options } => { CrateSource::Path { path } => {
fn is_cache_dir(entry: &DirEntry) -> bool { fn is_cache_dir(entry: &DirEntry) -> bool {
fs::read(entry.path().join("CACHEDIR.TAG")) fs::read(entry.path().join("CACHEDIR.TAG"))
.map(|x| x.starts_with(b"Signature: 8a477f597d28d172789f06886806bc55")) .map(|x| x.starts_with(b"Signature: 8a477f597d28d172789f06886806bc55"))

View File

@ -38,7 +38,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::{env, fs}; use std::{env, fs};
use cargo_metadata::Message; use cargo_metadata::Message;
use input::{read_crates, CrateSource}; use input::read_crates;
use output::{ClippyCheckOutput, ClippyWarning, RustcIce}; use output::{ClippyCheckOutput, ClippyWarning, RustcIce};
use rayon::prelude::*; use rayon::prelude::*;
@ -292,13 +292,7 @@ fn lintcheck(config: LintcheckConfig) {
.into_iter() .into_iter()
.filter(|krate| { .filter(|krate| {
if let Some(only_one_crate) = &config.only { if let Some(only_one_crate) = &config.only {
let name = match krate { krate.name == *only_one_crate
CrateSource::CratesIo { name, .. }
| CrateSource::Git { name, .. }
| CrateSource::Path { name, .. } => name,
};
name == only_one_crate
} else { } else {
true true
} }