diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/plugins/quote/database.rs | 12 | ||||
| -rw-r--r-- | 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<S: ::std::hash::BuildHasher + Send + Sync> Database for HashMap<(String, String, i32), Quote, S> { +impl<S: ::std::hash::BuildHasher + Send + Sync> 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<S: ::std::hash::BuildHasher + Send + Sync> Database for HashMap<(String, St } fn get_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result<Quote, QuoteError> { - 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<i32, QuoteError> { - 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<T: Database, C: Client> Quote<T, C> { 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<T: Database, C: Client> Quote<T, C> { } 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<T: Database, C: Client> Quote<T, C> { } }; - 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<String, QuoteError> { |
