Add ergonomics to mpd IP addr

This commit is contained in:
NGnius (Graham) 2022-05-15 12:35:42 -04:00
parent 3b756bf0ad
commit 86af7b4cf5
2 changed files with 37 additions and 0 deletions

View file

@ -62,6 +62,7 @@ impl Iterator for MpdQueryStatement {
)), )),
Err(e) => return Some(Err(e.with(RuntimeOp(PseudoOp::from_printable(self))))), Err(e) => return Some(Err(e.with(RuntimeOp(PseudoOp::from_printable(self))))),
}; };
#[cfg(not(feature = "ergonomics"))]
let addr: SocketAddr = match addr_str.parse() { let addr: SocketAddr = match addr_str.parse() {
Ok(a) => a, Ok(a) => a,
Err(e) => return Some(Err(RuntimeError { Err(e) => return Some(Err(RuntimeError {
@ -70,6 +71,30 @@ impl Iterator for MpdQueryStatement {
msg: format!("Cannot convert `{}` to IP Address: {}", addr_str, e), 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::<u16>() {
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 // build params
let mut new_params = Vec::<(&str, String)>::with_capacity(self.params.len()); let mut new_params = Vec::<(&str, String)>::with_capacity(self.params.len());
for (term, value) in self.params.iter() { for (term, value) in self.params.iter() {

View file

@ -842,6 +842,18 @@ fn execute_mpdfunction_line() -> Result<(), MpsError> {
true, true,
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( execute_single_line(
"mpd(`127.0.0.1:6600`)", "mpd(`127.0.0.1:6600`)",
false, false,