Skip to content

Commit 4ec73a6

Browse files
authored
Merge pull request #233 from nitkach/refactoring
Type constraint for paths and IntoUtf8 trait
2 parents 62e0aa6 + 994fbbf commit 4ec73a6

File tree

16 files changed

+194
-91
lines changed

16 files changed

+194
-91
lines changed

Cargo.lock

Lines changed: 19 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
@@ -29,6 +29,7 @@ bumpalo = "3.12"
2929
camino = { version = "1.1", features = ["serde1"] }
3030
cargo_metadata = "0.17"
3131
clap = { version = "4.0", features = ["string", "derive"] }
32+
expect-test = "1.4"
3233
itertools = "0.11"
3334
libloading = "0.8.0"
3435
miette = { version = "5.9", features = ["fancy-no-backtrace"] }

cargo-marker/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ tracing = { workspace = true }
3434
tracing-error = { workspace = true }
3535
tracing-subscriber = { workspace = true, features = ["env-filter"] }
3636
yansi = { workspace = true }
37+
38+
[dev-dependencies]
39+
expect-test = { workspace = true }

cargo-marker/src/backend.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ use crate::observability::prelude::*;
1212
use std::{
1313
collections::HashMap,
1414
ffi::{OsStr, OsString},
15-
path::PathBuf,
1615
};
1716

17+
use camino::Utf8PathBuf;
18+
1819
pub mod cargo;
1920
pub mod driver;
2021
pub mod lints;
@@ -31,7 +32,7 @@ pub struct Config {
3132
/// This should generally be used as a base path for everything. Notable
3233
/// exceptions can be the installation of a driver or the compilation of
3334
/// a lint for uitests.
34-
pub marker_dir: PathBuf,
35+
pub marker_dir: Utf8PathBuf,
3536
/// The list of lints.
3637
pub lints: HashMap<String, LintDependencyEntry>,
3738
/// Additional flags, which should be passed to rustc during the compilation
@@ -53,11 +54,11 @@ impl Config {
5354
})
5455
}
5556

56-
fn markers_target_dir(&self) -> PathBuf {
57+
fn markers_target_dir(&self) -> Utf8PathBuf {
5758
self.marker_dir.join("target")
5859
}
5960

60-
fn lint_crate_dir(&self) -> PathBuf {
61+
fn lint_crate_dir(&self) -> Utf8PathBuf {
6162
self.marker_dir.join("lints")
6263
}
6364
}

cargo-marker/src/backend/driver.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use super::toolchain::{get_toolchain_folder, rustup_which, Toolchain};
22
use crate::error::prelude::*;
33
use crate::observability::display::print_stage;
44
use crate::observability::prelude::*;
5+
use crate::utils::utf8::IntoUtf8;
56
use crate::{utils::is_local_driver, Result};
6-
use std::{path::Path, process::Command};
7+
use camino::Utf8Path;
8+
use std::process::Command;
79
use yansi::Paint;
810

911
pub fn marker_driver_bin_name() -> String {
@@ -28,7 +30,7 @@ pub struct DriverVersionInfo {
2830
}
2931

3032
impl DriverVersionInfo {
31-
pub fn try_from_toolchain(toolchain: &Toolchain, manifest: &Path) -> Result<DriverVersionInfo> {
33+
pub fn try_from_toolchain(toolchain: &Toolchain, manifest: &Utf8Path) -> Result<DriverVersionInfo> {
3234
// The driver has to be invoked via cargo, to ensure that the libraries
3335
// are correctly linked. Toolchains are truly fun...
3436
let output = toolchain
@@ -50,7 +52,7 @@ impl DriverVersionInfo {
5052
));
5153
}
5254

53-
let info = String::from_utf8(output.stdout).context(|| "Failed to parse the driver metadata as UTF8")?;
55+
let info = output.stdout.into_utf8()?;
5456

5557
let fields = ["toolchain", "driver", "marker-api"];
5658

cargo-marker/src/backend/lints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::Config;
22
use crate::error::prelude::*;
3-
use std::path::PathBuf;
3+
use camino::Utf8PathBuf;
44

55
mod build;
66
mod fetch;
@@ -14,7 +14,7 @@ pub struct LintCrateSource {
1414
/// that will be used to construct the dynamic library.
1515
name: String,
1616
/// The absolute path to the manifest of this lint crate
17-
manifest: PathBuf,
17+
manifest: Utf8PathBuf,
1818
}
1919

2020
/// The information of a compiled lint crate.
@@ -23,7 +23,7 @@ pub struct LintCrate {
2323
/// The name of the crate
2424
pub name: String,
2525
/// The absolute path of the compiled crate, as a dynamic library.
26-
pub file: PathBuf,
26+
pub file: Utf8PathBuf,
2727
}
2828

2929
/// This function fetches and builds all lints specified in the given [`Config`]

cargo-marker/src/backend/lints/build.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use super::{LintCrate, LintCrateSource};
22
use crate::backend::Config;
33
use crate::error::prelude::*;
44
use crate::observability::prelude::*;
5+
use crate::utils::utf8::IntoUtf8;
6+
use camino::Utf8Path;
57
use itertools::Itertools;
6-
use std::{collections::HashSet, ffi::OsStr, path::Path};
8+
use std::{collections::HashSet, ffi::OsStr};
79
use yansi::Paint;
810

911
#[cfg(target_os = "linux")]
@@ -45,15 +47,15 @@ pub fn build_lints(sources: &[LintCrateSource], config: &Config) -> Result<Vec<L
4547

4648
// Build lint crates and find the output of those builds
4749
let mut found_paths = HashSet::new();
48-
let ending = OsStr::new(DYNAMIC_LIB_FILE_ENDING);
50+
let ending = DYNAMIC_LIB_FILE_ENDING;
4951
let mut lints = Vec::with_capacity(sources.len());
5052

5153
for lint_src in sources {
5254
build_lint(lint_src, config)?;
5355
match std::fs::read_dir(&lints_dir) {
5456
Ok(dir) => {
5557
for file in dir {
56-
let file = file.unwrap().path();
58+
let file = file.unwrap().path().into_utf8()?;
5759
if file.extension() == Some(ending) && !found_paths.contains(&file) {
5860
found_paths.insert(file.clone());
5961
lints.push(LintCrate {
@@ -66,10 +68,7 @@ pub fn build_lints(sources: &[LintCrateSource], config: &Config) -> Result<Vec<L
6668
Err(err) => {
6769
// This shouldn't really be a point of failure. In this case, I'm
6870
// more interested in the HOW?
69-
panic!(
70-
"unable to read lints dir after lint compilation: {} ({err:#?})",
71-
lints_dir.display()
72-
);
71+
panic!("unable to read lints dir after lint compilation: {lints_dir} ({err:#?})");
7372
},
7473
}
7574
}
@@ -82,7 +81,7 @@ pub fn build_lints(sources: &[LintCrateSource], config: &Config) -> Result<Vec<L
8281
///
8382
/// This is an extra function to not call `delete_dir_all` and just accidentally delete
8483
/// the entire system.
85-
fn clear_lints_dir(lints_dir: &Path) -> Result {
84+
fn clear_lints_dir(lints_dir: &Utf8Path) -> Result {
8685
// Delete all files
8786
let dir = match std::fs::read_dir(lints_dir) {
8887
Ok(dir) => dir,
@@ -115,7 +114,7 @@ fn clear_lints_dir(lints_dir: &Path) -> Result {
115114
}
116115

117116
// The dir should now be empty
118-
std::fs::remove_dir(lints_dir).context(|| format!("Failed to remove lints directory {}", lints_dir.display()))
117+
std::fs::remove_dir(lints_dir).context(|| format!("Failed to remove lints directory {lints_dir}"))
119118
}
120119

121120
fn build_lint(lint_src: &LintCrateSource, config: &Config) -> Result {

cargo-marker/src/backend/lints/fetch.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use super::LintCrateSource;
1414
use crate::error::prelude::*;
1515
use crate::observability::prelude::*;
1616
use crate::{backend::Config, config::LintDependencyEntry};
17+
use camino::{Utf8Path, Utf8PathBuf};
1718
use cargo_metadata::Metadata;
1819
use std::collections::HashMap;
19-
use std::path::{Path, PathBuf};
2020

2121
/// This function fetches and locates all lint crates specified in the given
2222
/// configuration.
@@ -35,7 +35,7 @@ pub fn fetch_crates(config: &Config) -> Result<Vec<LintCrateSource>> {
3535

3636
/// This function sets up the dummy crate with all the lints listed as dependencies.
3737
/// It returns the path of the manifest, if everything was successful.
38-
fn setup_dummy_crate(config: &Config) -> Result<PathBuf> {
38+
fn setup_dummy_crate(config: &Config) -> Result<Utf8PathBuf> {
3939
/// A small hack, to have the lints namespaced under the `[dependencies]` section
4040
#[derive(serde::Serialize)]
4141
struct DepNamespace<'a> {
@@ -58,15 +58,14 @@ fn setup_dummy_crate(config: &Config) -> Result<PathBuf> {
5858
Ok(manifest_path)
5959
}
6060

61-
fn write_to_file(path: &PathBuf, content: &str) -> Result {
61+
fn write_to_file(path: &Utf8Path, content: &str) -> Result {
6262
let parent = path
6363
.parent()
64-
.unwrap_or_else(|| panic!("The file must have a parent directory. Path: {}", path.display()));
64+
.unwrap_or_else(|| panic!("The file must have a parent directory. Path: {path}"));
6565

66-
std::fs::create_dir_all(parent)
67-
.context(|| format!("Failed to create the ditectory structure for {}", parent.display()))?;
66+
std::fs::create_dir_all(parent).context(|| format!("Failed to create the directory structure for {parent}"))?;
6867

69-
std::fs::write(path, content).context(|| format!("Failed to write a file at {}", path.display()))
68+
std::fs::write(path, content).context(|| format!("Failed to write a file at {path}"))
7069
}
7170

7271
const DUMMY_MANIFEST_TEMPLATE: &str = r#"
@@ -94,7 +93,7 @@ const DUMMY_MAIN_CONTENT: &str = r#"
9493
}
9594
"#;
9695

97-
fn call_cargo_fetch(manifest: &Path, config: &Config) -> Result {
96+
fn call_cargo_fetch(manifest: &Utf8Path, config: &Config) -> Result {
9897
let mut cmd = config.toolchain.cargo.command();
9998
cmd.arg("fetch");
10099
cmd.arg("--manifest-path");
@@ -121,19 +120,14 @@ fn call_cargo_fetch(manifest: &Path, config: &Config) -> Result {
121120
Err(Error::root("cargo fetch failed for lint crates"))
122121
}
123122

124-
fn call_cargo_metadata(manifest: &PathBuf, config: &Config) -> Result<Metadata> {
123+
fn call_cargo_metadata(manifest: &Utf8Path, config: &Config) -> Result<Metadata> {
125124
config
126125
.toolchain
127126
.cargo
128127
.metadata()
129128
.manifest_path(manifest)
130129
.exec()
131-
.context(|| {
132-
format!(
133-
"Failed to get cargo metadata for the lint crates at {}",
134-
manifest.display()
135-
)
136-
})
130+
.context(|| format!("Failed to get cargo metadata for the lint crates at {manifest}"))
137131
}
138132

139133
fn extract_lint_crate_sources(metadata: &Metadata, marker_config: &Config) -> Vec<LintCrateSource> {
@@ -143,7 +137,7 @@ fn extract_lint_crate_sources(metadata: &Metadata, marker_config: &Config) -> Ve
143137
.filter(|pkg| marker_config.lints.contains_key(&pkg.name))
144138
.map(|pkg| LintCrateSource {
145139
name: pkg.name.clone(),
146-
manifest: pkg.manifest_path.clone().into(),
140+
manifest: pkg.manifest_path.clone(),
147141
})
148142
.collect()
149143
}

0 commit comments

Comments
 (0)