Skip to content

Commit 50c3ff0

Browse files
committed
[review] update override_value
1 parent 325b66e commit 50c3ff0

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

src/config/config_type.rs

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -161,28 +161,13 @@ macro_rules! create_config {
161161

162162
fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
163163
$(
164-
if let Some(val) = parsed.$i {
165-
let nightly = crate::is_nightly_channel!();
164+
if let Some(option_value) = parsed.$i {
166165
let option_stable = self.$i.3;
167-
let variant_stable = val.stable_variant();
168-
169-
match (nightly, option_stable, variant_stable) {
170-
// Stable with an unstable option
171-
(false, false, _) => {
172-
eprintln!("Warning: can't set `{} = {:?}`, unstable features are only \
173-
available in nightly channel.", stringify!($i), val);
174-
},
175-
// Stable with a stable option, but an unstable variant
176-
(false, true, false) => {
177-
eprintln!("Warning: can't set `{} = {:?}`, unstable variants are only \
178-
available in nightly channel.", stringify!($i), val);
179-
},
180-
// Nightly: everything allowed
181-
// Stable with stable option and variant: allowed
182-
(true, _, _) | (false, true, true) => {
183-
self.$i.1 = true;
184-
self.$i.2 = val;
185-
},
166+
if $crate::config::config_type::is_stable_option_and_value(
167+
stringify!($i), option_stable, &option_value
168+
) {
169+
self.$i.1 = true;
170+
self.$i.2 = option_value;
186171
}
187172
}
188173
)+
@@ -249,12 +234,18 @@ macro_rules! create_config {
249234
match key {
250235
$(
251236
stringify!($i) => {
252-
self.$i.1 = true;
253-
self.$i.2 = val.parse::<$ty>()
237+
let option_value = val.parse::<$ty>()
254238
.expect(&format!("Failed to parse override for {} (\"{}\") as a {}",
255239
stringify!($i),
256240
val,
257241
stringify!($ty)));
242+
let option_stable = self.$i.3;
243+
if $crate::config::config_type::is_stable_option_and_value(
244+
stringify!($i), option_stable, &option_value
245+
) {
246+
self.$i.1 = true;
247+
self.$i.2 = option_value;
248+
}
258249
}
259250
)+
260251
_ => panic!("Unknown config key in override: {}", key)
@@ -452,3 +443,38 @@ macro_rules! create_config {
452443
}
453444
)
454445
}
446+
447+
pub(crate) fn is_stable_option_and_value<T>(
448+
option_name: &str,
449+
option_stable: bool,
450+
option_value: &T,
451+
) -> bool
452+
where
453+
T: PartialEq + std::fmt::Debug + ConfigType,
454+
{
455+
let nightly = crate::is_nightly_channel!();
456+
let variant_stable = option_value.stable_variant();
457+
match (nightly, option_stable, variant_stable) {
458+
// Stable with an unstable option
459+
(false, false, _) => {
460+
eprintln!(
461+
"Warning: can't set `{} = {:?}`, unstable features are only \
462+
available in nightly channel.",
463+
option_name, option_value
464+
);
465+
false
466+
}
467+
// Stable with a stable option, but an unstable variant
468+
(false, true, false) => {
469+
eprintln!(
470+
"Warning: can't set `{} = {:?}`, unstable variants are only \
471+
available in nightly channel.",
472+
option_name, option_value
473+
);
474+
false
475+
}
476+
// Nightly: everything allowed
477+
// Stable with stable option and variant: allowed
478+
(true, _, _) | (false, true, true) => true,
479+
}
480+
}

0 commit comments

Comments
 (0)