prefer if let when possible

This commit is contained in:
Adrian Malacoda 2017-05-16 21:35:05 -05:00
parent 25d247f299
commit c69cc61114
4 changed files with 28 additions and 45 deletions

View File

@ -26,15 +26,10 @@ use std::sync::Arc;
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use std::rc::Rc;
use std::cell::{Cell, RefCell};
use transformable_channels::mpsc::TransformableSender; use transformable_channels::mpsc::TransformableSender;
mod helpers; mod helpers;
use std::collections::btree_set::BTreeSet;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
@ -105,16 +100,14 @@ impl Tenquestionmarks {
let arc_envelope = Arc::new(envelope); let arc_envelope = Arc::new(envelope);
for (key, sender) in &module_senders { for (key, sender) in &module_senders {
let from_this = String::from(*key); let from_this = String::from(*key);
match self.subscriptions.get::<String>(&from_this) { if let Some(subscription) = self.subscriptions.get::<String>(&from_this) {
Some(subscription) => { if subscription.can_handle_event(&arc_envelope) {
if subscription.can_handle_event(&arc_envelope) { if let Err(err) = sender.send(arc_envelope.clone()) {
match sender.send(arc_envelope.clone()) { debug!("Failed to dispatch event to module \"{}\": {:?}", key, err);
Err(err) => debug!("Failed to dispatch event to module \"{}\": {:?}", key, err),
Ok(_) => {}
}
} }
}, }
None => debug!("Failed to dispatch event to module \"{}\": No such module found!", key) } else {
debug!("Failed to dispatch event to module \"{}\": No such module found!", key);
} }
} }
}, },

View File

@ -32,35 +32,26 @@ fn main () {
init_logger(); init_logger();
let config_file_name = env::args().nth(1).unwrap_or("tenquestionmarks.toml".into()); let config_file_name = env::args().nth(1).unwrap_or("tenquestionmarks.toml".into());
match File::open(&config_file_name) { if let Ok(mut file) = File::open(&config_file_name) {
Ok(mut file) => { let mut contents = String::new();
let mut contents = String::new(); if let Err(e) = file.read_to_string(&mut contents) {
match file.read_to_string(&mut contents) { error!("Failed to open config file {}: {:?}", config_file_name, e);
Ok(_) => { } else {
let mut parser = Parser::new(&contents); let mut parser = Parser::new(&contents);
match parser.parse() { if let Some(configuration) = parser.parse() {
Some(configuration) => { info!("Loaded configuration from: {}", config_file_name);
info!("Loaded configuration from: {}", config_file_name); match Tenquestionmarks::from_configuration(configuration) {
match Tenquestionmarks::from_configuration(configuration) { Ok(tqm) => {
Ok(tqm) => { info!("tenquestionmarks initialized successfully");
info!("tenquestionmarks initialized successfully"); tqm.run();
tqm.run(); },
}, Err(e) => error!("Failed to initialize tenquestionmarks: {:?}", e)
Err(e) => error!("Failed to initialize tenquestionmarks: {:?}", e)
}
},
None => {
error!("Failed to parse config file {}: {:?}. Config file must be a valid TOML file.", config_file_name, parser.errors);
}
}
},
Err(e) => {
error!("Failed to open config file {}: {:?}", config_file_name, e);
} }
} else {
error!("Failed to parse config file {}: {:?}. Config file must be a valid TOML file.", config_file_name, parser.errors);
} }
},
Err(_) => {
error!("Failed to open config file! Please specify path to a config file.");
} }
} else {
error!("Failed to open config file! Please specify path to a config file.");
} }
} }

View File

@ -147,9 +147,8 @@ impl EventLoop for DiscordModule {
channel: discord.get_channel(message.channel_id).ok().and_then(|channel| make_channel_object(discord.clone(), channel)) channel: discord.get_channel(message.channel_id).ok().and_then(|channel| make_channel_object(discord.clone(), channel))
}; };
match sender.send(Envelope::new(event::Event::Message { message: message })) { if let Err(err) = sender.send(Envelope::new(event::Event::Message { message: message })) {
Err(err) => error!("Error sending message event: {:?}", err), error!("Error sending message event: {:?}", err)
Ok(_) => {}
} }
} }
Ok(_) => {} Ok(_) => {}

View File

@ -85,4 +85,4 @@ end
[autolink] [autolink]
[logger] [logger]
filters = [{ username = "Dave" }] filters = [{ username = "Dave" }, { username = "Kevin" }]