@@ -52,6 +52,41 @@ pub fn handle_config(
5252 }
5353}
5454
55+ /// Stands in for the value of a key rumdl accepts that is neither set nor backed by a
56+ /// default it could show (an unset `Option`). Deliberately not valid TOML, so it can
57+ /// never be read back as a real setting.
58+ const UNSET_DISPLAY : & str = "<unset>" ;
59+
60+ fn print_config_value ( rule : & str , field : & str , value : & toml:: Value , source : ConfigSource ) {
61+ println ! (
62+ "{}.{} = {} [from {}]" ,
63+ rule,
64+ field,
65+ formatter:: format_toml_value( value) ,
66+ formatter:: format_provenance( source)
67+ ) ;
68+ }
69+
70+ /// Reports a key as accepted but carrying no value, which is a different answer from
71+ /// "unknown key" and must not be collapsed into one.
72+ fn print_unset_config_value ( rule : & str , field : & str ) {
73+ println ! ( "{rule}.{field} = {UNSET_DISPLAY} [no default]" ) ;
74+ }
75+
76+ /// The defaults a rule publishes for display, which is where a shown value comes from.
77+ /// Distinct from the validation schema in [`rumdl_config::RuleRegistry`]: that one
78+ /// carries sentinels for keys whose type cannot be checked, and a sentinel must never
79+ /// reach output.
80+ fn default_config_values_for_rule ( rule_name : & str ) -> toml:: map:: Map < String , toml:: Value > {
81+ rumdl_lib:: rules:: all_rules ( & rumdl_config:: Config :: default ( ) )
82+ . iter ( )
83+ . find_map ( |rule| match rule. default_config_section ( ) {
84+ Some ( ( name, toml:: Value :: Table ( table) ) ) if normalize_key ( & name) == rule_name => Some ( table) ,
85+ _ => None ,
86+ } )
87+ . unwrap_or_default ( )
88+ }
89+
5590fn handle_config_get ( key : & str , config_path : Option < & str > , no_config : bool , inline_overrides : & [ toml:: Table ] ) {
5691 // Load config once; both dot-key and bare-rule paths use the same sourced state.
5792 let mut sourced = match rumdl_config:: SourcedConfig :: load_with_discovery ( config_path, None , no_config) {
@@ -70,94 +105,49 @@ fn handle_config_get(key: &str, config_path: Option<&str>, no_config: bool, inli
70105
71106 // Handle GLOBAL keys
72107 if section_part. eq_ignore_ascii_case ( "global" ) {
73- let maybe_value_source: Option < ( toml:: Value , ConfigSource ) > = match normalized_field. as_str ( ) {
74- "enable" => Some ( (
75- toml:: Value :: Array (
76- final_config
77- . global
78- . enable
79- . iter ( )
80- . map ( |s| toml:: Value :: String ( s. clone ( ) ) )
81- . collect ( ) ,
82- ) ,
83- sourced. global . enable . source ,
84- ) ) ,
85- "disable" => Some ( (
86- toml:: Value :: Array (
87- final_config
88- . global
89- . disable
90- . iter ( )
91- . map ( |s| toml:: Value :: String ( s. clone ( ) ) )
92- . collect ( ) ,
93- ) ,
94- sourced. global . disable . source ,
95- ) ) ,
96- "exclude" => Some ( (
97- toml:: Value :: Array (
98- final_config
99- . global
100- . exclude
101- . iter ( )
102- . map ( |s| toml:: Value :: String ( s. clone ( ) ) )
103- . collect ( ) ,
104- ) ,
105- sourced. global . exclude . source ,
106- ) ) ,
107- "include" => Some ( (
108- toml:: Value :: Array (
109- final_config
110- . global
111- . include
112- . iter ( )
113- . map ( |s| toml:: Value :: String ( s. clone ( ) ) )
114- . collect ( ) ,
115- ) ,
116- sourced. global . include . source ,
117- ) ) ,
118- "respect-gitignore" => Some ( (
119- toml:: Value :: Boolean ( final_config. global . respect_gitignore ) ,
120- sourced. global . respect_gitignore . source ,
121- ) ) ,
122- "output-format" | "output_format" => {
123- if let Some ( ref output_format) = final_config. global . output_format {
124- Some ( (
125- toml:: Value :: String ( output_format. clone ( ) ) ,
126- sourced
127- . global
128- . output_format
129- . as_ref ( )
130- . map ( |v| v. source )
131- . unwrap_or ( ConfigSource :: Default ) ,
132- ) )
133- } else {
134- None
135- }
108+ match rumdl_config:: read_global_key ( & sourced. global , & normalized_field) {
109+ Some ( rumdl_config:: GlobalKeyValue :: Set ( value, source) ) => {
110+ print_config_value ( "global" , & normalized_field, & value, source) ;
111+ }
112+ Some ( rumdl_config:: GlobalKeyValue :: Unset ) => {
113+ print_unset_config_value ( "global" , & normalized_field) ;
114+ }
115+ None => {
116+ eprintln ! ( "Unknown global key: {field_part}" ) ;
117+ exit:: tool_error ( ) ;
136118 }
137- "flavor" => Some ( (
138- toml:: Value :: String ( final_config. global . flavor . to_string ( ) ) ,
139- sourced. global . flavor . source ,
140- ) ) ,
141- _ => None ,
142- } ;
143-
144- if let Some ( ( value, source) ) = maybe_value_source {
145- println ! (
146- "{} = {} [from {}]" ,
147- key,
148- formatter:: format_toml_value( & value) ,
149- formatter:: format_provenance( source)
150- ) ;
151- } else {
152- eprintln ! ( "Unknown global key: {field_part}" ) ;
153- exit:: tool_error ( ) ;
154119 }
155120 }
156121 // Handle RULE keys (MDxxx.field or alias.field)
157122 else {
158123 let normalized_rule_name = rumdl_config:: resolve_rule_name_alias ( section_part)
159124 . map ( |s| s. to_string ( ) )
160125 . unwrap_or_else ( || normalize_key ( section_part) ) ;
126+ let registry = rumdl_config:: default_registry ( ) ;
127+
128+ if !registry. rule_schemas . contains_key ( & normalized_rule_name) {
129+ eprintln ! ( "Unknown config key: {normalized_rule_name}.{normalized_field}" ) ;
130+ exit:: tool_error ( ) ;
131+ }
132+
133+ // `severity` is accepted for every rule but is parsed into its own field
134+ // rather than into `values`, so it needs its own lookup.
135+ if normalized_field == "severity" {
136+ match sourced
137+ . rules
138+ . get ( & normalized_rule_name)
139+ . and_then ( |sc| sc. severity . as_ref ( ) )
140+ {
141+ Some ( sourced_severity) => print_config_value (
142+ & normalized_rule_name,
143+ & normalized_field,
144+ & toml:: Value :: try_from ( sourced_severity. value ) . expect ( "Severity serializes to a TOML string" ) ,
145+ sourced_severity. source ,
146+ ) ,
147+ None => print_unset_config_value ( & normalized_rule_name, & normalized_field) ,
148+ }
149+ return ;
150+ }
161151
162152 // Try to get the value from the final config first
163153 let final_value: Option < & toml:: Value > = final_config
@@ -172,22 +162,30 @@ fn handle_config_get(key: &str, config_path: Option<&str>, no_config: bool, inli
172162 . and_then ( |sc| sc. values . get ( & normalized_field) )
173163 . map_or ( ConfigSource :: Default , |sv| sv. source ) ;
174164
175- println ! (
176- "{}.{} = {} [from {}]" ,
177- normalized_rule_name,
178- normalized_field,
179- formatter:: format_toml_value( value) ,
180- formatter:: format_provenance( provenance)
181- ) ;
182- } else {
183- let registry = rumdl_config:: default_registry ( ) ;
184- if let Some ( v) = registry. expected_value_for ( & normalized_rule_name, & normalized_field) {
185- let value_str = formatter:: format_toml_value ( v) ;
186- println ! ( "{normalized_rule_name}.{normalized_field} = {value_str} [from default]" ) ;
165+ print_config_value ( & normalized_rule_name, & normalized_field, value, provenance) ;
166+ return ;
167+ }
168+
169+ // `enabled` is accepted for every rule and belongs to no rule's schema,
170+ // so an unset one is reported as unset rather than as an unknown key.
171+ let Some ( canonical_key) = registry. canonical_config_key ( & normalized_rule_name, & normalized_field) else {
172+ if normalized_field == "enabled" {
173+ print_unset_config_value ( & normalized_rule_name, & normalized_field) ;
187174 return ;
188175 }
189176 eprintln ! ( "Unknown config key: {normalized_rule_name}.{normalized_field}" ) ;
190177 exit:: tool_error ( ) ;
178+ } ;
179+
180+ // The default comes from the rule's user-facing table, not from the
181+ // validation schema: a key whose schema entry is a sentinel can still have
182+ // a displayable default (a polymorphic key), and one that has none is unset
183+ // rather than unknown.
184+ match default_config_values_for_rule ( & normalized_rule_name) . get ( canonical_key) {
185+ Some ( value) => {
186+ print_config_value ( & normalized_rule_name, & normalized_field, value, ConfigSource :: Default )
187+ }
188+ None => print_unset_config_value ( & normalized_rule_name, & normalized_field) ,
191189 }
192190 }
193191 } else {
@@ -199,12 +197,37 @@ fn handle_config_get(key: &str, config_path: Option<&str>, no_config: bool, inli
199197
200198 if registry. rule_schemas . contains_key ( & normalized_rule_name) {
201199 let schema = & registry. rule_schemas [ & normalized_rule_name] ;
202- let mut fields: Vec < & String > = schema. keys ( ) . collect ( ) ;
203- fields. sort ( ) ;
200+ let defaults = default_config_values_for_rule ( & normalized_rule_name) ;
201+
202+ // Every key the rule accepts, so the listing is the answer to "what can I
203+ // set here?": the rule's own schema plus the two keys every rule takes.
204+ let mut fields: Vec < & str > = schema. keys ( ) . map ( String :: as_str) . collect ( ) ;
205+ fields. push ( "enabled" ) ;
206+ fields. push ( "severity" ) ;
207+ fields. sort_unstable ( ) ;
208+ fields. dedup ( ) ;
204209
205210 for field in fields {
206211 let normalized_field = normalize_key ( field) ;
207212
213+ if normalized_field == "severity" {
214+ match sourced
215+ . rules
216+ . get ( & normalized_rule_name)
217+ . and_then ( |sc| sc. severity . as_ref ( ) )
218+ {
219+ Some ( sourced_severity) => print_config_value (
220+ & normalized_rule_name,
221+ & normalized_field,
222+ & toml:: Value :: try_from ( sourced_severity. value )
223+ . expect ( "Severity serializes to a TOML string" ) ,
224+ sourced_severity. source ,
225+ ) ,
226+ None => print_unset_config_value ( & normalized_rule_name, & normalized_field) ,
227+ }
228+ continue ;
229+ }
230+
208231 let final_value = final_config
209232 . rules
210233 . get ( & normalized_rule_name)
@@ -217,20 +240,14 @@ fn handle_config_get(key: &str, config_path: Option<&str>, no_config: bool, inli
217240 . and_then ( |sc| sc. values . get ( & normalized_field) )
218241 . map_or ( rumdl_config:: ConfigSource :: Default , |sv| sv. source ) ;
219242 ( v, provenance)
220- } else if let Some ( v) = registry . expected_value_for ( & normalized_rule_name , & normalized_field ) {
243+ } else if let Some ( v) = defaults . get ( field ) {
221244 ( v, rumdl_config:: ConfigSource :: Default )
222245 } else {
223- // Nullable sentinel field — no displayable default, skip
246+ print_unset_config_value ( & normalized_rule_name , & normalized_field ) ;
224247 continue ;
225248 } ;
226249
227- println ! (
228- "{}.{} = {} [from {}]" ,
229- normalized_rule_name,
230- normalized_field,
231- formatter:: format_toml_value( value) ,
232- formatter:: format_provenance( source)
233- ) ;
250+ print_config_value ( & normalized_rule_name, & normalized_field, value, source) ;
234251 }
235252 } else {
236253 eprintln ! (
0 commit comments