@@ -5,28 +5,59 @@ use std::sync::{Arc, Mutex};
55
66use termcolor:: { Color , WriteColor } ;
77
8+ #[ derive( Clone , Default ) ]
9+ ///CLI flags used by tidy.
10+ pub struct TidyFlags {
11+ ///Applies style and formatting changes during a tidy run.
12+ bless : bool ,
13+ }
14+
15+ impl TidyFlags {
16+ pub fn new ( cfg_args : & [ String ] ) -> Self {
17+ let mut flags = Self :: default ( ) ;
18+
19+ for arg in cfg_args {
20+ match arg. as_str ( ) {
21+ "--bless" => flags. bless = true ,
22+ _ => continue ,
23+ }
24+ }
25+ flags
26+ }
27+ }
28+
829/// Collects diagnostics from all tidy steps, and contains shared information
930/// that determines how should message and logs be presented.
1031///
1132/// Since checks are executed in parallel, the context is internally synchronized, to avoid
1233/// all checks to lock it explicitly.
1334#[ derive( Clone ) ]
14- pub struct DiagCtx ( Arc < Mutex < DiagCtxInner > > ) ;
35+ pub struct TidyCtx {
36+ tidy_flags : TidyFlags ,
37+ diag_ctx : Arc < Mutex < DiagCtxInner > > ,
38+ }
39+
40+ impl TidyCtx {
41+ pub fn new ( root_path : & Path , verbose : bool , tidy_flags : TidyFlags ) -> Self {
42+ Self {
43+ diag_ctx : Arc :: new ( Mutex :: new ( DiagCtxInner {
44+ running_checks : Default :: default ( ) ,
45+ finished_checks : Default :: default ( ) ,
46+ root_path : root_path. to_path_buf ( ) ,
47+ verbose,
48+ } ) ) ,
49+ tidy_flags,
50+ }
51+ }
1552
16- impl DiagCtx {
17- pub fn new ( root_path : & Path , verbose : bool ) -> Self {
18- Self ( Arc :: new ( Mutex :: new ( DiagCtxInner {
19- running_checks : Default :: default ( ) ,
20- finished_checks : Default :: default ( ) ,
21- root_path : root_path. to_path_buf ( ) ,
22- verbose,
23- } ) ) )
53+ pub fn is_bless_enabled ( & self ) -> bool {
54+ self . tidy_flags . bless
2455 }
2556
2657 pub fn start_check < Id : Into < CheckId > > ( & self , id : Id ) -> RunningCheck {
2758 let mut id = id. into ( ) ;
2859
29- let mut ctx = self . 0 . lock ( ) . unwrap ( ) ;
60+ let mut ctx = self . diag_ctx . lock ( ) . unwrap ( ) ;
3061
3162 // Shorten path for shorter diagnostics
3263 id. path = match id. path {
@@ -38,14 +69,14 @@ impl DiagCtx {
3869 RunningCheck {
3970 id,
4071 bad : false ,
41- ctx : self . 0 . clone ( ) ,
72+ ctx : self . diag_ctx . clone ( ) ,
4273 #[ cfg( test) ]
4374 errors : vec ! [ ] ,
4475 }
4576 }
4677
4778 pub fn into_failed_checks ( self ) -> Vec < FinishedCheck > {
48- let ctx = Arc :: into_inner ( self . 0 ) . unwrap ( ) . into_inner ( ) . unwrap ( ) ;
79+ let ctx = Arc :: into_inner ( self . diag_ctx ) . unwrap ( ) . into_inner ( ) . unwrap ( ) ;
4980 assert ! ( ctx. running_checks. is_empty( ) , "Some checks are still running" ) ;
5081 ctx. finished_checks . into_iter ( ) . filter ( |c| c. bad ) . collect ( )
5182 }
@@ -151,7 +182,7 @@ impl RunningCheck {
151182 /// Useful if you want to run some functions from tidy without configuring
152183 /// diagnostics.
153184 pub fn new_noop ( ) -> Self {
154- let ctx = DiagCtx :: new ( Path :: new ( "" ) , false ) ;
185+ let ctx = TidyCtx :: new ( Path :: new ( "" ) , false , TidyFlags :: default ( ) ) ;
155186 ctx. start_check ( "noop" )
156187 }
157188
0 commit comments