condense some option parsings

This commit is contained in:
JasonLG1979 2021-12-13 16:40:26 -06:00
parent d29337c62d
commit 368bee1088

View file

@ -852,8 +852,9 @@ fn get_setup() -> Setup {
let control = mixer_default_config.control; let control = mixer_default_config.control;
let volume_range = opt_str(VOLUME_RANGE) let volume_range = opt_str(VOLUME_RANGE)
.map(|range| { .map(|range| match range.parse::<f64>() {
let on_error = || { Ok(value) if (VALID_VOLUME_RANGE).contains(&value) => value,
_ => {
let valid_values = &format!( let valid_values = &format!(
"{} - {}", "{} - {}",
VALID_VOLUME_RANGE.start(), VALID_VOLUME_RANGE.start(),
@ -876,19 +877,9 @@ fn get_setup() -> Setup {
valid_values, valid_values,
default_value, default_value,
); );
};
let range = range.parse::<f64>().unwrap_or_else(|_| {
on_error();
exit(1);
});
if !(VALID_VOLUME_RANGE).contains(&range) {
on_error();
exit(1); exit(1);
} }
range
}) })
.unwrap_or_else(|| match mixer_type.as_deref() { .unwrap_or_else(|| match mixer_type.as_deref() {
#[cfg(feature = "alsa-backend")] #[cfg(feature = "alsa-backend")]
@ -1041,23 +1032,14 @@ fn get_setup() -> Setup {
let zeroconf_port = if enable_discovery { let zeroconf_port = if enable_discovery {
opt_str(ZEROCONF_PORT) opt_str(ZEROCONF_PORT)
.map(|port| { .map(|port| match port.parse::<u16>() {
let on_error = || { Ok(value) if value != 0 => value,
_ => {
let valid_values = &format!("1 - {}", u16::MAX); let valid_values = &format!("1 - {}", u16::MAX);
invalid_error_msg(ZEROCONF_PORT, ZEROCONF_PORT_SHORT, &port, valid_values, ""); invalid_error_msg(ZEROCONF_PORT, ZEROCONF_PORT_SHORT, &port, valid_values, "");
};
let port = port.parse::<u16>().unwrap_or_else(|_| {
on_error();
exit(1);
});
if port == 0 {
on_error();
exit(1); exit(1);
} }
port
}) })
.unwrap_or(0) .unwrap_or(0)
} else { } else {
@ -1076,44 +1058,39 @@ fn get_setup() -> Setup {
let initial_volume = opt_str(INITIAL_VOLUME) let initial_volume = opt_str(INITIAL_VOLUME)
.map(|initial_volume| { .map(|initial_volume| {
let on_error = || { let volume = match initial_volume.parse::<u16>() {
let valid_values = &format!( Ok(value) if (VALID_INITIAL_VOLUME_RANGE).contains(&value) => value,
"{} - {}", _ => {
VALID_INITIAL_VOLUME_RANGE.start(), let valid_values = &format!(
VALID_INITIAL_VOLUME_RANGE.end() "{} - {}",
); VALID_INITIAL_VOLUME_RANGE.start(),
VALID_INITIAL_VOLUME_RANGE.end()
);
#[cfg(feature = "alsa-backend")] #[cfg(feature = "alsa-backend")]
let default_value = &format!( let default_value = &format!(
"{}, or the current value when the alsa mixer is used.", "{}, or the current value when the alsa mixer is used.",
connect_default_config.initial_volume.unwrap_or_default() connect_default_config.initial_volume.unwrap_or_default()
); );
#[cfg(not(feature = "alsa-backend"))] #[cfg(not(feature = "alsa-backend"))]
let default_value = &connect_default_config let default_value = &connect_default_config
.initial_volume .initial_volume
.unwrap_or_default() .unwrap_or_default()
.to_string(); .to_string();
invalid_error_msg( invalid_error_msg(
INITIAL_VOLUME, INITIAL_VOLUME,
INITIAL_VOLUME_SHORT, INITIAL_VOLUME_SHORT,
&initial_volume, &initial_volume,
valid_values, valid_values,
default_value, default_value,
); );
exit(1);
}
}; };
let volume = initial_volume.parse::<u16>().unwrap_or_else(|_| {
on_error();
exit(1);
});
if !(VALID_INITIAL_VOLUME_RANGE).contains(&volume) {
on_error();
exit(1);
}
(volume as f32 / 100.0 * VolumeCtrl::MAX_VOLUME as f32) as u16 (volume as f32 / 100.0 * VolumeCtrl::MAX_VOLUME as f32) as u16
}) })
.or_else(|| match mixer_type.as_deref() { .or_else(|| match mixer_type.as_deref() {
@ -1135,7 +1112,7 @@ fn get_setup() -> Setup {
gameconsole, castaudio, castvideo, \ gameconsole, castaudio, castvideo, \
automobile, smartwatch, chromebook, \ automobile, smartwatch, chromebook, \
carthing, homething", carthing, homething",
"speaker", DeviceType::default().into(),
); );
exit(1); exit(1);
@ -1181,23 +1158,14 @@ fn get_setup() -> Setup {
} }
}, },
), ),
ap_port: opt_str(AP_PORT).map(|port| { ap_port: opt_str(AP_PORT).map(|port| match port.parse::<u16>() {
let on_error = || { Ok(value) if value != 0 => value,
_ => {
let valid_values = &format!("1 - {}", u16::MAX); let valid_values = &format!("1 - {}", u16::MAX);
invalid_error_msg(AP_PORT, AP_PORT_SHORT, &port, valid_values, ""); invalid_error_msg(AP_PORT, AP_PORT_SHORT, &port, valid_values, "");
};
let port = port.parse::<u16>().unwrap_or_else(|_| {
on_error();
exit(1);
});
if port == 0 {
on_error();
exit(1); exit(1);
} }
port
}), }),
}; };
@ -1302,8 +1270,9 @@ fn get_setup() -> Setup {
.unwrap_or(player_default_config.normalisation_type); .unwrap_or(player_default_config.normalisation_type);
normalisation_pregain = opt_str(NORMALISATION_PREGAIN) normalisation_pregain = opt_str(NORMALISATION_PREGAIN)
.map(|pregain| { .map(|pregain| match pregain.parse::<f64>() {
let on_error = || { Ok(value) if (VALID_NORMALISATION_PREGAIN_RANGE).contains(&value) => value,
_ => {
let valid_values = &format!( let valid_values = &format!(
"{} - {}", "{} - {}",
VALID_NORMALISATION_PREGAIN_RANGE.start(), VALID_NORMALISATION_PREGAIN_RANGE.start(),
@ -1317,25 +1286,18 @@ fn get_setup() -> Setup {
valid_values, valid_values,
&player_default_config.normalisation_pregain.to_string(), &player_default_config.normalisation_pregain.to_string(),
); );
};
let pregain = pregain.parse::<f64>().unwrap_or_else(|_| {
on_error();
exit(1);
});
if !(VALID_NORMALISATION_PREGAIN_RANGE).contains(&pregain) {
on_error();
exit(1); exit(1);
} }
pregain
}) })
.unwrap_or(player_default_config.normalisation_pregain); .unwrap_or(player_default_config.normalisation_pregain);
normalisation_threshold = opt_str(NORMALISATION_THRESHOLD) normalisation_threshold = opt_str(NORMALISATION_THRESHOLD)
.map(|threshold| { .map(|threshold| match threshold.parse::<f64>() {
let on_error = || { Ok(value) if (VALID_NORMALISATION_THRESHOLD_RANGE).contains(&value) => {
db_to_ratio(value)
}
_ => {
let valid_values = &format!( let valid_values = &format!(
"{} - {}", "{} - {}",
VALID_NORMALISATION_THRESHOLD_RANGE.start(), VALID_NORMALISATION_THRESHOLD_RANGE.start(),
@ -1349,25 +1311,18 @@ fn get_setup() -> Setup {
valid_values, valid_values,
&ratio_to_db(player_default_config.normalisation_threshold).to_string(), &ratio_to_db(player_default_config.normalisation_threshold).to_string(),
); );
};
let threshold = threshold.parse::<f64>().unwrap_or_else(|_| {
on_error();
exit(1);
});
if !(VALID_NORMALISATION_THRESHOLD_RANGE).contains(&threshold) {
on_error();
exit(1); exit(1);
} }
db_to_ratio(threshold)
}) })
.unwrap_or(player_default_config.normalisation_threshold); .unwrap_or(player_default_config.normalisation_threshold);
normalisation_attack = opt_str(NORMALISATION_ATTACK) normalisation_attack = opt_str(NORMALISATION_ATTACK)
.map(|attack| { .map(|attack| match attack.parse::<u64>() {
let on_error = || { Ok(value) if (VALID_NORMALISATION_ATTACK_RANGE).contains(&value) => {
Duration::from_millis(value)
}
_ => {
let valid_values = &format!( let valid_values = &format!(
"{} - {}", "{} - {}",
VALID_NORMALISATION_ATTACK_RANGE.start(), VALID_NORMALISATION_ATTACK_RANGE.start(),
@ -1384,25 +1339,18 @@ fn get_setup() -> Setup {
.as_millis() .as_millis()
.to_string(), .to_string(),
); );
};
let attack = attack.parse::<u64>().unwrap_or_else(|_| {
on_error();
exit(1);
});
if !(VALID_NORMALISATION_ATTACK_RANGE).contains(&attack) {
on_error();
exit(1); exit(1);
} }
Duration::from_millis(attack)
}) })
.unwrap_or(player_default_config.normalisation_attack); .unwrap_or(player_default_config.normalisation_attack);
normalisation_release = opt_str(NORMALISATION_RELEASE) normalisation_release = opt_str(NORMALISATION_RELEASE)
.map(|release| { .map(|release| match release.parse::<u64>() {
let on_error = || { Ok(value) if (VALID_NORMALISATION_RELEASE_RANGE).contains(&value) => {
Duration::from_millis(value)
}
_ => {
let valid_values = &format!( let valid_values = &format!(
"{} - {}", "{} - {}",
VALID_NORMALISATION_RELEASE_RANGE.start(), VALID_NORMALISATION_RELEASE_RANGE.start(),
@ -1419,25 +1367,16 @@ fn get_setup() -> Setup {
.as_millis() .as_millis()
.to_string(), .to_string(),
); );
};
let release = release.parse::<u64>().unwrap_or_else(|_| {
on_error();
exit(1);
});
if !(VALID_NORMALISATION_RELEASE_RANGE).contains(&release) {
on_error();
exit(1); exit(1);
} }
Duration::from_millis(release)
}) })
.unwrap_or(player_default_config.normalisation_release); .unwrap_or(player_default_config.normalisation_release);
normalisation_knee = opt_str(NORMALISATION_KNEE) normalisation_knee = opt_str(NORMALISATION_KNEE)
.map(|knee| { .map(|knee| match knee.parse::<f64>() {
let on_error = || { Ok(value) if (VALID_NORMALISATION_KNEE_RANGE).contains(&value) => value,
_ => {
let valid_values = &format!( let valid_values = &format!(
"{} - {}", "{} - {}",
VALID_NORMALISATION_KNEE_RANGE.start(), VALID_NORMALISATION_KNEE_RANGE.start(),
@ -1451,47 +1390,35 @@ fn get_setup() -> Setup {
valid_values, valid_values,
&player_default_config.normalisation_knee.to_string(), &player_default_config.normalisation_knee.to_string(),
); );
};
let knee = knee.parse::<f64>().unwrap_or_else(|_| {
on_error();
exit(1);
});
if !(VALID_NORMALISATION_KNEE_RANGE).contains(&knee) {
on_error();
exit(1); exit(1);
} }
knee
}) })
.unwrap_or(player_default_config.normalisation_knee); .unwrap_or(player_default_config.normalisation_knee);
} }
let ditherer_name = opt_str(DITHER); let ditherer_name = opt_str(DITHER);
let ditherer = match ditherer_name.as_deref() { let ditherer = match ditherer_name.as_deref() {
// explicitly disabled on command line Some(value) => match value {
Some("none") => None, "none" => None,
// explicitly set on command line _ => match format {
Some(_) => { AudioFormat::F64 | AudioFormat::F32 => {
if matches!(format, AudioFormat::F64 | AudioFormat::F32) { error!("Dithering is not available with format: {:?}.", format);
error!("Dithering is not available with format: {:?}.", format); exit(1);
exit(1); }
} _ => Some(dither::find_ditherer(ditherer_name).unwrap_or_else(|| {
invalid_error_msg(
DITHER,
DITHER_SHORT,
&opt_str(DITHER).unwrap_or_default(),
"none, gpdf, tpdf, tpdf_hp for formats S16, S24, S24_3, S32, none for formats F32, F64",
"tpdf for formats S16, S24, S24_3 and none for formats S32, F32, F64",
);
Some(dither::find_ditherer(ditherer_name).unwrap_or_else(|| { exit(1);
invalid_error_msg( })),
DITHER, },
DITHER_SHORT, },
&opt_str(DITHER).unwrap_or_default(),
"none, gpdf, tpdf, tpdf_hp",
"tpdf for formats S16, S24, S24_3 and none for other formats",
);
exit(1);
}))
}
// nothing set on command line => use default
None => match format { None => match format {
AudioFormat::S16 | AudioFormat::S24 | AudioFormat::S24_3 => { AudioFormat::S16 | AudioFormat::S24 | AudioFormat::S24_3 => {
player_default_config.ditherer player_default_config.ditherer