diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/error.rs | 9 | ||||
| -rw-r--r-- | src/main.rs | 15 | ||||
| -rw-r--r-- | src/plugins/emoji.rs | 3 | ||||
| -rw-r--r-- | src/plugins/factoid/database.rs (renamed from src/plugins/factoids/database.rs) | 27 | ||||
| -rw-r--r-- | src/plugins/factoid/mod.rs (renamed from src/plugins/factoids/mod.rs) | 41 | ||||
| -rw-r--r-- | src/plugins/factoid/sandbox.lua (renamed from src/plugins/factoids/sandbox.lua) | 0 | ||||
| -rw-r--r-- | src/plugins/factoid/utils.rs (renamed from src/plugins/factoids/utils.rs) | 9 | ||||
| -rw-r--r-- | src/plugins/mod.rs | 4 |
8 files changed, 57 insertions, 51 deletions
diff --git a/src/error.rs b/src/error.rs index 70a7724..039b71d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,8 @@ use failure::Fail; pub fn log_error(e: &FrippyError) { - let text = e.causes() + let text = e + .causes() .skip(1) .fold(format!("{}", e), |acc, err| format!("{}: {}", acc, err)); error!("{}", text); @@ -29,9 +30,9 @@ pub enum ErrorKind { #[fail(display = "A Tell error has occured")] Tell, - /// A Factoids error - #[fail(display = "A Factoids error has occured")] - Factoids, + /// A Factoid error + #[fail(display = "A Factoid error has occured")] + Factoid, /// A Quote error #[fail(display = "A Quote error has occured")] diff --git a/src/main.rs b/src/main.rs index c337a46..03f3c7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,10 +30,10 @@ use glob::glob; use irc::client::reactor::IrcReactor; use frippy::plugins::emoji::Emoji; -use frippy::plugins::factoids::Factoids; -use frippy::plugins::quote::Quote; +use frippy::plugins::factoid::Factoid; use frippy::plugins::help::Help; use frippy::plugins::keepnick::KeepNick; +use frippy::plugins::quote::Quote; use frippy::plugins::remind::Remind; use frippy::plugins::sed::Sed; use frippy::plugins::tell::Tell; @@ -58,7 +58,8 @@ fn main() { // Print any errors that caused frippy to shut down if let Err(e) = run() { - let text = e.iter_causes() + let text = e + .iter_causes() .fold(format!("{}", e), |acc, err| format!("{}: {}", acc, err)); error!("{}", text); } @@ -122,14 +123,14 @@ fn run() -> Result<(), Error> { Ok(pool) => match embedded_migrations::run(&*pool.get()?) { Ok(_) => { let pool = Arc::new(pool); - bot.add_plugin(Factoids::new(pool.clone())); + bot.add_plugin(Factoid::new(pool.clone())); bot.add_plugin(Quote::new(pool.clone())); bot.add_plugin(Tell::new(pool.clone())); bot.add_plugin(Remind::new(pool.clone())); info!("Connected to MySQL server") } Err(e) => { - bot.add_plugin(Factoids::new(HashMap::new())); + bot.add_plugin(Factoid::new(HashMap::new())); bot.add_plugin(Quote::new(HashMap::new())); bot.add_plugin(Tell::new(HashMap::new())); bot.add_plugin(Remind::new(HashMap::new())); @@ -139,7 +140,7 @@ fn run() -> Result<(), Error> { Err(e) => error!("Failed to connect to database: {}", e), } } else { - bot.add_plugin(Factoids::new(HashMap::new())); + bot.add_plugin(Factoid::new(HashMap::new())); bot.add_plugin(Quote::new(HashMap::new())); bot.add_plugin(Tell::new(HashMap::new())); bot.add_plugin(Remind::new(HashMap::new())); @@ -150,7 +151,7 @@ fn run() -> Result<(), Error> { if mysql_url.is_some() { error!("frippy was not built with the mysql feature") } - bot.add_plugin(Factoids::new(HashMap::new())); + bot.add_plugin(Factoid::new(HashMap::new())); bot.add_plugin(Quote::new(HashMap::new())); bot.add_plugin(Tell::new(HashMap::new())); bot.add_plugin(Remind::new(HashMap::new())); diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index d738110..aee61b1 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -137,8 +137,7 @@ impl<C: FrippyClient> Plugin for Emoji<C> { .send_notice( &command.source, "This Plugin does not implement any commands.", - ) - .context(FrippyErrorKind::Connection)?; + ).context(FrippyErrorKind::Connection)?; Ok(()) } diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoid/database.rs index ec8ed3e..5e7e24c 100644 --- a/src/plugins/factoids/database.rs +++ b/src/plugins/factoid/database.rs @@ -38,15 +38,15 @@ pub struct NewFactoid<'a> { } pub trait Database: Send + Sync { - fn insert_factoid(&mut self, factoid: &NewFactoid) -> Result<(), FactoidsError>; - fn get_factoid(&self, name: &str, idx: i32) -> Result<Factoid, FactoidsError>; - fn delete_factoid(&mut self, name: &str, idx: i32) -> Result<(), FactoidsError>; - fn count_factoids(&self, name: &str) -> Result<i32, FactoidsError>; + fn insert_factoid(&mut self, factoid: &NewFactoid) -> Result<(), FactoidError>; + fn get_factoid(&self, name: &str, idx: i32) -> Result<Factoid, FactoidError>; + fn delete_factoid(&mut self, name: &str, idx: i32) -> Result<(), FactoidError>; + fn count_factoids(&self, name: &str) -> Result<i32, FactoidError>; } // HashMap impl<S: ::std::hash::BuildHasher + Send + Sync> Database for HashMap<(String, i32), Factoid, S> { - fn insert_factoid(&mut self, factoid: &NewFactoid) -> Result<(), FactoidsError> { + fn insert_factoid(&mut self, factoid: &NewFactoid) -> Result<(), FactoidError> { let factoid = Factoid { name: factoid.name.to_owned(), idx: factoid.idx, @@ -62,20 +62,21 @@ impl<S: ::std::hash::BuildHasher + Send + Sync> Database for HashMap<(String, i3 } } - fn get_factoid(&self, name: &str, idx: i32) -> Result<Factoid, FactoidsError> { - Ok(self.get(&(name.to_owned(), idx)) + fn get_factoid(&self, name: &str, idx: i32) -> Result<Factoid, FactoidError> { + Ok(self + .get(&(name.to_owned(), idx)) .cloned() .ok_or(ErrorKind::NotFound)?) } - fn delete_factoid(&mut self, name: &str, idx: i32) -> Result<(), FactoidsError> { + fn delete_factoid(&mut self, name: &str, idx: i32) -> Result<(), FactoidError> { match self.remove(&(name.to_owned(), idx)) { Some(_) => Ok(()), None => Err(ErrorKind::NotFound)?, } } - fn count_factoids(&self, name: &str) -> Result<i32, FactoidsError> { + fn count_factoids(&self, name: &str) -> Result<i32, FactoidError> { Ok(self.iter().filter(|&(&(ref n, _), _)| n == name).count() as i32) } } @@ -100,7 +101,7 @@ use self::schema::factoids; #[cfg(feature = "mysql")] impl Database for Arc<Pool<ConnectionManager<MysqlConnection>>> { - fn insert_factoid(&mut self, factoid: &NewFactoid) -> Result<(), FactoidsError> { + fn insert_factoid(&mut self, factoid: &NewFactoid) -> Result<(), FactoidError> { use diesel; let conn = &*self.get().context(ErrorKind::NoConnection)?; @@ -112,7 +113,7 @@ impl Database for Arc<Pool<ConnectionManager<MysqlConnection>>> { Ok(()) } - fn get_factoid(&self, name: &str, idx: i32) -> Result<Factoid, FactoidsError> { + fn get_factoid(&self, name: &str, idx: i32) -> Result<Factoid, FactoidError> { let conn = &*self.get().context(ErrorKind::NoConnection)?; Ok(factoids::table .find((name, idx)) @@ -120,7 +121,7 @@ impl Database for Arc<Pool<ConnectionManager<MysqlConnection>>> { .context(ErrorKind::MysqlError)?) } - fn delete_factoid(&mut self, name: &str, idx: i32) -> Result<(), FactoidsError> { + fn delete_factoid(&mut self, name: &str, idx: i32) -> Result<(), FactoidError> { use self::factoids::columns; use diesel; @@ -142,7 +143,7 @@ impl Database for Arc<Pool<ConnectionManager<MysqlConnection>>> { } } - fn count_factoids(&self, name: &str) -> Result<i32, FactoidsError> { + fn count_factoids(&self, name: &str) -> Result<i32, FactoidError> { use diesel; let conn = &*self.get().context(ErrorKind::NoConnection)?; diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoid/mod.rs index a3d521a..4fcc7a0 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoid/mod.rs @@ -32,14 +32,14 @@ enum FactoidResponse { } #[derive(PluginName)] -pub struct Factoids<T: Database, C: Client> { +pub struct Factoid<T: Database, C: Client> { factoids: RwLock<T>, phantom: PhantomData<C>, } -impl<T: Database, C: Client> Factoids<T, C> { +impl<T: Database, C: Client> Factoid<T, C> { pub fn new(db: T) -> Self { - Factoids { + Factoid { factoids: RwLock::new(db), phantom: PhantomData, } @@ -50,7 +50,7 @@ impl<T: Database, C: Client> Factoids<T, C> { name: &str, content: &str, author: &str, - ) -> Result<&str, FactoidsError> { + ) -> Result<&str, FactoidError> { let count = self.factoids.read().count_factoids(name)?; let tm = time::now().to_timespec(); @@ -62,13 +62,14 @@ impl<T: Database, C: Client> Factoids<T, C> { created: NaiveDateTime::from_timestamp(tm.sec, 0u32), }; - Ok(self.factoids + Ok(self + .factoids .write() .insert_factoid(&factoid) .map(|()| "Successfully added!")?) } - fn add(&self, command: &mut PluginCommand) -> Result<&str, FactoidsError> { + fn add(&self, command: &mut PluginCommand) -> Result<&str, FactoidError> { if command.tokens.len() < 2 { Err(ErrorKind::InvalidCommand)?; } @@ -79,7 +80,7 @@ impl<T: Database, C: Client> Factoids<T, C> { Ok(self.create_factoid(&name, &content, &command.source)?) } - fn add_from_url(&self, command: &mut PluginCommand) -> Result<&str, FactoidsError> { + fn add_from_url(&self, command: &mut PluginCommand) -> Result<&str, FactoidError> { if command.tokens.len() < 2 { Err(ErrorKind::InvalidCommand)?; } @@ -94,7 +95,7 @@ impl<T: Database, C: Client> Factoids<T, C> { Ok(self.create_factoid(&name, &content, &command.source)?) } - fn remove(&self, command: &mut PluginCommand) -> Result<&str, FactoidsError> { + fn remove(&self, command: &mut PluginCommand) -> Result<&str, FactoidError> { if command.tokens.is_empty() { Err(ErrorKind::InvalidCommand)?; } @@ -108,7 +109,7 @@ impl<T: Database, C: Client> Factoids<T, C> { } } - fn get(&self, command: &PluginCommand) -> Result<String, FactoidsError> { + fn get(&self, command: &PluginCommand) -> Result<String, FactoidError> { let (name, idx) = match command.tokens.len() { 0 => Err(ErrorKind::InvalidCommand)?, 1 => { @@ -132,7 +133,8 @@ impl<T: Database, C: Client> Factoids<T, C> { } }; - let factoid = self.factoids + let factoid = self + .factoids .read() .get_factoid(name, idx) .context(ErrorKind::NotFound)?; @@ -142,7 +144,7 @@ impl<T: Database, C: Client> Factoids<T, C> { Ok(format!("{}: {}", factoid.name, message)) } - fn info(&self, command: &PluginCommand) -> Result<String, FactoidsError> { + fn info(&self, command: &PluginCommand) -> Result<String, FactoidError> { match command.tokens.len() { 0 => Err(ErrorKind::InvalidCommand)?, 1 => { @@ -168,7 +170,7 @@ impl<T: Database, C: Client> Factoids<T, C> { } } - fn exec(&self, mut command: PluginCommand) -> Result<String, FactoidsError> { + fn exec(&self, mut command: PluginCommand) -> Result<String, FactoidError> { if command.tokens.is_empty() { Err(ErrorKind::InvalidIndex)? } else { @@ -232,7 +234,7 @@ impl<T: Database, C: Client> Factoids<T, C> { } } -impl<T: Database, C: FrippyClient> Plugin for Factoids<T, C> { +impl<T: Database, C: FrippyClient> Plugin for Factoid<T, C> { type Client = C; fn execute(&self, _: &Self::Client, message: &Message) -> ExecutionStatus { match message.command { @@ -292,7 +294,8 @@ impl<T: Database, C: FrippyClient> Plugin for Factoids<T, C> { let sub_command = command.tokens.remove(0); let result = match sub_command.as_ref() { "add" => self.add(&mut command).map(|s| Private(s.to_owned())), - "fromurl" => self.add_from_url(&mut command) + "fromurl" => self + .add_from_url(&mut command) .map(|s| Private(s.to_owned())), "remove" => self.remove(&mut command).map(|s| Private(s.to_owned())), "get" => self.get(&command).map(Public), @@ -316,7 +319,7 @@ impl<T: Database, C: FrippyClient> Plugin for Factoids<T, C> { client .send_notice(&source, &message) .context(FrippyErrorKind::Connection)?; - Err(e).context(FrippyErrorKind::Factoids)? + Err(e).context(FrippyErrorKind::Factoid)? } } @@ -325,20 +328,20 @@ impl<T: Database, C: FrippyClient> Plugin for Factoids<T, C> { fn evaluate(&self, _: &Self::Client, _: PluginCommand) -> Result<String, String> { Err(String::from( - "Evaluation of commands is not implemented for Factoids at this time", + "Evaluation of commands is not implemented for Factoid at this time", )) } } -impl<T: Database, C: FrippyClient> fmt::Debug for Factoids<T, C> { +impl<T: Database, C: FrippyClient> fmt::Debug for Factoid<T, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Factoids {{ ... }}") + write!(f, "Factoid {{ ... }}") } } pub mod error { #[derive(Copy, Clone, Eq, PartialEq, Debug, Fail, Error)] - #[error = "FactoidsError"] + #[error = "FactoidError"] pub enum ErrorKind { /// Invalid command error #[fail(display = "Invalid Command")] diff --git a/src/plugins/factoids/sandbox.lua b/src/plugins/factoid/sandbox.lua index a927535..a927535 100644 --- a/src/plugins/factoids/sandbox.lua +++ b/src/plugins/factoid/sandbox.lua diff --git a/src/plugins/factoids/utils.rs b/src/plugins/factoid/utils.rs index a35dd27..7bd9b20 100644 --- a/src/plugins/factoids/utils.rs +++ b/src/plugins/factoid/utils.rs @@ -48,9 +48,9 @@ fn convert_value(lua: &Lua, sval: SerdeValue, max_recurs: usize) -> Result<LuaVa SerdeValue::Bool(b) => LuaValue::Boolean(b), SerdeValue::String(s) => LuaValue::String(lua.create_string(&s)?), SerdeValue::Number(n) => { - let f = n.as_f64().ok_or_else(|| RuntimeError(String::from( - "Failed to convert number into double", - )))?; + let f = n.as_f64().ok_or_else(|| { + RuntimeError(String::from("Failed to convert number into double")) + })?; LuaValue::Number(f) } SerdeValue::Array(arr) => { @@ -75,7 +75,8 @@ fn convert_value(lua: &Lua, sval: SerdeValue, max_recurs: usize) -> Result<LuaVa } pub fn json_decode(lua: &Lua, json: String) -> Result<LuaValue, LuaError> { - let ser_val: SerdeValue = serde_json::from_str(&json).map_err(|e| RuntimeError(e.to_string()))?; + let ser_val: SerdeValue = + serde_json::from_str(&json).map_err(|e| RuntimeError(e.to_string()))?; convert_value(lua, ser_val, 25) } diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index e3bda60..8aa19a0 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -1,9 +1,9 @@ //! Collection of plugins included pub mod emoji; -pub mod factoids; -pub mod quote; +pub mod factoid; pub mod help; pub mod keepnick; +pub mod quote; pub mod remind; pub mod sed; pub mod tell; |
