Skip to content

Commit

Permalink
Replace try!() with ? operator
Browse files Browse the repository at this point in the history
As of 1.39, the ``try!'' macro is deprecated:
rust-lang/rust#62672

This changeset contains purely mechanical substitutions, no
change in functionality.
  • Loading branch information
phi-gamma committed Nov 8, 2019
1 parent bc71907 commit e7bdf47
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 72 deletions.
6 changes: 3 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ impl<S: Read + Write> Client<S> {
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::<Version>());
let version = banner[7..].trim().parse::<Version>()?;

Ok(Client {
socket: socket,
Expand Down Expand Up @@ -610,7 +610,7 @@ impl<S: Read + Write> Proto for Client<S> {

fn read_line(&mut self) -> Result<String> {
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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl FromStr for ErrorCode {
type Err = ParseError;
fn from_str(s: &str) -> result::Result<ErrorCode, ParseError> {
use self::ErrorCode::*;
match try!(s.parse()) {
match s.parse()? {
1 => Ok(NotList),
2 => Ok(Argument),
3 => Ok(Password),
Expand Down
10 changes: 5 additions & 5 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))))?
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub struct Message {
impl FromMap for Message {
fn from_map(map: BTreeMap<String, String>) -> Result<Message, Error> {
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")))?,
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub struct Mount {
impl FromMap for Mount {
fn from_map(map: BTreeMap<String, String>) -> Result<Mount, Error> {
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")))?,
})
}
}
Expand All @@ -44,8 +44,8 @@ pub struct Neighbor {
impl FromMap for Neighbor {
fn from_map(map: BTreeMap<String, String>) -> Result<Neighbor, Error> {
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")))?,
})
}
}
2 changes: 1 addition & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl FromMap for Output {
fn from_map(map: BTreeMap<String, String>) -> Result<Output, Error> {
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"),
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub struct Playlist {
impl FromMap for Playlist {
fn from_map(map: BTreeMap<String, String>) -> Result<Playlist, Error> {
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))?,
})
}
}
2 changes: 1 addition & 1 deletion src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl FromIter for Vec<Plugin> {
let mut result = Vec::new();
let mut plugin: Option<Plugin> = None;
for reply in iter {
let (a, b) = try!(reply);
let (a, b) = reply?;
match &*a {
"plugin" => {
plugin.map(|p| result.push(p));
Expand Down
6 changes: 3 additions & 3 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
_ => (),
Expand All @@ -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::<Reply>() {
Ok(Reply::Ok) => Ok(()),
Expand All @@ -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::<Reply>() {
Ok(Reply::Pair(a, b)) => Ok((a, b)),
Expand Down
20 changes: 10 additions & 10 deletions src/song.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?,
}
}
_ => {
Expand Down
16 changes: 8 additions & 8 deletions src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
_ => (),
}
}
Expand Down
59 changes: 28 additions & 31 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<f32>().ok().map(|v| Duration::milliseconds((v * 1000.0) as i64)),
"duration" => result.duration = line.1.parse::<f32>().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()?),
_ => (),
}
}
Expand All @@ -231,15 +229,14 @@ impl FromStr for AudioFormat {
fn from_str(s: &str) -> Result<AudioFormat, ParseError> {
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))?,
})
}
}
Expand Down

0 comments on commit e7bdf47

Please sign in to comment.