Skip to content

Commit d0af80d

Browse files
committed
some refactoring
1 parent fca490c commit d0af80d

File tree

3 files changed

+64
-48
lines changed

3 files changed

+64
-48
lines changed

cargo-marker/src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::Deserialize;
88

99
use toml_edit::easy::{from_str, Value};
1010

11-
use crate::ExitStatus;
11+
use crate::{lints::LintCrateSpec, ExitStatus};
1212

1313
const CARGO_TOML: &str = "Cargo.toml";
1414

@@ -118,7 +118,7 @@ impl Config {
118118
Ok(marker_config)
119119
}
120120

121-
pub fn collect_crates(&self) -> Result<Vec<(String, String)>, ExitStatus> {
121+
pub fn collect_crates(&self) -> Result<Vec<LintCrateSpec>, ExitStatus> {
122122
self.lints
123123
.iter()
124124
.map(|(name, dep)| match dep {
@@ -141,9 +141,9 @@ impl Config {
141141
);
142142
if let Some(ref path) = dep.path {
143143
if let Some(ref package) = dep.package {
144-
return Ok((package.clone(), path.clone()));
144+
return Ok(LintCrateSpec::new(Some(package), path.as_ref()));
145145
}
146-
return Ok((name.clone(), path.clone()));
146+
return Ok(LintCrateSpec::new(Some(name), path.as_ref()));
147147
}
148148
eprintln!("No `path` field found for lint crate {name}");
149149
Err(ExitStatus::BadConfiguration)

cargo-marker/src/lints.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,35 @@ use std::{
66

77
use crate::ExitStatus;
88

9+
pub struct LintCrateSpec<'a> {
10+
package_name: Option<&'a str>,
11+
dir: &'a Path,
12+
}
13+
14+
impl<'a> LintCrateSpec<'a> {
15+
pub fn new(package_name: Option<&'a str>, dir: &'a Path) -> Self {
16+
Self { package_name, dir }
17+
}
18+
19+
/// Currently only checks for semicolons, can be extended in the future
20+
pub fn validate(&self) -> bool {
21+
self.dir.to_string_lossy().contains(';')
22+
}
23+
24+
pub fn build_self(&self, target_dir: &Path, verbose: bool) -> Result<PathBuf, ExitStatus> {
25+
build_local_lint_crate(self, target_dir, verbose)
26+
}
27+
}
28+
929
/// This creates a debug build for a local crate. The path of the build library
1030
/// will be returned, if the operation was successful.
1131
pub fn build_local_lint_crate(
12-
package_name: Option<&str>,
13-
crate_dir: &Path,
32+
krate: &LintCrateSpec<'_>,
1433
target_dir: &Path,
1534
verbose: bool,
1635
) -> Result<PathBuf, ExitStatus> {
17-
if !crate_dir.exists() {
18-
eprintln!("The given lint can't be found, searched at: `{}`", crate_dir.display());
36+
if !krate.dir.exists() {
37+
eprintln!("The given lint can't be found, searched at: `{}`", krate.dir.display());
1938
return Err(ExitStatus::LintCrateNotFound);
2039
}
2140

@@ -25,12 +44,12 @@ pub fn build_local_lint_crate(
2544
if verbose {
2645
cmd.arg("--verbose");
2746
}
28-
if let Some(name) = package_name {
47+
if let Some(name) = krate.package_name {
2948
cmd.arg("--package");
3049
cmd.arg(name);
3150
}
3251
let exit_status = cmd
33-
.current_dir(std::fs::canonicalize(crate_dir).unwrap())
52+
.current_dir(std::fs::canonicalize(krate.dir).unwrap())
3453
.args(["--lib", "--target-dir"])
3554
.arg(target_dir.as_os_str())
3655
.env("RUSTFLAGS", "--cap-lints=allow")
@@ -53,7 +72,7 @@ pub fn build_local_lint_crate(
5372
// See marker#60
5473
let file_name = format!(
5574
"{lib_file_prefix}{}",
56-
crate_dir.file_name().and_then(OsStr::to_str).unwrap_or_default()
75+
krate.dir.file_name().and_then(OsStr::to_str).unwrap_or_default()
5776
);
5877
// Here `debug` is attached as the crate is build with the `cargo build` command
5978
let mut krate_path = target_dir.join("debug").join(file_name);

cargo-marker/src/main.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,26 @@ use std::{
1111
ffi::{OsStr, OsString},
1212
fs::create_dir_all,
1313
io,
14-
path::{Path, PathBuf},
14+
path::Path,
1515
process::exit,
1616
};
1717

1818
use cli::get_clap_config;
1919
use config::Config;
2020
use driver::{get_driver_path, run_driver};
21-
use lints::build_local_lint_crate;
21+
use lints::LintCrateSpec;
2222
use once_cell::sync::Lazy;
2323

2424
use crate::driver::print_driver_version;
2525

2626
const CARGO_ARGS_SEPARATOR: &str = "--";
2727
const VERSION: &str = concat!("cargo-marker ", env!("CARGO_PKG_VERSION"));
2828
const LINT_KRATES_BASE_DIR: &str = "./target/marker";
29+
const NO_LINTS_ERROR: &str = concat!(
30+
"Please provide at least one valid lint crate, ",
31+
"with the `--lints` argument, ",
32+
"or `[workspace.metadata.marker.lints]` in `Cargo.toml`"
33+
);
2934
static MARKER_LINT_DIR: Lazy<String> = Lazy::new(|| prepare_lint_build_dir("marker", "marker"));
3035

3136
#[derive(Debug)]
@@ -79,28 +84,21 @@ fn prepare_lint_build_dir(dir_name: &str, info_name: &str) -> String {
7984
.to_string()
8085
}
8186

82-
fn choose_lint_crates(
83-
args: &clap::ArgMatches,
84-
config: Option<Config>,
85-
) -> Result<Vec<(Option<String>, OsString)>, ExitStatus> {
86-
let lint_crates: Vec<(Option<String>, OsString)> = match args.get_many::<OsString>("lints") {
87-
Some(v) => v.cloned().map(|v| (None, v)).collect(),
87+
fn choose_lint_crates<'a>(
88+
args: &'a clap::ArgMatches,
89+
config: Option<&'a Config>,
90+
) -> Result<Vec<LintCrateSpec<'a>>, ExitStatus> {
91+
match args.get_many::<OsString>("lints") {
92+
Some(v) => Ok(v.map(|v| LintCrateSpec::new(None, v.as_ref())).collect()),
8893
None => {
8994
if let Some(config) = config {
90-
config
91-
.collect_crates()?
92-
.into_iter()
93-
.map(|(name, path)| (Some(name), path.into()))
94-
.collect()
95+
config.collect_crates()
9596
} else {
96-
eprintln!(
97-
"Please provide at least one valid lint crate, with the `--lints` argument, or `[workspace.metadata.marker.lints]` in `Cargo.toml`"
98-
);
99-
return Err(ExitStatus::NoLints);
97+
eprintln!("{NO_LINTS_ERROR}");
98+
Err(ExitStatus::NoLints)
10099
}
101100
},
102-
};
103-
Ok(lint_crates)
101+
}
104102
}
105103

106104
fn main() -> Result<(), ExitStatus> {
@@ -130,14 +128,24 @@ fn main() -> Result<(), ExitStatus> {
130128

131129
match matches.subcommand() {
132130
Some(("setup", _args)) => driver::install_driver(verbose, dev_build),
133-
Some(("check", args)) => run_check(&choose_lint_crates(args, config)?, verbose, dev_build, test_build),
134-
None => run_check(&choose_lint_crates(&matches, config)?, verbose, dev_build, test_build),
131+
Some(("check", args)) => run_check(
132+
&choose_lint_crates(args, config.as_ref())?,
133+
verbose,
134+
dev_build,
135+
test_build,
136+
),
137+
None => run_check(
138+
&choose_lint_crates(&matches, config.as_ref())?,
139+
verbose,
140+
dev_build,
141+
test_build,
142+
),
135143
_ => unreachable!(),
136144
}
137145
}
138146

139147
fn run_check(
140-
crate_entries: &[(Option<String>, OsString)],
148+
crate_entries: &[LintCrateSpec],
141149
verbose: bool,
142150
dev_build: bool,
143151
test_build: bool,
@@ -148,16 +156,11 @@ fn run_check(
148156
}
149157

150158
if crate_entries.is_empty() {
151-
eprintln!(
152-
"Please provide at least one valid lint crate, with the `--lints` argument, or `[workspace.metadata.marker.lints]` in `Cargo.toml`"
153-
);
159+
eprintln!("{NO_LINTS_ERROR}");
154160
return Err(ExitStatus::NoLints);
155161
}
156162

157-
if crate_entries
158-
.iter()
159-
.any(|(_name, path)| path.to_string_lossy().contains(';'))
160-
{
163+
if crate_entries.iter().any(LintCrateSpec::validate) {
161164
eprintln!("The absolute paths of lint crates are not allowed to contain a `;`");
162165
return Err(ExitStatus::InvalidValue);
163166
}
@@ -167,14 +170,8 @@ fn run_check(
167170
println!();
168171
println!("Compiling Lints:");
169172
let target_dir = Path::new(&*MARKER_LINT_DIR);
170-
for (name, path) in crate_entries {
171-
let src_dir = PathBuf::from(path);
172-
let crate_file = build_local_lint_crate(
173-
name.as_ref().map(String::as_str),
174-
src_dir.as_path(),
175-
target_dir,
176-
verbose,
177-
)?;
173+
for krate in crate_entries {
174+
let crate_file = krate.build_self(target_dir, verbose)?;
178175
lint_crates.push(crate_file.as_os_str().to_os_string());
179176
}
180177

0 commit comments

Comments
 (0)