Skip to content

Commit 09eb6ff

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 84d2d6b commit 09eb6ff

File tree

4 files changed

+313
-149
lines changed

4 files changed

+313
-149
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"
@@ -152,12 +152,28 @@ macro_rules! create_config {
152152
impl Config {
153153
$(
154154
#[allow(unreachable_pub)]
155-
pub fn $i(&self) -> $ty {
155+
pub fn $i(&self) -> <$ty as StyleEditionDefault>::ConfigType {
156156
self.$i.0.set(true);
157157
self.$i.2.clone()
158158
}
159159
)+
160160

161+
#[allow(unreachable_pub)]
162+
pub fn deafult_with_style_edition(style_edition: StyleEdition) -> Config {
163+
Config {
164+
$(
165+
$i: (
166+
Cell::new(false),
167+
false,
168+
<$ty as StyleEditionDefault>::style_edition_default(
169+
style_edition
170+
),
171+
$stb
172+
),
173+
)+
174+
}
175+
}
176+
161177
#[allow(unreachable_pub)]
162178
pub fn set(&mut self) -> ConfigSetter<'_> {
163179
ConfigSetter(self)
@@ -210,7 +226,9 @@ macro_rules! create_config {
210226
pub fn is_valid_key_val(key: &str, val: &str) -> bool {
211227
match key {
212228
$(
213-
stringify!($i) => val.parse::<$ty>().is_ok(),
229+
stringify!($i) => {
230+
val.parse::<<$ty as StyleEditionDefault>::ConfigType>().is_ok()
231+
}
214232
)+
215233
_ => false,
216234
}
@@ -244,11 +262,15 @@ macro_rules! create_config {
244262
match key {
245263
$(
246264
stringify!($i) => {
247-
let option_value = val.parse::<$ty>()
248-
.expect(&format!("Failed to parse override for {} (\"{}\") as a {}",
249-
stringify!($i),
250-
val,
251-
stringify!($ty)));
265+
let value = val.parse::<<$ty as StyleEditionDefault>::ConfigType>()
266+
.expect(
267+
&format!(
268+
"Failed to parse override for {} (\"{}\") as a {}",
269+
stringify!($i),
270+
val,
271+
stringify!(<$ty as StyleEditionDefault>::ConfigType)
272+
)
273+
);
252274

253275
// Users are currently allowed to set unstable
254276
// options/variants via the `--config` options override.
@@ -259,7 +281,7 @@ macro_rules! create_config {
259281
// For now, do not validate whether the option or value is stable,
260282
// just always set it.
261283
self.$i.1 = true;
262-
self.$i.2 = option_value;
284+
self.$i.2 = value;
263285
}
264286
)+
265287
_ => panic!("Unknown config key in override: {}", key)
@@ -297,6 +319,7 @@ macro_rules! create_config {
297319

298320
#[allow(unreachable_pub)]
299321
pub fn print_docs(out: &mut dyn Write, include_unstable: bool) {
322+
let style_edition = StyleEdition::Edition2015;
300323
use std::cmp;
301324
let max = 0;
302325
$( let max = cmp::max(max, stringify!($i).len()+1); )+
@@ -313,14 +336,17 @@ macro_rules! create_config {
313336
}
314337
name_out.push_str(name_raw);
315338
name_out.push(' ');
316-
let mut default_str = format!("{}", $def);
339+
let default = <$ty as StyleEditionDefault>::style_edition_default(
340+
style_edition
341+
);
342+
let mut default_str = format!("{}", default);
317343
if default_str.is_empty() {
318344
default_str = String::from("\"\"");
319345
}
320346
writeln!(out,
321347
"{}{} Default: {}{}",
322348
name_out,
323-
<$ty>::doc_hint(),
349+
<<$ty as StyleEditionDefault>::ConfigType>::doc_hint(),
324350
default_str,
325351
if !$stb { " (unstable)" } else { "" }).unwrap();
326352
$(
@@ -464,9 +490,13 @@ macro_rules! create_config {
464490
#[allow(unreachable_pub)]
465491
/// Returns `true` if the config key was explicitly set and is the default value.
466492
pub fn is_default(&self, key: &str) -> bool {
493+
let style_edition = StyleEdition::Edition2015;
467494
$(
495+
let default = <$ty as StyleEditionDefault>::style_edition_default(
496+
style_edition
497+
);
468498
if let stringify!($i) = key {
469-
return self.$i.1 && self.$i.2 == $def;
499+
return self.$i.1 && self.$i.2 == default;
470500
}
471501
)+
472502
false
@@ -476,11 +506,7 @@ macro_rules! create_config {
476506
// Template for the default configuration
477507
impl Default for Config {
478508
fn default() -> Config {
479-
Config {
480-
$(
481-
$i: (Cell::new(false), false, $def, $stb),
482-
)+
483-
}
509+
Config::deafult_with_style_edition(StyleEdition::Edition2015)
484510
}
485511
}
486512
)

0 commit comments

Comments
 (0)