From 12487966b274aff7325a7cae6355f7692681ccf2 Mon Sep 17 00:00:00 2001 From: Michael Herger Date: Wed, 31 Jan 2018 06:45:48 +0100 Subject: [PATCH 1/3] Somewhat uniform coding style might help myself to better understand Rust :-) --- .gitignore | 2 ++ src/main.rs | 35 +++++++++++++---------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 50242a6a..1ca8ef72 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ target .cargo spotify_appkey.key .vagrant/ +.project +.history \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2ecfb57c..579f5bc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -135,30 +135,21 @@ fn setup(args: &[String]) -> Setup { let mixer = mixer::find(mixer_name.as_ref()) .expect("Invalid mixer"); - let initial_volume: i32; - if matches.opt_present("initial-volume") && matches.opt_str("initial-volume").unwrap().parse::().is_ok() { - let iv = matches.opt_str("initial-volume").unwrap().parse::().unwrap(); - match iv { - iv if iv >= 0 && iv <= 100 => { initial_volume = iv * 0xFFFF / 100 } - _ => { - debug!("Volume needs to be a value from 0-100; set volume level to 50%"); - initial_volume = 0x8000; + let initial_volume = matches + .opt_str("initial-volume") + .map(|volume| { + let volume = volume.parse::().unwrap(); + if volume < 0 || volume > 100 { + panic!("Initial volume must be in the range 0-100"); } - } - } else { - initial_volume = 0x8000; - } + volume * 0xFFFF / 100 + }) + .unwrap_or(0x8000); - let zeroconf_port: u16; - if matches.opt_present("zeroconf-port") && matches.opt_str("zeroconf-port").unwrap().parse::().is_ok() { - let z = matches.opt_str("zeroconf-port").unwrap().parse::().unwrap(); - match z { - z if z >= 1024 => { zeroconf_port = z } - _ => { zeroconf_port = 0 } - } - } else { - zeroconf_port = 0 - } + let zeroconf_port = + matches.opt_str("zeroconf-port") + .map(|port| port.parse::().unwrap()) + .unwrap_or(0); let name = matches.opt_str("name").unwrap(); let use_audio_cache = !matches.opt_present("disable-audio-cache"); From 5ed4639cca9c13035b72ec58a2f1a56f1571cf63 Mon Sep 17 00:00:00 2001 From: Michael Herger Date: Wed, 31 Jan 2018 16:15:20 +0100 Subject: [PATCH 2/3] Remove the -z as an alias for the --zeroconf-port parameter --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 579f5bc9..4c04ad1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,7 +101,7 @@ fn setup(args: &[String]) -> Setup { .optopt("", "device", "Audio device to use. Use '?' to list options", "DEVICE") .optopt("", "mixer", "Mixer to use", "MIXER") .optopt("", "initial-volume", "Initial volume in %, once connected (must be from 0 to 100)", "VOLUME") - .optopt("z", "zeroconf-port", "The port the internal server advertised over zeroconf uses.", "ZEROCONF_PORT"); + .optopt("", "zeroconf-port", "The port the internal server advertised over zeroconf uses.", "ZEROCONF_PORT"); let matches = match opts.parse(&args[1..]) { Ok(m) => m, From 4636cb71b96785bc44004c77ec0034e7eca3b140 Mon Sep 17 00:00:00 2001 From: Michael Herger Date: Fri, 9 Feb 2018 21:51:56 +0100 Subject: [PATCH 3/3] Print more descriptive error message when we fail to bind zeroconf to the given port. --- discovery/src/discovery.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discovery/src/discovery.rs b/discovery/src/discovery.rs index e29a798a..24bf0e57 100644 --- a/discovery/src/discovery.rs +++ b/discovery/src/discovery.rs @@ -215,7 +215,7 @@ pub fn discovery(handle: &Handle, config: ConnectConfig, device_id: String, port let serve = { let http = Http::new(); debug!("Zeroconf server listening on 0.0.0.0:{}", port); - http.serve_addr_handle(&format!("0.0.0.0:{}", port).parse().unwrap(), &handle, move || Ok(discovery.clone())).unwrap() + http.serve_addr_handle(&format!("0.0.0.0:{}", port).parse().unwrap(), &handle, move || Ok(discovery.clone())).expect("Unable to bind Zeroconf to port") }; let s_port = serve.incoming_ref().local_addr().port();