Skip to content

Commit 368bee1

Browse files
committed
condense some option parsings
1 parent d29337c commit 368bee1

1 file changed

Lines changed: 78 additions & 151 deletions

File tree

src/main.rs

Lines changed: 78 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,9 @@ fn get_setup() -> Setup {
852852
let control = mixer_default_config.control;
853853

854854
let volume_range = opt_str(VOLUME_RANGE)
855-
.map(|range| {
856-
let on_error = || {
855+
.map(|range| match range.parse::<f64>() {
856+
Ok(value) if (VALID_VOLUME_RANGE).contains(&value) => value,
857+
_ => {
857858
let valid_values = &format!(
858859
"{} - {}",
859860
VALID_VOLUME_RANGE.start(),
@@ -876,19 +877,9 @@ fn get_setup() -> Setup {
876877
valid_values,
877878
default_value,
878879
);
879-
};
880-
881-
let range = range.parse::<f64>().unwrap_or_else(|_| {
882-
on_error();
883-
exit(1);
884-
});
885880

886-
if !(VALID_VOLUME_RANGE).contains(&range) {
887-
on_error();
888881
exit(1);
889882
}
890-
891-
range
892883
})
893884
.unwrap_or_else(|| match mixer_type.as_deref() {
894885
#[cfg(feature = "alsa-backend")]
@@ -1041,23 +1032,14 @@ fn get_setup() -> Setup {
10411032

10421033
let zeroconf_port = if enable_discovery {
10431034
opt_str(ZEROCONF_PORT)
1044-
.map(|port| {
1045-
let on_error = || {
1035+
.map(|port| match port.parse::<u16>() {
1036+
Ok(value) if value != 0 => value,
1037+
_ => {
10461038
let valid_values = &format!("1 - {}", u16::MAX);
10471039
invalid_error_msg(ZEROCONF_PORT, ZEROCONF_PORT_SHORT, &port, valid_values, "");
1048-
};
10491040

1050-
let port = port.parse::<u16>().unwrap_or_else(|_| {
1051-
on_error();
1052-
exit(1);
1053-
});
1054-
1055-
if port == 0 {
1056-
on_error();
10571041
exit(1);
10581042
}
1059-
1060-
port
10611043
})
10621044
.unwrap_or(0)
10631045
} else {
@@ -1076,43 +1058,38 @@ fn get_setup() -> Setup {
10761058

10771059
let initial_volume = opt_str(INITIAL_VOLUME)
10781060
.map(|initial_volume| {
1079-
let on_error = || {
1080-
let valid_values = &format!(
1081-
"{} - {}",
1082-
VALID_INITIAL_VOLUME_RANGE.start(),
1083-
VALID_INITIAL_VOLUME_RANGE.end()
1084-
);
1061+
let volume = match initial_volume.parse::<u16>() {
1062+
Ok(value) if (VALID_INITIAL_VOLUME_RANGE).contains(&value) => value,
1063+
_ => {
1064+
let valid_values = &format!(
1065+
"{} - {}",
1066+
VALID_INITIAL_VOLUME_RANGE.start(),
1067+
VALID_INITIAL_VOLUME_RANGE.end()
1068+
);
10851069

1086-
#[cfg(feature = "alsa-backend")]
1087-
let default_value = &format!(
1088-
"{}, or the current value when the alsa mixer is used.",
1089-
connect_default_config.initial_volume.unwrap_or_default()
1090-
);
1070+
#[cfg(feature = "alsa-backend")]
1071+
let default_value = &format!(
1072+
"{}, or the current value when the alsa mixer is used.",
1073+
connect_default_config.initial_volume.unwrap_or_default()
1074+
);
10911075

1092-
#[cfg(not(feature = "alsa-backend"))]
1093-
let default_value = &connect_default_config
1094-
.initial_volume
1095-
.unwrap_or_default()
1096-
.to_string();
1076+
#[cfg(not(feature = "alsa-backend"))]
1077+
let default_value = &connect_default_config
1078+
.initial_volume
1079+
.unwrap_or_default()
1080+
.to_string();
10971081

1098-
invalid_error_msg(
1099-
INITIAL_VOLUME,
1100-
INITIAL_VOLUME_SHORT,
1101-
&initial_volume,
1102-
valid_values,
1103-
default_value,
1104-
);
1105-
};
1106-
1107-
let volume = initial_volume.parse::<u16>().unwrap_or_else(|_| {
1108-
on_error();
1109-
exit(1);
1110-
});
1082+
invalid_error_msg(
1083+
INITIAL_VOLUME,
1084+
INITIAL_VOLUME_SHORT,
1085+
&initial_volume,
1086+
valid_values,
1087+
default_value,
1088+
);
11111089

1112-
if !(VALID_INITIAL_VOLUME_RANGE).contains(&volume) {
1113-
on_error();
1114-
exit(1);
1115-
}
1090+
exit(1);
1091+
}
1092+
};
11161093

11171094
(volume as f32 / 100.0 * VolumeCtrl::MAX_VOLUME as f32) as u16
11181095
})
@@ -1135,7 +1112,7 @@ fn get_setup() -> Setup {
11351112
gameconsole, castaudio, castvideo, \
11361113
automobile, smartwatch, chromebook, \
11371114
carthing, homething",
1138-
"speaker",
1115+
DeviceType::default().into(),
11391116
);
11401117

11411118
exit(1);
@@ -1181,23 +1158,14 @@ fn get_setup() -> Setup {
11811158
}
11821159
},
11831160
),
1184-
ap_port: opt_str(AP_PORT).map(|port| {
1185-
let on_error = || {
1161+
ap_port: opt_str(AP_PORT).map(|port| match port.parse::<u16>() {
1162+
Ok(value) if value != 0 => value,
1163+
_ => {
11861164
let valid_values = &format!("1 - {}", u16::MAX);
11871165
invalid_error_msg(AP_PORT, AP_PORT_SHORT, &port, valid_values, "");
1188-
};
1189-
1190-
let port = port.parse::<u16>().unwrap_or_else(|_| {
1191-
on_error();
1192-
exit(1);
1193-
});
11941166

1195-
if port == 0 {
1196-
on_error();
11971167
exit(1);
11981168
}
1199-
1200-
port
12011169
}),
12021170
};
12031171

@@ -1302,8 +1270,9 @@ fn get_setup() -> Setup {
13021270
.unwrap_or(player_default_config.normalisation_type);
13031271

13041272
normalisation_pregain = opt_str(NORMALISATION_PREGAIN)
1305-
.map(|pregain| {
1306-
let on_error = || {
1273+
.map(|pregain| match pregain.parse::<f64>() {
1274+
Ok(value) if (VALID_NORMALISATION_PREGAIN_RANGE).contains(&value) => value,
1275+
_ => {
13071276
let valid_values = &format!(
13081277
"{} - {}",
13091278
VALID_NORMALISATION_PREGAIN_RANGE.start(),
@@ -1317,25 +1286,18 @@ fn get_setup() -> Setup {
13171286
valid_values,
13181287
&player_default_config.normalisation_pregain.to_string(),
13191288
);
1320-
};
1321-
1322-
let pregain = pregain.parse::<f64>().unwrap_or_else(|_| {
1323-
on_error();
1324-
exit(1);
1325-
});
13261289

1327-
if !(VALID_NORMALISATION_PREGAIN_RANGE).contains(&pregain) {
1328-
on_error();
13291290
exit(1);
13301291
}
1331-
1332-
pregain
13331292
})
13341293
.unwrap_or(player_default_config.normalisation_pregain);
13351294

13361295
normalisation_threshold = opt_str(NORMALISATION_THRESHOLD)
1337-
.map(|threshold| {
1338-
let on_error = || {
1296+
.map(|threshold| match threshold.parse::<f64>() {
1297+
Ok(value) if (VALID_NORMALISATION_THRESHOLD_RANGE).contains(&value) => {
1298+
db_to_ratio(value)
1299+
}
1300+
_ => {
13391301
let valid_values = &format!(
13401302
"{} - {}",
13411303
VALID_NORMALISATION_THRESHOLD_RANGE.start(),
@@ -1349,25 +1311,18 @@ fn get_setup() -> Setup {
13491311
valid_values,
13501312
&ratio_to_db(player_default_config.normalisation_threshold).to_string(),
13511313
);
1352-
};
13531314

1354-
let threshold = threshold.parse::<f64>().unwrap_or_else(|_| {
1355-
on_error();
1356-
exit(1);
1357-
});
1358-
1359-
if !(VALID_NORMALISATION_THRESHOLD_RANGE).contains(&threshold) {
1360-
on_error();
13611315
exit(1);
13621316
}
1363-
1364-
db_to_ratio(threshold)
13651317
})
13661318
.unwrap_or(player_default_config.normalisation_threshold);
13671319

13681320
normalisation_attack = opt_str(NORMALISATION_ATTACK)
1369-
.map(|attack| {
1370-
let on_error = || {
1321+
.map(|attack| match attack.parse::<u64>() {
1322+
Ok(value) if (VALID_NORMALISATION_ATTACK_RANGE).contains(&value) => {
1323+
Duration::from_millis(value)
1324+
}
1325+
_ => {
13711326
let valid_values = &format!(
13721327
"{} - {}",
13731328
VALID_NORMALISATION_ATTACK_RANGE.start(),
@@ -1384,25 +1339,18 @@ fn get_setup() -> Setup {
13841339
.as_millis()
13851340
.to_string(),
13861341
);
1387-
};
1388-
1389-
let attack = attack.parse::<u64>().unwrap_or_else(|_| {
1390-
on_error();
1391-
exit(1);
1392-
});
13931342

1394-
if !(VALID_NORMALISATION_ATTACK_RANGE).contains(&attack) {
1395-
on_error();
13961343
exit(1);
13971344
}
1398-
1399-
Duration::from_millis(attack)
14001345
})
14011346
.unwrap_or(player_default_config.normalisation_attack);
14021347

14031348
normalisation_release = opt_str(NORMALISATION_RELEASE)
1404-
.map(|release| {
1405-
let on_error = || {
1349+
.map(|release| match release.parse::<u64>() {
1350+
Ok(value) if (VALID_NORMALISATION_RELEASE_RANGE).contains(&value) => {
1351+
Duration::from_millis(value)
1352+
}
1353+
_ => {
14061354
let valid_values = &format!(
14071355
"{} - {}",
14081356
VALID_NORMALISATION_RELEASE_RANGE.start(),
@@ -1419,25 +1367,16 @@ fn get_setup() -> Setup {
14191367
.as_millis()
14201368
.to_string(),
14211369
);
1422-
};
1423-
1424-
let release = release.parse::<u64>().unwrap_or_else(|_| {
1425-
on_error();
1426-
exit(1);
1427-
});
14281370

1429-
if !(VALID_NORMALISATION_RELEASE_RANGE).contains(&release) {
1430-
on_error();
14311371
exit(1);
14321372
}
1433-
1434-
Duration::from_millis(release)
14351373
})
14361374
.unwrap_or(player_default_config.normalisation_release);
14371375

14381376
normalisation_knee = opt_str(NORMALISATION_KNEE)
1439-
.map(|knee| {
1440-
let on_error = || {
1377+
.map(|knee| match knee.parse::<f64>() {
1378+
Ok(value) if (VALID_NORMALISATION_KNEE_RANGE).contains(&value) => value,
1379+
_ => {
14411380
let valid_values = &format!(
14421381
"{} - {}",
14431382
VALID_NORMALISATION_KNEE_RANGE.start(),
@@ -1451,47 +1390,35 @@ fn get_setup() -> Setup {
14511390
valid_values,
14521391
&player_default_config.normalisation_knee.to_string(),
14531392
);
1454-
};
1455-
1456-
let knee = knee.parse::<f64>().unwrap_or_else(|_| {
1457-
on_error();
1458-
exit(1);
1459-
});
14601393

1461-
if !(VALID_NORMALISATION_KNEE_RANGE).contains(&knee) {
1462-
on_error();
14631394
exit(1);
14641395
}
1465-
1466-
knee
14671396
})
14681397
.unwrap_or(player_default_config.normalisation_knee);
14691398
}
14701399

14711400
let ditherer_name = opt_str(DITHER);
14721401
let ditherer = match ditherer_name.as_deref() {
1473-
// explicitly disabled on command line
1474-
Some("none") => None,
1475-
// explicitly set on command line
1476-
Some(_) => {
1477-
if matches!(format, AudioFormat::F64 | AudioFormat::F32) {
1478-
error!("Dithering is not available with format: {:?}.", format);
1479-
exit(1);
1480-
}
1481-
1482-
Some(dither::find_ditherer(ditherer_name).unwrap_or_else(|| {
1483-
invalid_error_msg(
1484-
DITHER,
1485-
DITHER_SHORT,
1486-
&opt_str(DITHER).unwrap_or_default(),
1487-
"none, gpdf, tpdf, tpdf_hp",
1488-
"tpdf for formats S16, S24, S24_3 and none for other formats",
1489-
);
1402+
Some(value) => match value {
1403+
"none" => None,
1404+
_ => match format {
1405+
AudioFormat::F64 | AudioFormat::F32 => {
1406+
error!("Dithering is not available with format: {:?}.", format);
1407+
exit(1);
1408+
}
1409+
_ => Some(dither::find_ditherer(ditherer_name).unwrap_or_else(|| {
1410+
invalid_error_msg(
1411+
DITHER,
1412+
DITHER_SHORT,
1413+
&opt_str(DITHER).unwrap_or_default(),
1414+
"none, gpdf, tpdf, tpdf_hp for formats S16, S24, S24_3, S32, none for formats F32, F64",
1415+
"tpdf for formats S16, S24, S24_3 and none for formats S32, F32, F64",
1416+
);
14901417

1491-
exit(1);
1492-
}))
1493-
}
1494-
// nothing set on command line => use default
1418+
exit(1);
1419+
})),
1420+
},
1421+
},
14951422
None => match format {
14961423
AudioFormat::S16 | AudioFormat::S24 | AudioFormat::S24_3 => {
14971424
player_default_config.ditherer

0 commit comments

Comments
 (0)