From ce2db228aac1c0114bcac30bb6c01212faca025a Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 24 Feb 2018 20:13:32 +0100 Subject: Run rustfmt and clippy --- src/plugins/emoji.rs | 2 +- src/plugins/factoids/database.rs | 31 +++++------ src/plugins/factoids/mod.rs | 117 ++++++++++++++++++++------------------- src/plugins/keepnick.rs | 2 +- src/plugins/tell.rs | 11 ++-- src/plugins/url.rs | 1 - 6 files changed, 85 insertions(+), 79 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index fcb04d1..a4276f4 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -100,7 +100,7 @@ impl Plugin for Emoji { .send_privmsg(message.response_target().unwrap(), &self.emoji(content)) { Ok(_) => ExecutionStatus::Done, - Err(e) => ExecutionStatus::Err(e), + Err(e) => ExecutionStatus::Err(Box::new(e)), }, _ => ExecutionStatus::Done, } diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoids/database.rs index f003419..ccebfee 100644 --- a/src/plugins/factoids/database.rs +++ b/src/plugins/factoids/database.rs @@ -32,7 +32,7 @@ pub struct Factoid { #[cfg(feature = "mysql")] use self::mysql::factoids; #[cfg_attr(feature = "mysql", derive(Insertable))] -#[cfg_attr(feature = "mysql", table_name="factoids")] +#[cfg_attr(feature = "mysql", table_name = "factoids")] pub struct NewFactoid<'a> { pub name: &'a str, pub idx: i32, @@ -41,7 +41,6 @@ pub struct NewFactoid<'a> { pub created: NaiveDateTime, } - pub trait Database: Send { fn insert_factoid(&mut self, factoid: &NewFactoid) -> DbResponse; fn get_factoid(&self, name: &str, idx: i32) -> Option; @@ -60,7 +59,7 @@ impl Database for HashMap<(String, i32), Factoid> { created: factoid.created, }; - let name = String::from(factoid.name.clone()); + let name = factoid.name.clone(); match self.insert((name, factoid.idx), factoid) { None => DbResponse::Success, Some(_) => DbResponse::Failed("Factoid was overwritten"), @@ -68,7 +67,7 @@ impl Database for HashMap<(String, i32), Factoid> { } fn get_factoid(&self, name: &str, idx: i32) -> Option { - self.get(&(String::from(name), idx)).map(|f| f.clone()) + self.get(&(String::from(name), idx)).cloned() } fn delete_factoid(&mut self, name: &str, idx: i32) -> DbResponse { @@ -79,9 +78,7 @@ impl Database for HashMap<(String, i32), Factoid> { } fn count_factoids(&self, name: &str) -> Result { - Ok(self.iter() - .filter(|&(&(ref n, _), _)| n == name) - .count() as i32) + Ok(self.iter().filter(|&(&(ref n, _), _)| n == name).count() as i32) } } @@ -107,8 +104,9 @@ impl Database for Pool> { let conn = &*self.get().expect("Failed to get connection"); match diesel::insert_into(factoids::table) - .values(factoid) - .execute(conn) { + .values(factoid) + .execute(conn) + { Ok(_) => DbResponse::Success, Err(e) => { error!("DB Insertion Error: {}", e); @@ -124,7 +122,7 @@ impl Database for Pool> { Err(e) => { error!("DB Count Error: {}", e); None - }, + } } } @@ -133,10 +131,12 @@ impl Database for Pool> { use self::factoids::columns; let conn = &*self.get().expect("Failed to get connection"); - match diesel::delete(factoids::table - .filter(columns::name.eq(name)) - .filter(columns::idx.eq(idx))) - .execute(conn) { + match diesel::delete( + factoids::table + .filter(columns::name.eq(name)) + .filter(columns::idx.eq(idx)), + ).execute(conn) + { Ok(v) => { if v > 0 { DbResponse::Success @@ -152,7 +152,6 @@ impl Database for Pool> { } fn count_factoids(&self, name: &str) -> Result { - let conn = &*self.get().expect("Failed to get connection"); let count: Result = factoids::table .filter(factoids::columns::name.eq(name)) @@ -164,7 +163,7 @@ impl Database for Pool> { Err(e) => { error!("DB Count Error: {}", e); Err("Database Error") - }, + } } } } diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs index b662d22..c3d19b0 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoids/mod.rs @@ -35,25 +35,27 @@ macro_rules! try_lock { impl Factoids { pub fn new(db: T) -> Factoids { - Factoids { factoids: Mutex::new(db) } + Factoids { + factoids: Mutex::new(db), + } } fn create_factoid(&self, name: &str, content: &str, author: &str) -> Result<&str, &str> { - let count = try_lock!(self.factoids).count_factoids(&name)?; - let tm = time::now().to_timespec(); - - let factoid = database::NewFactoid { - name: name, - idx: count, - content: content, - author: author, - created: NaiveDateTime::from_timestamp(tm.sec, tm.nsec as u32), - }; + let count = try_lock!(self.factoids).count_factoids(name)?; + let tm = time::now().to_timespec(); + + let factoid = database::NewFactoid { + name: name, + idx: count, + content: content, + author: author, + created: NaiveDateTime::from_timestamp(tm.sec, tm.nsec as u32), + }; - match try_lock!(self.factoids).insert_factoid(&factoid) { - DbResponse::Success => Ok("Successfully added"), - DbResponse::Failed(e) => Err(e), - } + match try_lock!(self.factoids).insert_factoid(&factoid) { + DbResponse::Success => Ok("Successfully added"), + DbResponse::Failed(e) => Err(e), + } } fn add(&self, client: &IrcClient, command: &mut PluginCommand) -> Result<(), IrcError> { @@ -70,7 +72,11 @@ impl Factoids { } } - fn from_url(&self, client: &IrcClient, command: &mut PluginCommand) -> Result<(), IrcError> { + fn save_from_url( + &self, + client: &IrcClient, + command: &mut PluginCommand, + ) -> Result<(), IrcError> { if command.tokens.len() < 2 { return self.invalid_command(client, command); } @@ -100,12 +106,11 @@ impl Factoids { match try_lock!(self.factoids).delete_factoid(&name, count - 1) { DbResponse::Success => client.send_notice(&command.source, "Successfully removed"), - DbResponse::Failed(e) => client.send_notice(&command.source, &e), + DbResponse::Failed(e) => client.send_notice(&command.source, e), } } fn get(&self, client: &IrcClient, command: &PluginCommand) -> Result<(), IrcError> { - let (name, idx) = match command.tokens.len() { 0 => return self.invalid_command(client, command), 1 => { @@ -135,19 +140,17 @@ impl Factoids { let factoid = match try_lock!(self.factoids).get_factoid(name, idx) { Some(v) => v, None => { - return client.send_notice(&command.source, - &format!("{}~{} does not exist", name, idx)) + return client + .send_notice(&command.source, &format!("{}~{} does not exist", name, idx)) } }; let message = factoid.content.replace("\n", "|").replace("\r", ""); - client.send_privmsg(&command.target, - &format!("{}: {}", factoid.name, message)) + client.send_privmsg(&command.target, &format!("{}: {}", factoid.name, message)) } fn info(&self, client: &IrcClient, command: &PluginCommand) -> Result<(), IrcError> { - match command.tokens.len() { 0 => self.invalid_command(client, command), 1 => { @@ -159,14 +162,12 @@ impl Factoids { match count { 0 => client.send_notice(&command.source, &format!("{} does not exist", name)), - 1 => { - client.send_privmsg(&command.target, - &format!("There is 1 version of {}", name)) - } - _ => { - client.send_privmsg(&command.target, - &format!("There are {} versions of {}", count, name)) - } + 1 => client + .send_privmsg(&command.target, &format!("There is 1 version of {}", name)), + _ => client.send_privmsg( + &command.target, + &format!("There are {} versions of {}", count, name), + ), } } _ => { @@ -179,29 +180,32 @@ impl Factoids { let factoid = match try_lock!(self.factoids).get_factoid(name, idx) { Some(v) => v, None => { - return client.send_notice(&command.source, - &format!("{}~{} does not exist", name, idx)) + return client.send_notice( + &command.source, + &format!("{}~{} does not exist", name, idx), + ) } }; - client.send_privmsg(&command.target, - &format!("{}: Added by {} at {} UTC", - name, - factoid.author, - factoid.created)) + client.send_privmsg( + &command.target, + &format!( + "{}: Added by {} at {} UTC", + name, factoid.author, factoid.created + ), + ) } - } } - fn exec(&self, - client: &IrcClient, - mut command: PluginCommand, - error: bool) - -> Result<(), IrcError> { + fn exec( + &self, + client: &IrcClient, + mut command: PluginCommand, + error: bool, + ) -> Result<(), IrcError> { if command.tokens.len() < 1 { self.invalid_command(client, &command) - } else { let name = command.tokens.remove(0); let count = match try_lock!(self.factoids).count_factoids(&name) { @@ -215,10 +219,10 @@ impl Factoids { None => return Ok(()), }; - let value = &if factoid.starts_with(">") { + let value = &if factoid.starts_with('>') { let factoid = String::from(&factoid[1..]); - if factoid.starts_with(">") { + if factoid.starts_with('>') { factoid } else { match self.run_lua(&name, &factoid, &command) { @@ -234,12 +238,12 @@ impl Factoids { } } - fn run_lua(&self, - name: &str, - code: &str, - command: &PluginCommand) - -> Result { - + fn run_lua( + &self, + name: &str, + code: &str, + command: &PluginCommand, + ) -> Result { let args = command .tokens .iter() @@ -295,7 +299,6 @@ impl Plugin for Factoids { }; self.exec(client, c, false) - } else { Ok(()) } @@ -309,7 +312,7 @@ impl Plugin for Factoids { let sub_command = command.tokens.remove(0); match sub_command.as_ref() { "add" => self.add(client, &mut command), - "fromurl" => self.from_url(client, &mut command), + "fromurl" => self.save_from_url(client, &mut command), "remove" => self.remove(client, &mut command), "get" => self.get(client, &command), "info" => self.info(client, &command), @@ -319,7 +322,9 @@ impl Plugin for Factoids { } fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result { - Err(String::from("Evaluation of commands is not implemented for Factoids at this time")) + Err(String::from( + "Evaluation of commands is not implemented for Factoids at this time", + )) } } diff --git a/src/plugins/keepnick.rs b/src/plugins/keepnick.rs index 73f4893..bdabd90 100644 --- a/src/plugins/keepnick.rs +++ b/src/plugins/keepnick.rs @@ -27,7 +27,7 @@ impl KeepNick { info!("Trying to switch nick from {} to {}", client_nick, cfg_nick); match client.send(Command::NICK(cfg_nick)) { Ok(_) => ExecutionStatus::Done, - Err(e) => ExecutionStatus::Err(e), + Err(e) => ExecutionStatus::Err(Box::new(e)), } } else { ExecutionStatus::Done diff --git a/src/plugins/tell.rs b/src/plugins/tell.rs index 34d7cf8..89d91f2 100644 --- a/src/plugins/tell.rs +++ b/src/plugins/tell.rs @@ -61,7 +61,9 @@ impl Tell { }; let mut tells = try_lock!(self.tells); - let tell_messages = tells.entry(receiver).or_insert(Vec::with_capacity(3)); + let tell_messages = tells + .entry(receiver) + .or_insert_with(|| Vec::with_capacity(3)); (*tell_messages).push(tell); Ok("Got it!") @@ -75,7 +77,7 @@ impl Tell { receiver, &format!("Tell from {}: {}", tell.sender, tell.message), ) { - return ExecutionStatus::Err(e); + return ExecutionStatus::Err(Box::new(e)); } debug!( "Sent {:?} from {:?} to {:?}", @@ -107,8 +109,9 @@ impl Tell { impl Plugin for Tell { fn execute(&self, client: &IrcClient, message: &Message) -> ExecutionStatus { match message.command { - Command::JOIN(_, _, _) => self.send_tell(client, message.source_nickname().unwrap()), - Command::PRIVMSG(_, _) => self.send_tell(client, message.source_nickname().unwrap()), + Command::JOIN(_, _, _) | Command::PRIVMSG(_, _) => { + self.send_tell(client, message.source_nickname().unwrap()) + } _ => ExecutionStatus::Done, } } diff --git a/src/plugins/url.rs b/src/plugins/url.rs index df4fdf2..52f92d8 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -44,7 +44,6 @@ impl Url { None => return Err("No Url was found."), }; - match utils::download(self.max_kib, &url) { Some(body) => { let doc = Document::from(body.as_ref()); -- cgit v1.2.3-70-g09d2