From 86af7b4cf593ea5da8cd457a69acbf9fcb9a0fa7 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sun, 15 May 2022 12:35:42 -0400 Subject: [PATCH] Add ergonomics to mpd IP addr --- .../src/lang/vocabulary/mpd_query.rs | 25 +++++++++++++++++++ mps-interpreter/tests/single_line.rs | 12 +++++++++ 2 files changed, 37 insertions(+) diff --git a/mps-interpreter/src/lang/vocabulary/mpd_query.rs b/mps-interpreter/src/lang/vocabulary/mpd_query.rs index 1c20e37..27b8c00 100644 --- a/mps-interpreter/src/lang/vocabulary/mpd_query.rs +++ b/mps-interpreter/src/lang/vocabulary/mpd_query.rs @@ -62,6 +62,7 @@ impl Iterator for MpdQueryStatement { )), Err(e) => return Some(Err(e.with(RuntimeOp(PseudoOp::from_printable(self))))), }; + #[cfg(not(feature = "ergonomics"))] let addr: SocketAddr = match addr_str.parse() { Ok(a) => a, Err(e) => return Some(Err(RuntimeError { @@ -70,6 +71,30 @@ impl Iterator for MpdQueryStatement { msg: format!("Cannot convert `{}` to IP Address: {}", addr_str, e), })) }; + #[cfg(feature = "ergonomics")] + let addr: SocketAddr = if addr_str.starts_with("localhost:") { + let port_str = addr_str.replace("localhost:", ""); + let port = match port_str.parse::() { + Ok(p) => p, + Err(e) => return Some(Err(RuntimeError { + line: 0, + op: PseudoOp::from_printable(self), + msg: format!("Cannot convert `{}` to IP port: {}", port_str, e), + })) + }; + SocketAddr::V4(std::net::SocketAddrV4::new(std::net::Ipv4Addr::LOCALHOST, port)) + } else if addr_str == "default" { + SocketAddr::V4(std::net::SocketAddrV4::new(std::net::Ipv4Addr::LOCALHOST, 6600)) + } else { + match addr_str.parse() { + Ok(a) => a, + Err(e) => return Some(Err(RuntimeError { + line: 0, + op: PseudoOp::from_printable(self), + msg: format!("Cannot convert `{}` to IP Address: {}", addr_str, e), + })) + } + }; // build params let mut new_params = Vec::<(&str, String)>::with_capacity(self.params.len()); for (term, value) in self.params.iter() { diff --git a/mps-interpreter/tests/single_line.rs b/mps-interpreter/tests/single_line.rs index c6370fd..8dedc2a 100644 --- a/mps-interpreter/tests/single_line.rs +++ b/mps-interpreter/tests/single_line.rs @@ -842,6 +842,18 @@ fn execute_mpdfunction_line() -> Result<(), MpsError> { true, true, )?; + #[cfg(feature = "ergonomics")] + execute_single_line( + "mpd(`localhost:6600`)", + false, + true, + )?; + #[cfg(feature = "ergonomics")] + execute_single_line( + "mpd(`default`)", + false, + true, + )?; execute_single_line( "mpd(`127.0.0.1:6600`)", false,