diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-02-24 20:13:32 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-02-24 20:13:32 +0100 |
| commit | ce2db228aac1c0114bcac30bb6c01212faca025a (patch) | |
| tree | 73dc3b12bbeccfcfde6151bc629a7d0e02397c88 | |
| parent | f24da1f5090d71a4963e05c5a76dad2eccac15bd (diff) | |
| download | frippy-ce2db228aac1c0114bcac30bb6c01212faca025a.tar.gz frippy-ce2db228aac1c0114bcac30bb6c01212faca025a.zip | |
Run rustfmt and clippy
| -rw-r--r-- | Cargo.lock | 6 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/main.rs | 34 | ||||
| -rw-r--r-- | src/plugin.rs | 2 | ||||
| -rw-r--r-- | src/plugins/emoji.rs | 2 | ||||
| -rw-r--r-- | src/plugins/factoids/database.rs | 31 | ||||
| -rw-r--r-- | src/plugins/factoids/mod.rs | 117 | ||||
| -rw-r--r-- | src/plugins/keepnick.rs | 2 | ||||
| -rw-r--r-- | src/plugins/tell.rs | 11 | ||||
| -rw-r--r-- | src/plugins/url.rs | 1 | ||||
| -rw-r--r-- | src/utils.rs | 13 |
11 files changed, 111 insertions, 110 deletions
@@ -721,7 +721,7 @@ dependencies = [ [[package]] name = "mime_guess" -version = "2.0.0-alpha.3" +version = "2.0.0-alpha.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1024,7 +1024,7 @@ dependencies = [ "hyper-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "libflate 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "mime_guess 2.0.0-alpha.3 (registry+https://github.com/rust-lang/crates.io-index)", + "mime_guess 2.0.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)", "native-tls 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1599,7 +1599,7 @@ dependencies = [ "checksum migrations_internals 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bd916de6df9ac7e811e7e1ac28e0abfebe5205f3b29a7bda9ec8a41ee980a4eb" "checksum migrations_macros 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5a550cfd76f6cfdf15a7b541893d7c79b68277b0b309f12179211a373a56e617" "checksum mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e2e00e17be181010a91dbfefb01660b17311059dc8c7f48b9017677721e732bd" -"checksum mime_guess 2.0.0-alpha.3 (registry+https://github.com/rust-lang/crates.io-index)" = "013572795763289e14710c7b279461295f2673b2b338200c235082cd7ca9e495" +"checksum mime_guess 2.0.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = "130ea3c9c1b65dba905ab5a4d9ac59234a9585c24d135f264e187fe7336febbd" "checksum mio 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "7da01a5e23070d92d99b1ecd1cd0af36447c6fd44b0fe283c2db199fa136724f" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" "checksum mysqlclient-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "879ce08e38739c54d87b7f8332a476004fe2a095f40a142a36f889779d9942b7" @@ -44,8 +44,8 @@ extern crate lazy_static; #[macro_use] extern crate log; -extern crate irc; extern crate chrono; +extern crate irc; extern crate time; pub mod plugin; diff --git a/src/main.rs b/src/main.rs index 6605693..511fe09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,11 +7,11 @@ extern crate irc; extern crate time; #[cfg(feature = "mysql")] +extern crate diesel; +#[cfg(feature = "mysql")] #[macro_use] extern crate diesel_migrations; #[cfg(feature = "mysql")] -extern crate diesel; -#[cfg(feature = "mysql")] extern crate r2d2; #[cfg(feature = "mysql")] extern crate r2d2_diesel; @@ -103,10 +103,7 @@ fn main() { let mut mysql_url = None; if let Some(ref options) = config.options { if let Some(disabled) = options.get("disabled_plugins") { - disabled_plugins = Some(disabled - .split(",") - .map(|p| p.trim()) - .collect::<Vec<_>>()); + disabled_plugins = Some(disabled.split(',').map(|p| p.trim()).collect::<Vec<_>>()); } mysql_url = options.get("mysql_url"); @@ -129,18 +126,18 @@ fn main() { let manager = ConnectionManager::<MysqlConnection>::new(url.clone()); match r2d2::Pool::builder().build(manager) { - Ok(pool) => { - match embedded_migrations::run(&*pool.get().expect("Failed to get connection")) { - Ok(_) => { - bot.add_plugin(plugins::Factoids::new(pool)); - info!("Connected to MySQL server") - } - Err(e) => { - bot.add_plugin(plugins::Factoids::new(HashMap::new())); - error!("Failed to run migrations: {}", e); - } + Ok(pool) => match embedded_migrations::run(&*pool.get() + .expect("Failed to get connection")) + { + Ok(_) => { + bot.add_plugin(plugins::Factoids::new(pool)); + info!("Connected to MySQL server") + } + Err(e) => { + bot.add_plugin(plugins::Factoids::new(HashMap::new())); + error!("Failed to run migrations: {}", e); } - } + }, Err(e) => error!("Failed to connect to database: {}", e), } } else { @@ -149,13 +146,12 @@ fn main() { } #[cfg(not(feature = "mysql"))] { - if let Some(_) = mysql_url { + if mysql_url.is_some() { error!("frippy was not built with the mysql feature") } bot.add_plugin(plugins::Factoids::new(HashMap::new())); } - if let Some(disabled_plugins) = disabled_plugins { for name in disabled_plugins { if bot.remove_plugin(name).is_none() { diff --git a/src/plugin.rs b/src/plugin.rs index a67d68f..f33fa80 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -11,7 +11,7 @@ pub enum ExecutionStatus { /// The [`Plugin`](trait.Plugin.html) does not need to do any more work on this [`Message`](../../irc/proto/message/struct.Message.html). Done, /// An error occured during the execution. - Err(IrcError), + Err(Box<IrcError>), /// The execution needs to be done by [`execute_threaded()`](trait.Plugin.html#tymethod.execute_threaded). RequiresThread, } 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<Factoid>; @@ -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<Factoid> { - 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<i32, &'static str> { - 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<ConnectionManager<MysqlConnection>> { 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<ConnectionManager<MysqlConnection>> { Err(e) => { error!("DB Count Error: {}", e); None - }, + } } } @@ -133,10 +131,12 @@ impl Database for Pool<ConnectionManager<MysqlConnection>> { 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<ConnectionManager<MysqlConnection>> { } fn count_factoids(&self, name: &str) -> Result<i32, &'static str> { - let conn = &*self.get().expect("Failed to get connection"); let count: Result<i64, _> = factoids::table .filter(factoids::columns::name.eq(name)) @@ -164,7 +163,7 @@ impl Database for Pool<ConnectionManager<MysqlConnection>> { 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<T: Database> Factoids<T> { pub fn new(db: T) -> Factoids<T> { - 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<T: Database> Factoids<T> { } } - 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<T: Database> Factoids<T> { 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<T: Database> Factoids<T> { 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<T: Database> Factoids<T> { 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<T: Database> Factoids<T> { 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<T: Database> Factoids<T> { 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<T: Database> Factoids<T> { } } - fn run_lua(&self, - name: &str, - code: &str, - command: &PluginCommand) - -> Result<String, rlua::Error> { - + fn run_lua( + &self, + name: &str, + code: &str, + command: &PluginCommand, + ) -> Result<String, rlua::Error> { let args = command .tokens .iter() @@ -295,7 +299,6 @@ impl<T: Database> Plugin for Factoids<T> { }; self.exec(client, c, false) - } else { Ok(()) } @@ -309,7 +312,7 @@ impl<T: Database> Plugin for Factoids<T> { 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<T: Database> Plugin for Factoids<T> { } fn evaluate(&self, _: &IrcClient, _: PluginCommand) -> Result<String, String> { - 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()); diff --git a/src/utils.rs b/src/utils.rs index 59a5a54..0b965c8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,10 +7,7 @@ use self::reqwest::Client; use self::reqwest::header::Connection; pub fn download(max_kib: usize, url: &str) -> Option<String> { - let response = Client::new() - .get(url) - .header(Connection::close()) - .send(); + let response = Client::new().get(url).header(Connection::close()).send(); match response { Ok(mut response) => { @@ -44,16 +41,18 @@ pub fn download(max_kib: usize, url: &str) -> Option<String> { // Check if the file is too large to download if written > max_kib * 1024 { - debug!("Stopping download - File from {:?} is larger than {} KiB", url, max_kib); + debug!( + "Stopping download - File from {:?} is larger than {} KiB", + url, max_kib + ); return None; } - } Some(body) } Err(e) => { debug!("Bad response from {:?}: ({})", url, e); - return None; + None } } } |
