@@ -2,20 +2,18 @@ use std::env::home_dir;
22use std:: path:: Path ;
33use std:: path:: PathBuf ;
44
5- use analyzer:: AnalyzerConfiguration ;
65use config:: Config ;
7- use config:: ConfigBuilder ;
86use config:: Environment ;
97use config:: File ;
108use config:: FileFormat ;
119use config:: Value ;
1210use config:: ValueKind ;
13- use config:: builder:: BuilderState ;
1411use serde:: Deserialize ;
12+ use serde:: Serialize ;
1513
1614use mago_php_version:: PHPVersion ;
17- use serde:: Serialize ;
1815
16+ use crate :: config:: analyzer:: AnalyzerConfiguration ;
1917use crate :: config:: formatter:: FormatterConfiguration ;
2018use crate :: config:: linter:: LinterConfiguration ;
2119use crate :: config:: source:: SourceConfiguration ;
@@ -48,17 +46,14 @@ pub struct Configuration {
4846
4947 /// Configuration options for the linter.
5048 #[ serde( default ) ]
51- #[ serde( alias = "lint" ) ]
5249 pub linter : LinterConfiguration ,
5350
5451 /// Configuration options for the formatter.
5552 #[ serde( default ) ]
56- #[ serde( alias = "format" ) ]
5753 pub formatter : FormatterConfiguration ,
5854
5955 /// Configuration options for the analyzer.
6056 #[ serde( default ) ]
61- #[ serde( alias = "analyser" , alias = "analyze" , alias = "analyse" ) ]
6257 pub analyzer : AnalyzerConfiguration ,
6358
6459 /// The log filter.
@@ -68,7 +63,8 @@ pub struct Configuration {
6863 ///
6964 /// If this field is to be removed, serde will complain about an unknown field in the configuration
7065 /// when `MAGO_LOG` is set due to the `deny_unknown_fields` attribute and the use of `Environment` source.
71- #[ serde( skip_serializing) ]
66+ #[ serde( default , skip_serializing) ]
67+ #[ allow( dead_code) ]
7268 log : Value ,
7369}
7470
@@ -106,29 +102,38 @@ impl Configuration {
106102 allow_unsupported_php_version : bool ,
107103 ) -> Result < Configuration , Error > {
108104 let workspace_dir = workspace. clone ( ) . unwrap_or_else ( || CURRENT_DIR . to_path_buf ( ) ) ;
105+ let workspace_config_path = workspace_dir. join ( CONFIGURATION_FILE ) ;
106+
107+ let mut configuration = Configuration :: from_workspace ( workspace_dir) ;
108+ let mut builder = Config :: builder ( ) . add_source ( Config :: try_from ( & configuration) ?) ;
109109
110- let mut builder = Config :: builder ( ) ;
111110 if let Some ( file) = file {
111+ tracing:: debug!( "Sourcing configuration from {}." , file. display( ) ) ;
112+
112113 builder = builder. add_source ( File :: from ( file) . required ( true ) . format ( FileFormat :: Toml ) ) ;
113114 } else {
114- let global_config_roots =
115- [ std:: env:: var_os ( "XDG_CONFIG_HOME" ) . map ( PathBuf :: from) , home_dir ( ) ] . into_iter ( ) . flatten ( ) ;
116-
115+ let global_config_roots = [ std:: env:: var_os ( "XDG_CONFIG_HOME" ) . map ( PathBuf :: from) , home_dir ( ) ] ;
117116 for global_config_root in global_config_roots {
118- builder = builder. add_source (
119- File :: from ( global_config_root. join ( CONFIGURATION_FILE ) ) . required ( false ) . format ( FileFormat :: Toml ) ,
120- ) ;
121- }
117+ let Some ( global_config_root) = global_config_root else {
118+ continue ;
119+ } ;
122120
123- builder = builder
124- . add_source ( File :: from ( workspace_dir. join ( CONFIGURATION_FILE ) ) . required ( false ) . format ( FileFormat :: Toml ) )
125- }
121+ let global_config_path = global_config_root. join ( CONFIGURATION_FILE ) ;
126122
127- builder = builder . add_source ( Environment :: with_prefix ( ENVIRONMENT_PREFIX ) ) ;
123+ tracing :: debug! ( "Sourcing global configuration from {}." , global_config_path . display ( ) ) ;
128124
129- let mut configuration = Configuration :: from_workspace ( workspace_dir) ;
125+ builder = builder. add_source ( File :: from ( global_config_path) . required ( false ) . format ( FileFormat :: Toml ) ) ;
126+ }
130127
131- configuration = configuration. configure ( builder) ?. build ( ) ?. try_deserialize :: < Configuration > ( ) ?;
128+ tracing:: debug!( "Sourcing workspace configuration from {}." , workspace_config_path. display( ) ) ;
129+
130+ builder = builder. add_source ( File :: from ( workspace_config_path) . required ( false ) . format ( FileFormat :: Toml ) ) ;
131+ }
132+
133+ configuration = builder
134+ . add_source ( Environment :: with_prefix ( ENVIRONMENT_PREFIX ) )
135+ . build ( ) ?
136+ . try_deserialize :: < Configuration > ( ) ?;
132137
133138 if allow_unsupported_php_version && !configuration. allow_unsupported_php_version {
134139 tracing:: warn!( "Allowing unsupported PHP versions." ) ;
@@ -183,32 +188,7 @@ impl Configuration {
183188 }
184189}
185190
186- trait ConfigurationEntry {
187- /// Configures the builder with the entry.
188- fn configure < St : BuilderState > ( self , builder : ConfigBuilder < St > ) -> Result < ConfigBuilder < St > , Error > ;
189-
190- fn normalize ( & mut self ) -> Result < ( ) , Error > {
191- Ok ( ( ) )
192- }
193- }
194-
195- impl ConfigurationEntry for Configuration {
196- fn configure < St : BuilderState > ( self , builder : ConfigBuilder < St > ) -> Result < ConfigBuilder < St > , Error > {
197- let mut builder = builder
198- . set_default ( "threads" , Value :: new ( None , ValueKind :: U64 ( self . threads as u64 ) ) ) ?
199- . set_default ( "stack-size" , Value :: new ( None , ValueKind :: U64 ( self . stack_size as u64 ) ) ) ?
200- . set_default ( "php-version" , Value :: new ( None , ValueKind :: String ( self . php_version . to_string ( ) ) ) ) ?
201- . set_default ( "allow-unsupported-php-version" , self . allow_unsupported_php_version ) ?
202- . set_default ( "log" , self . log ) ?;
203-
204- builder = self . source . configure ( builder) ?;
205- builder = self . linter . configure ( builder) ?;
206- builder = self . formatter . configure ( builder) ?;
207- builder = self . analyzer . configure ( builder) ?;
208-
209- Ok ( builder)
210- }
211-
191+ impl Configuration {
212192 fn normalize ( & mut self ) -> Result < ( ) , Error > {
213193 match self . threads {
214194 0 => {
@@ -252,9 +232,6 @@ impl ConfigurationEntry for Configuration {
252232 }
253233
254234 self . source . normalize ( ) ?;
255- self . linter . normalize ( ) ?;
256- self . formatter . normalize ( ) ?;
257- self . analyzer . normalize ( ) ?;
258235
259236 Ok ( ( ) )
260237 }
0 commit comments