From e7bdf470594cfde88edf3dfc68faf68bb45abd06 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 8 Nov 2019 12:30:02 +0100 Subject: [PATCH] Replace try!() with ? operator As of 1.39, the ``try!'' macro is deprecated: https://github.com/rust-lang/rust/pull/62672 This changeset contains purely mechanical substitutions, no change in functionality. --- src/client.rs | 6 ++--- src/error.rs | 2 +- src/macros.rs | 10 ++++----- src/message.rs | 4 ++-- src/mount.rs | 8 +++---- src/output.rs | 2 +- src/playlist.rs | 6 ++--- src/plugin.rs | 2 +- src/proto.rs | 6 ++--- src/song.rs | 20 ++++++++--------- src/stats.rs | 16 +++++++------- src/status.rs | 59 +++++++++++++++++++++++-------------------------- 12 files changed, 69 insertions(+), 72 deletions(-) diff --git a/src/client.rs b/src/client.rs index 9905eac4..8a733e2e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -58,13 +58,13 @@ impl Client { let mut socket = BufStream::new(socket); let mut banner = String::new(); - try!(socket.read_line(&mut banner)); + socket.read_line(&mut banner)?; if !banner.starts_with("OK MPD ") { return Err(From::from(ProtoError::BadBanner)); } - let version = try!(banner[7..].trim().parse::()); + let version = banner[7..].trim().parse::()?; Ok(Client { socket: socket, @@ -610,7 +610,7 @@ impl Proto for Client { fn read_line(&mut self) -> Result { let mut buf = String::new(); - try!(self.socket.read_line(&mut buf)); + self.socket.read_line(&mut buf)?; if buf.ends_with('\n') { buf.pop(); } diff --git a/src/error.rs b/src/error.rs index 65361982..3a06189c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -56,7 +56,7 @@ impl FromStr for ErrorCode { type Err = ParseError; fn from_str(s: &str) -> result::Result { use self::ErrorCode::*; - match try!(s.parse()) { + match s.parse()? { 1 => Ok(NotList), 2 => Ok(Argument), 3 => Ok(Password), diff --git a/src/macros.rs b/src/macros.rs index 0f8ae582..76690db2 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -2,15 +2,15 @@ macro_rules! get_field_impl { ($op:ident, $map:expr, bool $name:expr) => { - try!($map.$op($name).ok_or(Error::Proto(ProtoError::NoField($name))) - .map(|v| v == "1")) + $map.$op($name).ok_or(Error::Proto(ProtoError::NoField($name))) + .map(|v| v == "1")? }; ($op:ident, $map:expr, opt $name:expr) => { - try!($map.$op($name).map(|v| v.parse().map(Some)).unwrap_or(Ok(None))) + $map.$op($name).map(|v| v.parse().map(Some)).unwrap_or(Ok(None))? }; ($op:ident, $map:expr, $name:expr) => { - try!($map.$op($name).ok_or(Error::Proto(ProtoError::NoField($name))) - .and_then(|v| v.parse().map_err(|e| Error::Parse(From::from(e))))) + $map.$op($name).ok_or(Error::Proto(ProtoError::NoField($name))) + .and_then(|v| v.parse().map_err(|e| Error::Parse(From::from(e))))? }; } diff --git a/src/message.rs b/src/message.rs index f8205497..49151861 100644 --- a/src/message.rs +++ b/src/message.rs @@ -27,8 +27,8 @@ pub struct Message { impl FromMap for Message { fn from_map(map: BTreeMap) -> Result { Ok(Message { - channel: Channel(try!(map.get("channel").map(|v| v.to_owned()).ok_or(Error::Proto(ProtoError::NoField("channel"))))), - message: try!(map.get("message").map(|v| v.to_owned()).ok_or(Error::Proto(ProtoError::NoField("message")))), + channel: Channel(map.get("channel").map(|v| v.to_owned()).ok_or(Error::Proto(ProtoError::NoField("channel")))?), + message: map.get("message").map(|v| v.to_owned()).ok_or(Error::Proto(ProtoError::NoField("message")))?, }) } } diff --git a/src/mount.rs b/src/mount.rs index 2c2a2a4d..34931ab5 100644 --- a/src/mount.rs +++ b/src/mount.rs @@ -26,8 +26,8 @@ pub struct Mount { impl FromMap for Mount { fn from_map(map: BTreeMap) -> Result { Ok(Mount { - name: try!(map.get("mount").map(|s| s.to_owned()).ok_or(Error::Proto(ProtoError::NoField("mount")))), - storage: try!(map.get("storage").map(|s| s.to_owned()).ok_or(Error::Proto(ProtoError::NoField("storage")))), + name: map.get("mount").map(|s| s.to_owned()).ok_or(Error::Proto(ProtoError::NoField("mount")))?, + storage: map.get("storage").map(|s| s.to_owned()).ok_or(Error::Proto(ProtoError::NoField("storage")))?, }) } } @@ -44,8 +44,8 @@ pub struct Neighbor { impl FromMap for Neighbor { fn from_map(map: BTreeMap) -> Result { Ok(Neighbor { - name: try!(map.get("name").map(|s| s.to_owned()).ok_or(Error::Proto(ProtoError::NoField("name")))), - storage: try!(map.get("neighbor").map(|s| s.to_owned()).ok_or(Error::Proto(ProtoError::NoField("neighbor")))), + name: map.get("name").map(|s| s.to_owned()).ok_or(Error::Proto(ProtoError::NoField("name")))?, + storage: map.get("neighbor").map(|s| s.to_owned()).ok_or(Error::Proto(ProtoError::NoField("neighbor")))?, }) } } diff --git a/src/output.rs b/src/output.rs index c210609f..277f114f 100644 --- a/src/output.rs +++ b/src/output.rs @@ -21,7 +21,7 @@ impl FromMap for Output { fn from_map(map: BTreeMap) -> Result { Ok(Output { id: get_field!(map, "outputid"), - name: try!(map.get("outputname").map(|v| v.to_owned()).ok_or(Error::Proto(ProtoError::NoField("outputname")))), + name: map.get("outputname").map(|v| v.to_owned()).ok_or(Error::Proto(ProtoError::NoField("outputname")))?, enabled: get_field!(map, bool "outputenabled"), }) } diff --git a/src/playlist.rs b/src/playlist.rs index c43d9be2..cf691ab2 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -18,10 +18,10 @@ pub struct Playlist { impl FromMap for Playlist { fn from_map(map: BTreeMap) -> Result { Ok(Playlist { - name: try!(map.get("playlist").map(|v| v.to_owned()).ok_or(Error::Proto(ProtoError::NoField("playlist")))), - last_mod: try!(map.get("Last-Modified") + name: map.get("playlist").map(|v| v.to_owned()).ok_or(Error::Proto(ProtoError::NoField("playlist")))?, + last_mod: map.get("Last-Modified") .ok_or(Error::Proto(ProtoError::NoField("Last-Modified"))) - .and_then(|v| strptime(&*v, "%Y-%m-%dT%H:%M:%S%Z").map_err(From::from))), + .and_then(|v| strptime(&*v, "%Y-%m-%dT%H:%M:%S%Z").map_err(From::from))?, }) } } diff --git a/src/plugin.rs b/src/plugin.rs index 3f9bc92d..8c7df36e 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -19,7 +19,7 @@ impl FromIter for Vec { let mut result = Vec::new(); let mut plugin: Option = None; for reply in iter { - let (a, b) = try!(reply); + let (a, b) = reply?; match &*a { "plugin" => { plugin.map(|p| result.push(p)); diff --git a/src/proto.rs b/src/proto.rs index 1d06bc47..273e8fa2 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -123,7 +123,7 @@ pub trait Proto { fn drain(&mut self) -> Result<()> { loop { - let reply = try!(self.read_line()); + let reply = self.read_line()?; match &*reply { "OK" | "list_OK" => break, _ => (), @@ -133,7 +133,7 @@ pub trait Proto { } fn expect_ok(&mut self) -> Result<()> { - let line = try!(self.read_line()); + let line = self.read_line()?; match line.parse::() { Ok(Reply::Ok) => Ok(()), @@ -144,7 +144,7 @@ pub trait Proto { } fn read_pair(&mut self) -> Result<(String, String)> { - let line = try!(self.read_line()); + let line = self.read_line()?; match line.parse::() { Ok(Reply::Pair(a, b)) => Ok((a, b)), diff --git a/src/song.rs b/src/song.rs index 75a25482..fecd2ded 100644 --- a/src/song.rs +++ b/src/song.rs @@ -147,49 +147,49 @@ impl FromIter for Song { let mut result = Song::default(); for res in iter { - let line = try!(res); + let line = res?; match &*line.0 { "file" => result.file = line.1.to_owned(), "Title" => result.title = Some(line.1.to_owned()), - "Last-Modified" => result.last_mod = try!(strptime(&*line.1, "%Y-%m-%dT%H:%M:%S%Z").map_err(ParseError::BadTime).map(Some)), + "Last-Modified" => result.last_mod = strptime(&*line.1, "%Y-%m-%dT%H:%M:%S%Z").map_err(ParseError::BadTime).map(Some)?, "Artist" => result.artist = Some(line.1.to_owned()), "Name" => result.name = Some(line.1.to_owned()), - "Time" => result.duration = Some(Duration::seconds(try!(line.1.parse()))), - "Range" => result.range = Some(try!(line.1.parse())), + "Time" => result.duration = Some(Duration::seconds(line.1.parse()?)), + "Range" => result.range = Some(line.1.parse()?), "Id" => { match result.place { None => { result.place = Some(QueuePlace { - id: Id(try!(line.1.parse())), + id: Id(line.1.parse()?), pos: 0, prio: 0, }) } - Some(ref mut place) => place.id = Id(try!(line.1.parse())), + Some(ref mut place) => place.id = Id(line.1.parse()?), } } "Pos" => { match result.place { None => { result.place = Some(QueuePlace { - pos: try!(line.1.parse()), + pos: line.1.parse()?, id: Id(0), prio: 0, }) } - Some(ref mut place) => place.pos = try!(line.1.parse()), + Some(ref mut place) => place.pos = line.1.parse()?, } } "Prio" => { match result.place { None => { result.place = Some(QueuePlace { - prio: try!(line.1.parse()), + prio: line.1.parse()?, id: Id(0), pos: 0, }) } - Some(ref mut place) => place.prio = try!(line.1.parse()), + Some(ref mut place) => place.prio = line.1.parse()?, } } _ => { diff --git a/src/stats.rs b/src/stats.rs index d41773dd..085b8eb9 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -60,15 +60,15 @@ impl FromIter for Stats { let mut result = Stats::default(); for res in iter { - let line = try!(res); + let line = res?; match &*line.0 { - "artists" => result.artists = try!(line.1.parse()), - "albums" => result.albums = try!(line.1.parse()), - "songs" => result.songs = try!(line.1.parse()), - "uptime" => result.uptime = Duration::seconds(try!(line.1.parse())), - "playtime" => result.playtime = Duration::seconds(try!(line.1.parse())), - "db_playtime" => result.db_playtime = Duration::seconds(try!(line.1.parse())), - "db_update" => result.db_update = Timespec::new(try!(line.1.parse()), 0), + "artists" => result.artists = line.1.parse()?, + "albums" => result.albums = line.1.parse()?, + "songs" => result.songs = line.1.parse()?, + "uptime" => result.uptime = Duration::seconds(line.1.parse()?), + "playtime" => result.playtime = Duration::seconds(line.1.parse()?), + "db_playtime" => result.db_playtime = Duration::seconds(line.1.parse()?), + "db_update" => result.db_update = Timespec::new(line.1.parse()?, 0), _ => (), } } diff --git a/src/status.rs b/src/status.rs index c4ec51fa..2ad9c231 100644 --- a/src/status.rs +++ b/src/status.rs @@ -125,88 +125,86 @@ impl FromIter for Status { let mut result = Status::default(); for res in iter { - let line = try!(res); + let line = res?; match &*line.0 { - "volume" => result.volume = try!(line.1.parse()), + "volume" => result.volume = line.1.parse()?, "repeat" => result.repeat = &*line.1 == "1", "random" => result.random = &*line.1 == "1", "single" => result.single = &*line.1 == "1", "consume" => result.consume = &*line.1 == "1", - "playlist" => result.queue_version = try!(line.1.parse()), - "playlistlength" => result.queue_len = try!(line.1.parse()), - "state" => result.state = try!(line.1.parse()), + "playlist" => result.queue_version = line.1.parse()?, + "playlistlength" => result.queue_len = line.1.parse()?, + "state" => result.state = line.1.parse()?, "songid" => { match result.song { None => { result.song = Some(QueuePlace { - id: Id(try!(line.1.parse())), + id: Id(line.1.parse()?), pos: 0, prio: 0, }) } - Some(ref mut place) => place.id = Id(try!(line.1.parse())), + Some(ref mut place) => place.id = Id(line.1.parse()?), } } "song" => { match result.song { None => { result.song = Some(QueuePlace { - pos: try!(line.1.parse()), + pos: line.1.parse()?, id: Id(0), prio: 0, }) } - Some(ref mut place) => place.pos = try!(line.1.parse()), + Some(ref mut place) => place.pos = line.1.parse()?, } } "nextsongid" => { match result.nextsong { None => { result.nextsong = Some(QueuePlace { - id: Id(try!(line.1.parse())), + id: Id(line.1.parse()?), pos: 0, prio: 0, }) } - Some(ref mut place) => place.id = Id(try!(line.1.parse())), + Some(ref mut place) => place.id = Id(line.1.parse()?), } } "nextsong" => { match result.nextsong { None => { result.nextsong = Some(QueuePlace { - pos: try!(line.1.parse()), + pos: line.1.parse()?, id: Id(0), prio: 0, }) } - Some(ref mut place) => place.pos = try!(line.1.parse()), + Some(ref mut place) => place.pos = line.1.parse()?, } } "time" => { let mut splits = line.1.splitn(2, ':').map(|v| v.parse().map_err(ParseError::BadInteger).map(Duration::seconds)); - result.time = try!({ - match (splits.next(), splits.next()) { - (Some(Ok(a)), Some(Ok(b))) => Ok(Some((a, b))), - (Some(Err(e)), _) | - (_, Some(Err(e))) => Err(e), - _ => Ok(None), - } - }) + result.time = match (splits.next(), splits.next()) { + (Some(Ok(a)), Some(Ok(b))) => Ok(Some((a, b))), + (Some(Err(e)), _) | + (_, Some(Err(e))) => Err(e), + _ => Ok(None), + }?; } // TODO" => float errors don't work on stable "elapsed" => result.elapsed = line.1.parse::().ok().map(|v| Duration::milliseconds((v * 1000.0) as i64)), "duration" => result.duration = line.1.parse::().ok().map(|v| Duration::milliseconds((v * 1000.0) as i64)), - "bitrate" => result.bitrate = Some(try!(line.1.parse())), - "xfade" => result.crossfade = Some(Duration::seconds(try!(line.1.parse()))), + "bitrate" => result.bitrate = Some(line.1.parse()?), + "xfade" => result.crossfade = Some(Duration::seconds(line.1.parse()?)), // "mixrampdb" => 0.0, //get_field!(map, "mixrampdb"), // "mixrampdelay" => None, //get_field!(map, opt "mixrampdelay").map(|v: f64| Duration::milliseconds((v * 1000.0) as i64)), - "audio" => result.audio = Some(try!(line.1.parse())), - "updating_db" => result.updating_db = Some(try!(line.1.parse())), + "audio" => result.audio = Some(line.1.parse()?), + "updating_db" => result.updating_db = Some(line.1.parse()?), "error" => result.error = Some(line.1.to_owned()), - "replay_gain_mode" => result.replaygain = Some(try!(line.1.parse())), + "replay_gain_mode" => result.replaygain = Some(line.1.parse()?), _ => (), } } @@ -231,15 +229,14 @@ impl FromStr for AudioFormat { fn from_str(s: &str) -> Result { let mut it = s.split(':'); Ok(AudioFormat { - rate: try!(it.next().ok_or(ParseError::NoRate).and_then(|v| v.parse().map_err(ParseError::BadRate))), - bits: try!(it.next() - .ok_or(ParseError::NoBits) + rate: it.next().ok_or(ParseError::NoRate).and_then(|v| v.parse().map_err(ParseError::BadRate))?, + bits: it.next().ok_or(ParseError::NoBits) .and_then(|v| if &*v == "f" { Ok(0) } else { v.parse().map_err(ParseError::BadBits) - })), - chans: try!(it.next().ok_or(ParseError::NoChans).and_then(|v| v.parse().map_err(ParseError::BadChans))), + })?, + chans: it.next().ok_or(ParseError::NoChans).and_then(|v| v.parse().map_err(ParseError::BadChans))?, }) } }