Purged try! from code (replaced with ?).

This commit is contained in:
Aaron Weiss 2017-06-25 05:06:35 -04:00
parent 52035bd31e
commit 196d6425bd
No known key found for this signature in database
GPG key ID: 0237035D9BF03AE2
3 changed files with 21 additions and 21 deletions

View file

@ -77,9 +77,9 @@ pub struct Config {
impl Config { impl Config {
/// Loads a JSON configuration from the desired path. /// Loads a JSON configuration from the desired path.
pub fn load<P: AsRef<Path>>(path: P) -> Result<Config> { pub fn load<P: AsRef<Path>>(path: P) -> Result<Config> {
let mut file = try!(File::open(path)); let mut file = File::open(path)?;
let mut data = String::new(); let mut data = String::new();
try!(file.read_to_string(&mut data)); file.read_to_string(&mut data)?;
serde_json::from_str(&data[..]).map_err(|_| { serde_json::from_str(&data[..]).map_err(|_| {
Error::new( Error::new(
ErrorKind::InvalidInput, ErrorKind::InvalidInput,
@ -90,14 +90,14 @@ impl Config {
/// Saves a JSON configuration to the desired path. /// Saves a JSON configuration to the desired path.
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> { pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
let mut file = try!(File::create(path)); let mut file = File::create(path)?;
file.write_all( file.write_all(
try!(serde_json::to_string(self).map_err(|_| { serde_json::to_string(self).map_err(|_| {
Error::new( Error::new(
ErrorKind::InvalidInput, ErrorKind::InvalidInput,
"Failed to encode configuration file.", "Failed to encode configuration file.",
) )
})).as_bytes(), })?.as_bytes(),
).map_err(|e| e.into()) ).map_err(|e| e.into())
} }

View file

@ -120,7 +120,7 @@ impl<'a> Server for ServerState {
Self: Sized, Self: Sized,
{ {
let msg = &msg.into(); let msg = &msg.into();
try!(self.handle_sent_message(&msg)); self.handle_sent_message(&msg)?;
Ok((&self.outgoing).send( Ok((&self.outgoing).send(
ServerState::sanitize(&msg.to_string()) ServerState::sanitize(&msg.to_string())
.into(), .into(),
@ -236,9 +236,9 @@ impl ServerState {
body[1..end].split(' ').collect() body[1..end].split(' ').collect()
}; };
if target.starts_with('#') { if target.starts_with('#') {
try!(self.handle_ctcp(target, tokens)) self.handle_ctcp(target, tokens)?
} else if let Some(user) = msg.source_nickname() { } else if let Some(user) = msg.source_nickname() {
try!(self.handle_ctcp(user, tokens)) self.handle_ctcp(user, tokens)?
} }
} }
} }
@ -272,7 +272,7 @@ impl ServerState {
if *index >= alt_nicks.len() { if *index >= alt_nicks.len() {
panic!("All specified nicknames were in use or disallowed.") panic!("All specified nicknames were in use or disallowed.")
} else { } else {
try!(self.send(NICK(alt_nicks[*index].to_owned()))); self.send(NICK(alt_nicks[*index].to_owned()))?;
*index += 1; *index += 1;
} }
} }
@ -288,15 +288,15 @@ impl ServerState {
let mut index = self.alt_nick_index.write().unwrap(); let mut index = self.alt_nick_index.write().unwrap();
if self.config().should_ghost() && *index != 0 { if self.config().should_ghost() && *index != 0 {
for seq in &self.config().ghost_sequence() { for seq in &self.config().ghost_sequence() {
try!(self.send(NICKSERV(format!( self.send(NICKSERV(format!(
"{} {} {}", "{} {} {}",
seq, seq,
self.config().nickname(), self.config().nickname(),
self.config().nick_password() self.config().nick_password()
)))); )))?;
} }
*index = 0; *index = 0;
try!(self.send(NICK(self.config().nickname().to_owned()))) self.send(NICK(self.config().nickname().to_owned()))?
} }
self.send(NICKSERV( self.send(NICKSERV(
format!("IDENTIFY {}", self.config().nick_password()), format!("IDENTIFY {}", self.config().nick_password()),
@ -430,10 +430,10 @@ impl ServerState {
} else if tokens[0].eq_ignore_ascii_case("VERSION") { } else if tokens[0].eq_ignore_ascii_case("VERSION") {
self.send_ctcp_internal(resp, &format!("VERSION {}", self.config().version())) self.send_ctcp_internal(resp, &format!("VERSION {}", self.config().version()))
} else if tokens[0].eq_ignore_ascii_case("SOURCE") { } else if tokens[0].eq_ignore_ascii_case("SOURCE") {
try!(self.send_ctcp_internal( self.send_ctcp_internal(
resp, resp,
&format!("SOURCE {}", self.config().source()), &format!("SOURCE {}", self.config().source()),
)); )?;
self.send_ctcp_internal(resp, "SOURCE") self.send_ctcp_internal(resp, "SOURCE")
} else if tokens[0].eq_ignore_ascii_case("PING") && tokens.len() > 1 { } else if tokens[0].eq_ignore_ascii_case("PING") && tokens.len() > 1 {
self.send_ctcp_internal(resp, &format!("PING {}", tokens[1])) self.send_ctcp_internal(resp, &format!("PING {}", tokens[1]))

View file

@ -54,16 +54,16 @@ pub trait ServerExt: Server {
Self: Sized, Self: Sized,
{ {
// Send a CAP END to signify that we're IRCv3-compliant (and to end negotiations!). // Send a CAP END to signify that we're IRCv3-compliant (and to end negotiations!).
try!(self.send(CAP(None, END, None, None))); self.send(CAP(None, END, None, None))?;
if self.config().password() != "" { if self.config().password() != "" {
try!(self.send(PASS(self.config().password().to_owned()))); self.send(PASS(self.config().password().to_owned()))?;
} }
try!(self.send(NICK(self.config().nickname().to_owned()))); self.send(NICK(self.config().nickname().to_owned()))?;
try!(self.send(USER( self.send(USER(
self.config().username().to_owned(), self.config().username().to_owned(),
"0".to_owned(), "0".to_owned(),
self.config().real_name().to_owned(), self.config().real_name().to_owned(),
))); ))?;
Ok(()) Ok(())
} }
@ -146,7 +146,7 @@ pub trait ServerExt: Server {
Self: Sized, Self: Sized,
{ {
for line in message.split("\r\n") { for line in message.split("\r\n") {
try!(self.send(PRIVMSG(target.to_owned(), line.to_owned()))) self.send(PRIVMSG(target.to_owned(), line.to_owned()))?
} }
Ok(()) Ok(())
} }
@ -157,7 +157,7 @@ pub trait ServerExt: Server {
Self: Sized, Self: Sized,
{ {
for line in message.split("\r\n") { for line in message.split("\r\n") {
try!(self.send(NOTICE(target.to_owned(), line.to_owned()))) self.send(NOTICE(target.to_owned(), line.to_owned()))?
} }
Ok(()) Ok(())
} }