@@ -3,9 +3,78 @@ use std::fmt::{Debug, Display};
33use std:: io:: { self , Write } ;
44use std:: path:: { Path , PathBuf } ;
55use std:: process:: { Command , Stdio } ;
6+ use std:: str:: FromStr ;
67use tracing:: { debug, error, info, trace} ;
78use 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+
978pub enum FormatCodeError {
1079 // IO Error when running code formatter
1180 Io ( std:: io:: Error ) ,
@@ -131,6 +200,8 @@ pub trait CodeFormatter {
131200pub struct RustfmtRunner {
132201 dynamic_library_path : String ,
133202 binary_path : PathBuf ,
203+ edition : Edition ,
204+ style_edition : StyleEdition ,
134205}
135206
136207impl < 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> {
461536pub 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 ! (
0 commit comments