From 9401a62004f3ac7313f6bf12d2649d5af61835c5 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 21 Sep 2018 00:59:44 +0200 Subject: Quote: Ignore trailing space when no index was specified --- src/plugins/quote/database.rs | 12 +++++++++--- src/plugins/quote/mod.rs | 20 +++++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/plugins/quote/database.rs b/src/plugins/quote/database.rs index 6ad08a0..49d6058 100644 --- a/src/plugins/quote/database.rs +++ b/src/plugins/quote/database.rs @@ -46,7 +46,9 @@ pub trait Database: Send + Sync { } // HashMap -impl Database for HashMap<(String, String, i32), Quote, S> { +impl Database + for HashMap<(String, String, i32), Quote, S> +{ fn insert_quote(&mut self, quote: &NewQuote) -> Result<(), QuoteError> { let quote = Quote { quotee: quote.quotee.to_owned(), @@ -66,13 +68,17 @@ impl Database for HashMap<(String, St } fn get_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result { - Ok(self.get(&(quotee.to_owned(), channel.to_owned(), idx)) + Ok(self + .get(&(quotee.to_owned(), channel.to_owned(), idx)) .cloned() .ok_or(ErrorKind::NotFound)?) } fn count_quotes(&self, quotee: &str, channel: &str) -> Result { - Ok(self.iter().filter(|&(&(ref n, ref c, _), _)| n == quotee && c == channel).count() as i32) + Ok(self + .iter() + .filter(|&(&(ref n, ref c, _), _)| n == quotee && c == channel) + .count() as i32) } } diff --git a/src/plugins/quote/mod.rs b/src/plugins/quote/mod.rs index 43333e7..9f8a29e 100644 --- a/src/plugins/quote/mod.rs +++ b/src/plugins/quote/mod.rs @@ -3,9 +3,9 @@ use std::marker::PhantomData; use std::str::FromStr; use antidote::RwLock; +use chrono::NaiveDateTime; use irc::client::prelude::*; use rand::{thread_rng, Rng}; -use chrono::NaiveDateTime; use time; use plugin::*; @@ -56,7 +56,8 @@ impl Quote { created: NaiveDateTime::from_timestamp(tm.sec, 0u32), }; - Ok(self.quotes + Ok(self + .quotes .write() .insert_quote("e) .map(|()| "Successfully added!")?) @@ -92,9 +93,11 @@ impl Quote { } let idx = match command.tokens.len() { - 1 => thread_rng().gen_range(1, count + 1), + 1 | _ if command.tokens[1].is_empty() => thread_rng().gen_range(1, count + 1), _ => { - let idx = match i32::from_str(&command.tokens[1]) { + let idx_string = &command.tokens[1]; + + let idx = match i32::from_str(idx_string) { Ok(i) => i, Err(_) => Err(ErrorKind::InvalidIndex)?, }; @@ -107,13 +110,16 @@ impl Quote { } }; - let quote = self.quotes + let quote = self + .quotes .read() .get_quote(quotee, channel, idx) .context(ErrorKind::NotFound)?; - - Ok(format!("\"{}\" - {}[{}/{}]", quote.content, quote.quotee, idx, count)) + Ok(format!( + "\"{}\" - {}[{}/{}]", + quote.content, quote.quotee, idx, count + )) } fn info(&self, command: &PluginCommand) -> Result { -- cgit v1.2.3-70-g09d2