diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-10-22 03:14:51 +0200 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-10-22 03:14:51 +0200 |
| commit | f9e3022756ea454a31f797bfc9cfdc1d81ee86cf (patch) | |
| tree | b161e4283e85732b081f0c57b0500d67b07200e4 /src/plugins | |
| parent | 489b23850bd37f39c8ebabd4d59d5753923c3c07 (diff) | |
| download | frippy-f9e3022756ea454a31f797bfc9cfdc1d81ee86cf.tar.gz frippy-f9e3022756ea454a31f797bfc9cfdc1d81ee86cf.zip | |
Factoids: Rename to Factoid
Diffstat (limited to 'src/plugins')
| -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 |
6 files changed, 44 insertions, 40 deletions
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; |
