Skip to content

Commit 0e612d5

Browse files
committed
Implement StyleEditionDefault for each config option
Makes internal changes to define each configuration in terms of the `StyleEditionDefault` trait, and hard codes `StyleEdition::Edition2015` anywhere the new `style_edition` is needed. **Note** users are still unable to configure `style edition`.
1 parent 21afdb8 commit 0e612d5

File tree

4 files changed

+331
-158
lines changed

4 files changed

+331
-158
lines changed

src/config/config_type.rs

+47-21
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ macro_rules! create_config {
7070
//
7171
// - $i: the ident name of the option
7272
// - $ty: the type of the option value
73-
// - $def: the default value of the option
7473
// - $stb: true if the option is stable
7574
// - $dstring: description of the option
76-
($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => (
75+
($($i:ident: $ty:ty, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => (
7776
#[cfg(test)]
7877
use std::collections::HashSet;
7978
use std::io::Write;
8079

8180
use serde::{Deserialize, Serialize};
81+
use $crate::config::style_edition::StyleEditionDefault;
8282

8383
#[derive(Clone)]
8484
#[allow(unreachable_pub)]
@@ -89,7 +89,7 @@ macro_rules! create_config {
8989
// - 1: true if the option was manually initialized
9090
// - 2: the option value
9191
// - 3: true if the option is unstable
92-
$($i: (Cell<bool>, bool, $ty, bool)),+
92+
$($i: (Cell<bool>, bool, <$ty as StyleEditionDefault>::ConfigType, bool)),+
9393
}
9494

9595
// Just like the Config struct but with each property wrapped
@@ -100,7 +100,7 @@ macro_rules! create_config {
100100
#[derive(Deserialize, Serialize, Clone)]
101101
#[allow(unreachable_pub)]
102102
pub struct PartialConfig {
103-
$(pub $i: Option<$ty>),+
103+
$(pub $i: Option<<$ty as StyleEditionDefault>::ConfigType>),+
104104
}
105105

106106
// Macro hygiene won't allow us to make `set_$i()` methods on Config
@@ -114,7 +114,7 @@ macro_rules! create_config {
114114
impl<'a> ConfigSetter<'a> {
115115
$(
116116
#[allow(unreachable_pub)]
117-
pub fn $i(&mut self, value: $ty) {
117+
pub fn $i(&mut self, value: <$ty as StyleEditionDefault>::ConfigType) {
118118
(self.0).$i.2 = value;
119119
match stringify!($i) {
120120
"max_width"
@@ -153,12 +153,28 @@ macro_rules! create_config {
153153
impl Config {
154154
$(
155155
#[allow(unreachable_pub)]
156-
pub fn $i(&self) -> $ty {
156+
pub fn $i(&self) -> <$ty as StyleEditionDefault>::ConfigType {
157157
self.$i.0.set(true);
158158
self.$i.2.clone()
159159
}
160160
)+
161161

162+
#[allow(unreachable_pub)]
163+
pub fn default_with_style_edition(style_edition: StyleEdition) -> Config {
164+
Config {
165+
$(
166+
$i: (
167+
Cell::new(false),
168+
false,
169+
<$ty as StyleEditionDefault>::style_edition_default(
170+
style_edition
171+
),
172+
$stb
173+
),
174+
)+
175+
}
176+
}
177+
162178
#[allow(unreachable_pub)]
163179
pub fn set(&mut self) -> ConfigSetter<'_> {
164180
ConfigSetter(self)
@@ -212,7 +228,9 @@ macro_rules! create_config {
212228
pub fn is_valid_key_val(key: &str, val: &str) -> bool {
213229
match key {
214230
$(
215-
stringify!($i) => val.parse::<$ty>().is_ok(),
231+
stringify!($i) => {
232+
val.parse::<<$ty as StyleEditionDefault>::ConfigType>().is_ok()
233+
}
216234
)+
217235
_ => false,
218236
}
@@ -246,11 +264,15 @@ macro_rules! create_config {
246264
match key {
247265
$(
248266
stringify!($i) => {
249-
let option_value = val.parse::<$ty>()
250-
.expect(&format!("Failed to parse override for {} (\"{}\") as a {}",
251-
stringify!($i),
252-
val,
253-
stringify!($ty)));
267+
let value = val.parse::<<$ty as StyleEditionDefault>::ConfigType>()
268+
.expect(
269+
&format!(
270+
"Failed to parse override for {} (\"{}\") as a {}",
271+
stringify!($i),
272+
val,
273+
stringify!(<$ty as StyleEditionDefault>::ConfigType)
274+
)
275+
);
254276

255277
// Users are currently allowed to set unstable
256278
// options/variants via the `--config` options override.
@@ -261,7 +283,7 @@ macro_rules! create_config {
261283
// For now, do not validate whether the option or value is stable,
262284
// just always set it.
263285
self.$i.1 = true;
264-
self.$i.2 = option_value;
286+
self.$i.2 = value;
265287
}
266288
)+
267289
_ => panic!("Unknown config key in override: {}", key)
@@ -301,6 +323,7 @@ macro_rules! create_config {
301323

302324
#[allow(unreachable_pub)]
303325
pub fn print_docs(out: &mut dyn Write, include_unstable: bool) {
326+
let style_edition = StyleEdition::Edition2015;
304327
use std::cmp;
305328
let max = 0;
306329
$( let max = cmp::max(max, stringify!($i).len()+1); )+
@@ -317,14 +340,17 @@ macro_rules! create_config {
317340
}
318341
name_out.push_str(name_raw);
319342
name_out.push(' ');
320-
let mut default_str = format!("{}", $def);
343+
let default_value = <$ty as StyleEditionDefault>::style_edition_default(
344+
style_edition
345+
);
346+
let mut default_str = format!("{}", default_value);
321347
if default_str.is_empty() {
322348
default_str = String::from("\"\"");
323349
}
324350
writeln!(out,
325351
"{}{} Default: {}{}",
326352
name_out,
327-
<$ty>::doc_hint(),
353+
<<$ty as StyleEditionDefault>::ConfigType>::doc_hint(),
328354
default_str,
329355
if !$stb { " (unstable)" } else { "" }).unwrap();
330356
$(
@@ -480,9 +506,13 @@ macro_rules! create_config {
480506
#[allow(unreachable_pub)]
481507
/// Returns `true` if the config key was explicitly set and is the default value.
482508
pub fn is_default(&self, key: &str) -> bool {
509+
let style_edition = StyleEdition::Edition2015;
483510
$(
511+
let default_value = <$ty as StyleEditionDefault>::style_edition_default(
512+
style_edition
513+
);
484514
if let stringify!($i) = key {
485-
return self.$i.1 && self.$i.2 == $def;
515+
return self.$i.1 && self.$i.2 == default_value;
486516
}
487517
)+
488518
false
@@ -492,11 +522,7 @@ macro_rules! create_config {
492522
// Template for the default configuration
493523
impl Default for Config {
494524
fn default() -> Config {
495-
Config {
496-
$(
497-
$i: (Cell::new(false), false, $def, $stb),
498-
)+
499-
}
525+
Config::default_with_style_edition(StyleEdition::Edition2015)
500526
}
501527
}
502528
)

0 commit comments

Comments
 (0)