summaryrefslogtreecommitdiffstats
path: root/src/plugins/factoids
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/factoids')
-rw-r--r--src/plugins/factoids/database.rs31
-rw-r--r--src/plugins/factoids/mod.rs117
2 files changed, 76 insertions, 72 deletions
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",
+ ))
}
}