From bea4d2a63b15eb3f908d8612bdc94dca1829229f Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 14 Oct 2017 04:44:26 +0200 Subject: Improve readability of the Currency plugin --- src/plugins/currency.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src/plugins/currency.rs') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index bb16cd9..c6f0b2f 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -82,6 +82,11 @@ impl Currency { } fn convert(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + + if command.tokens.len() < 3 { + return self.invalid_command(server, &command); + } + let request = match self.eval_command(&command.tokens) { Some(request) => request, None => { @@ -103,7 +108,7 @@ impl Currency { } } - fn help(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + fn help(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<(), IrcError> { let help = format!("usage: {} currency value from_currency to_currency\r\n\ example: 1.5 eur usd\r\n\ available currencies: AUD, BGN, BRL, CAD, \ @@ -117,7 +122,7 @@ impl Currency { } fn invalid_command(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { - let help = format!("Incorrect value. \ + let help = format!("Incorrect Command. \ Send \"{} help currency\" for help.", server.current_nickname()); @@ -134,18 +139,15 @@ impl Plugin for Currency { Ok(()) } - fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { - if command.tokens.is_empty() { - self.invalid_command(server, &command) + fn command(&mut self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { - } else if command.tokens[0].to_lowercase() == "help" { - self.help(server, command) - - } else if command.tokens.len() >= 3 { - self.convert(server, command) + if command.tokens.is_empty() { + return self.invalid_command(server, &command); + } - } else { - self.invalid_command(server, &command) + match command.tokens[0].as_ref() { + "help" => self.help(server, &mut command), + _ => self.convert(server, command), } } } -- cgit v1.2.3-70-g09d2 From 4130d9b299265df7c06a9dcfa426746a870be615 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 14 Oct 2017 04:46:05 +0200 Subject: Adjust formatting with cargo fmt --- src/lib.rs | 14 ++++++++------ src/plugins/currency.rs | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src/plugins/currency.rs') diff --git a/src/lib.rs b/src/lib.rs index c556a32..873358a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,7 +105,9 @@ pub fn run() { if let Some(mut c) = command.clone() { // Skip empty commands - if c.tokens.is_empty() { continue; } + if c.tokens.is_empty() { + continue; + } if lock_plugin!(plugin).name().to_lowercase() == c.tokens[0].to_lowercase() { @@ -132,10 +134,7 @@ fn get_command(nick: &str, message: &Message) -> Option { if let PRIVMSG(_, ref content) = message.command { // Split content by spaces and filter empty tokens - let mut tokens: Vec = content - .split(' ') - .map(ToOwned::to_owned) - .collect(); + let mut tokens: Vec = content.split(' ').map(ToOwned::to_owned).collect(); // Commands start with our name if tokens[0].to_lowercase().starts_with(nick) { @@ -145,7 +144,10 @@ fn get_command(nick: &str, message: &Message) -> Option { // We assume that only ':' and ',' are used as suffixes on IRC // If there are any other chars we assume that it is not ment for the bot - tokens[0] = tokens[0].chars().filter(|&c| !":,".contains(c)).collect(); + tokens[0] = tokens[0] + .chars() + .filter(|&c| !":,".contains(c)) + .collect(); if !tokens[0].is_empty() { return None; } diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index c6f0b2f..bc87592 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -116,7 +116,7 @@ impl Currency { IDR, ILS, INR, JPY, KRW, MXN, MYR, NOK, \ NZD, PHP, PLN, RON, RUB, SEK, SGD, THB, \ TRY, USD, ZAR", - server.current_nickname()); + server.current_nickname()); server.send_notice(&command.source, &help) } -- cgit v1.2.3-70-g09d2 From 44cec3059fcc5697f7347260eb77a952cfca18ab Mon Sep 17 00:00:00 2001 From: Jokler Date: Tue, 17 Oct 2017 05:14:40 +0200 Subject: Remove a few unwraps and make error handling more concise --- src/lib.rs | 43 ++++++++++++++++++++----------------------- src/plugins/currency.rs | 7 +------ 2 files changed, 21 insertions(+), 29 deletions(-) (limited to 'src/plugins/currency.rs') diff --git a/src/lib.rs b/src/lib.rs index cc9f016..da36d91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ pub fn run() { Ok(v) => configs.push(v), Err(e) => error!("Incorrect config file {}", e), } - }, + } Err(e) => error!("Failed to read path {}", e), } } @@ -94,24 +94,16 @@ pub fn run() { // Open a connection and add work for each config for config in configs { - let fut = match IrcServer::new_future(reactor.handle(), &config) { - Ok(v) => v, - Err(e) => { - error!("Failed to connect: {}", e); - return; - } - }; + let server = + match IrcServer::new_future(reactor.handle(), &config).and_then(|f| reactor.run(f)) { + Ok(v) => v, + Err(e) => { + error!("Failed to connect: {}", e); + return; + } + }; - let server = match reactor.run(fut) { - Ok(v) => { - info!("Connected to server"); - v - } - Err(e) => { - error!("Failed to connect: {}", e); - return; - } - }; + info!("Connected to server"); match server.identify() { Ok(_) => info!("Identified"), @@ -125,7 +117,7 @@ pub fn run() { let task = server .stream() .for_each(move |message| process_msg(&server, &plugin_names, plugins.clone(), message)) - .map_err(|e| Err(e).unwrap()); + .map_err(|e| error!("Failed to process message: {}", e)); reactor.handle().spawn(task); } @@ -154,10 +146,10 @@ fn process_msg(server: &IrcServer, if let Some(ref c) = command { if c.tokens.is_empty() { let help = format!("Use \"{} help\" to get help", server.current_nickname()); - server.send_notice(&c.source, &help).unwrap(); + server.send_notice(&c.source, &help)?; } else if "help" == &c.tokens[0].to_lowercase() { - send_help_message(server, c).unwrap(); + send_help_message(server, c)?; } else if !plugin_names.contains(&c.tokens[0].to_lowercase()) { @@ -166,7 +158,7 @@ fn process_msg(server: &IrcServer, server.current_nickname(), c.tokens[0]); - server.send_notice(&c.source, &help).unwrap(); + server.send_notice(&c.source, &help)?; } } @@ -181,7 +173,12 @@ fn process_msg(server: &IrcServer, let server = server.clone(); // Execute the plugin in another thread - spawn(move || { lock_plugin!(plugin).execute(&server, &message).unwrap(); }); + spawn(move || { + if let Err(e) = lock_plugin!(plugin).execute(&server, &message) { + let name = lock_plugin!(plugin).name().to_string(); + error!("Error in {} - {}", name, e); + }; + }); } // Check if the command is for this plugin diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index bc87592..0db8240 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -67,12 +67,7 @@ impl Currency { } fn eval_command<'a>(&self, tokens: &'a [String]) -> Option> { - let parsed = match tokens[0].parse() { - Ok(v) => v, - Err(_) => { - return None; - } - }; + let parsed = tokens[0].parse().ok()?; Some(ConvertionRequest { value: parsed, -- cgit v1.2.3-70-g09d2 From 8802b4a2839cb2e4d4cd2ed3d3e1b27ae4b250e6 Mon Sep 17 00:00:00 2001 From: Jokler Date: Tue, 17 Oct 2017 05:28:21 +0200 Subject: Handle one error in a verbose way again to support rust stable --- src/plugins/currency.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/plugins/currency.rs') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index 0db8240..62ba847 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -67,13 +67,15 @@ impl Currency { } fn eval_command<'a>(&self, tokens: &'a [String]) -> Option> { - let parsed = tokens[0].parse().ok()?; - - Some(ConvertionRequest { - value: parsed, - source: &tokens[1], - target: &tokens[2], - }) + if let Some(parsed) = tokens[0].parse().ok() { + Some(ConvertionRequest { + value: parsed, + source: &tokens[1], + target: &tokens[2], + }) + } else { + None + } } fn convert(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { -- cgit v1.2.3-70-g09d2 From 8c51489bdd2d0c383c7d5b8e490cf2728702ba7b Mon Sep 17 00:00:00 2001 From: Jokler Date: Wed, 18 Oct 2017 15:42:00 +0200 Subject: Pass on a Result from the Currency eval function --- src/plugins/currency.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'src/plugins/currency.rs') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index 62ba847..b15c852 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -4,8 +4,11 @@ extern crate serde_json; extern crate regex; use std::io::Read; +use std::num::ParseFloatError; + use irc::client::prelude::*; use irc::error::Error as IrcError; + use self::reqwest::Client; use self::reqwest::header::Connection; use self::serde_json::Value; @@ -66,16 +69,12 @@ impl Currency { Currency {} } - fn eval_command<'a>(&self, tokens: &'a [String]) -> Option> { - if let Some(parsed) = tokens[0].parse().ok() { - Some(ConvertionRequest { - value: parsed, - source: &tokens[1], - target: &tokens[2], - }) - } else { - None - } + fn eval_command<'a>(&self, tokens: &'a [String]) -> Result, ParseFloatError> { + Ok(ConvertionRequest { + value: tokens[0].parse()?, + source: &tokens[1], + target: &tokens[2], + }) } fn convert(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { @@ -85,8 +84,8 @@ impl Currency { } let request = match self.eval_command(&command.tokens) { - Some(request) => request, - None => { + Ok(request) => request, + Err(_) => { return self.invalid_command(server, &command); } }; -- cgit v1.2.3-70-g09d2 From 4c6f2f088521cf0876b67f21a25e524983b7ada7 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 27 Oct 2017 15:35:30 +0200 Subject: Fix currency response on bad command --- src/plugins/currency.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/plugins/currency.rs') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index b15c852..fd447e3 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -119,7 +119,7 @@ impl Currency { fn invalid_command(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { let help = format!("Incorrect Command. \ - Send \"{} help currency\" for help.", + Send \"{} currency help\" for help.", server.current_nickname()); server.send_notice(&command.source, &help) -- cgit v1.2.3-70-g09d2 From afa5eb93e9e14b65e3c846e70e22c6134ca95f80 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 27 Oct 2017 15:45:29 +0200 Subject: Panic if execute is called on the Currency Plugin --- src/plugins/currency.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/plugins/currency.rs') diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index fd447e3..78ae593 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -132,7 +132,7 @@ impl Plugin for Currency { } fn execute(&mut self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { - Ok(()) + panic!("Currency does not implement the execute function!") } fn command(&mut self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { -- cgit v1.2.3-70-g09d2