Skip to content

Commit 45967f5

Browse files
committed
Add --edition and --style-edition options to the check_diff CLI
This let's us controll the rust language edition used to parse source code and the rustfmt style edition used for code formatting.
1 parent 1125576 commit 45967f5

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

check_diff/src/lib.rs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,78 @@ use std::fmt::{Debug, Display};
33
use std::io::{self, Write};
44
use std::path::{Path, PathBuf};
55
use std::process::{Command, Stdio};
6+
use std::str::FromStr;
67
use tracing::{debug, error, info, trace};
78
use walkdir::WalkDir;
89

10+
#[derive(Debug, Clone, Copy)]
11+
pub enum Edition {
12+
/// rust edition 2015
13+
Edition2015,
14+
/// rust edition 2018
15+
Edition2018,
16+
/// rust edition 2021
17+
Edition2021,
18+
/// rust edition 2024
19+
Edition2024,
20+
}
21+
22+
impl Edition {
23+
fn as_str(&self) -> &str {
24+
match self {
25+
Edition::Edition2015 => "2015",
26+
Edition::Edition2018 => "2018",
27+
Edition::Edition2021 => "2021",
28+
Edition::Edition2024 => "2024",
29+
}
30+
}
31+
}
32+
33+
impl FromStr for Edition {
34+
type Err = String;
35+
36+
fn from_str(s: &str) -> Result<Self, Self::Err> {
37+
match s {
38+
"2015" => Ok(Edition::Edition2015),
39+
"2018" => Ok(Edition::Edition2018),
40+
"2021" => Ok(Edition::Edition2021),
41+
"2024" => Ok(Edition::Edition2024),
42+
_ => Err(format!("Invalid rust language edition {s}")),
43+
}
44+
}
45+
}
46+
47+
#[derive(Debug, Clone, Copy)]
48+
pub enum StyleEdition {
49+
// rustfmt style_edition 2021. Also equivaluent to 2015 and 2018.
50+
Edition2021,
51+
// rustfmt style_edition 2024
52+
Edition2024,
53+
}
54+
55+
impl StyleEdition {
56+
fn as_str(&self) -> &str {
57+
match self {
58+
StyleEdition::Edition2021 => "2021",
59+
StyleEdition::Edition2024 => "2024",
60+
}
61+
}
62+
}
63+
64+
impl FromStr for StyleEdition {
65+
type Err = String;
66+
67+
fn from_str(s: &str) -> Result<Self, Self::Err> {
68+
match s {
69+
"2015" => Ok(StyleEdition::Edition2021),
70+
"2018" => Ok(StyleEdition::Edition2021),
71+
"2021" => Ok(StyleEdition::Edition2021),
72+
"2024" => Ok(StyleEdition::Edition2024),
73+
_ => Err(format!("Invalid rustfmt style edition {s}")),
74+
}
75+
}
76+
}
77+
978
pub enum FormatCodeError {
1079
// IO Error when running code formatter
1180
Io(std::io::Error),
@@ -131,6 +200,8 @@ pub trait CodeFormatter {
131200
pub struct RustfmtRunner {
132201
dynamic_library_path: String,
133202
binary_path: PathBuf,
203+
edition: Edition,
204+
style_edition: StyleEdition,
134205
}
135206

136207
impl<F, S> CheckDiffRunners<F, S> {
@@ -288,6 +359,10 @@ impl CodeFormatter for RustfmtRunner {
288359
&self.dynamic_library_path,
289360
)
290361
.args([
362+
"--edition",
363+
self.edition.as_str(),
364+
"--style-edition",
365+
self.style_edition.as_str(),
291366
"--unstable-features",
292367
"--skip-children",
293368
"--emit=stdout",
@@ -461,6 +536,8 @@ pub fn get_cargo_version() -> Result<String, CheckDiffError> {
461536
pub fn build_rustfmt_from_src(
462537
binary_path: PathBuf,
463538
dir: &Path,
539+
edition: Edition,
540+
style_edition: StyleEdition,
464541
) -> Result<RustfmtRunner, CheckDiffError> {
465542
// Because we're building standalone binaries we need to set the dynamic library path
466543
// so each rustfmt binary can find it's runtime dependencies.
@@ -482,6 +559,8 @@ pub fn build_rustfmt_from_src(
482559
Ok(RustfmtRunner {
483560
dynamic_library_path,
484561
binary_path,
562+
edition,
563+
style_edition,
485564
})
486565
}
487566

@@ -493,6 +572,8 @@ pub fn compile_rustfmt(
493572
dest: &Path,
494573
remote_repo_url: String,
495574
feature_branch: String,
575+
edition: Edition,
576+
style_edition: StyleEdition,
496577
commit_hash: Option<String>,
497578
) -> Result<CheckDiffRunners<RustfmtRunner, RustfmtRunner>, CheckDiffError> {
498579
const RUSTFMT_REPO: &str = "https://github.com/rust-lang/rustfmt.git";
@@ -504,14 +585,16 @@ pub fn compile_rustfmt(
504585

505586
let cargo_version = get_cargo_version()?;
506587
info!("Compiling with {}", cargo_version);
507-
let src_runner = build_rustfmt_from_src(dest.join("src_rustfmt"), dest)?;
588+
let src_runner =
589+
build_rustfmt_from_src(dest.join("src_rustfmt"), dest, edition, style_edition)?;
508590
let should_detach = commit_hash.is_some();
509591
git_switch(
510592
commit_hash.as_ref().unwrap_or(&feature_branch),
511593
should_detach,
512594
)?;
513595

514-
let feature_runner = build_rustfmt_from_src(dest.join("feature_rustfmt"), dest)?;
596+
let feature_runner =
597+
build_rustfmt_from_src(dest.join("feature_rustfmt"), dest, edition, style_edition)?;
515598
info!("RUSFMT_BIN {}", src_runner.get_binary_version()?);
516599
let dynamic_library_path_env_var = dynamic_library_path_env_var_name();
517600
info!(

check_diff/src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
55
use std::thread;
66

77
use check_diff::{
8-
check_diff, clone_git_repo, compile_rustfmt, get_repo_name,
8+
Edition, StyleEdition, check_diff, clone_git_repo, compile_rustfmt, get_repo_name,
99
};
1010
use clap::Parser;
1111
use tempfile::tempdir;
@@ -44,6 +44,12 @@ struct CliInputs {
4444
remote_repo_url: String,
4545
/// Name of the feature branch on the forked repo
4646
feature_branch: String,
47+
/// Rust language `edition` used to parse code. Possible values {2015, 2018, 2021, 2024}
48+
#[arg(short, long, default_value = "2015")]
49+
edition: Edition,
50+
/// rustfmt `style_edition` used when formatting code. Possible vales {2015, 2018, 2021, 2024}.
51+
#[arg(short, long, default_value = "2021")]
52+
style_edition: StyleEdition,
4753
/// Optional commit hash from the feature branch
4854
#[arg(short, long)]
4955
commit_hash: Option<String>,
@@ -64,6 +70,8 @@ fn main() -> Result<ExitCode, Error> {
6470
tmp_dir.path(),
6571
args.remote_repo_url,
6672
args.feature_branch,
73+
args.edition,
74+
args.style_edition,
6775
args.commit_hash,
6876
) else {
6977
error!("Failed to compile rustfmt");

0 commit comments

Comments
 (0)