aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2018-07-08 15:23:50 +0200
committerJokler <jokler.contact@gmail.com>2018-07-08 15:23:50 +0200
commit66ea53eb077fce74d483fd60d0105d85654088bc (patch)
treee07036386b4444ee8cc45f579676d7a5021f6ea1
parent975c94752b6ef59caea5ed632d1428efb5f6433b (diff)
downloadfrippy-66ea53eb077fce74d483fd60d0105d85654088bc.tar.gz
frippy-66ea53eb077fce74d483fd60d0105d85654088bc.zip
Sed: Remove some unwraps
-rw-r--r--src/plugins/factoids/utils.rs2
-rw-r--r--src/plugins/sed.rs18
2 files changed, 10 insertions, 10 deletions
diff --git a/src/plugins/factoids/utils.rs b/src/plugins/factoids/utils.rs
index a7bf04a..a35dd27 100644
--- a/src/plugins/factoids/utils.rs
+++ b/src/plugins/factoids/utils.rs
@@ -48,7 +48,7 @@ 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(RuntimeError(String::from(
+ let f = n.as_f64().ok_or_else(|| RuntimeError(String::from(
"Failed to convert number into double",
)))?;
LuaValue::Number(f)
diff --git a/src/plugins/sed.rs b/src/plugins/sed.rs
index cf7f9d5..2c8522f 100644
--- a/src/plugins/sed.rs
+++ b/src/plugins/sed.rs
@@ -3,7 +3,7 @@ use std::marker::PhantomData;
use antidote::RwLock;
use circular_queue::CircularQueue;
-use regex::{Regex, RegexBuilder};
+use regex::{Regex, RegexBuilder, Captures};
use irc::client::prelude::*;
@@ -64,14 +64,13 @@ impl<C: FrippyClient> Sed<C> {
output
}
- fn run_regex(&self, channel: &str, message: &str) -> Result<String, SedError> {
+ fn run_regex(&self, channel: &str, captures: &Captures) -> Result<String, SedError> {
let mut global_match = false;
let mut case_insens = false;
let mut ign_whitespace = false;
let mut swap_greed = false;
let mut enable_unicode = true;
- let captures = RE.captures(message).unwrap();
debug!("{:?}", captures);
let first = self.format_escaped(captures.get(1).unwrap().as_str());
@@ -119,20 +118,21 @@ impl<C: FrippyClient> Plugin for Sed<C> {
fn execute(&self, client: &Self::Client, message: &Message) -> ExecutionStatus {
match message.command {
Command::PRIVMSG(_, ref content) => {
- let channel = message.response_target().unwrap();
- if channel == message.source_nickname().unwrap() {
+ let channel = message.response_target().unwrap_or("");
+ let user = message.source_nickname().unwrap_or("");
+ if channel == user {
return ExecutionStatus::Done;
}
- if RE.is_match(content) {
- let result = match self.run_regex(channel, content) {
+ if let Some(captures) = RE.captures(content) {
+ let result = match self.run_regex(channel, &captures) {
Ok(msg) => client.send_privmsg(channel, &msg),
Err(e) => match e.kind() {
ErrorKind::InvalidRegex => {
let err = e.cause().unwrap().to_string();
- client.send_notice(channel, &err.replace('\n', "\r\n"))
+ client.send_notice(user, &err.replace('\n', "\r\n"))
}
- _ => client.send_notice(channel, &e.to_string()),
+ _ => client.send_notice(user, &e.to_string()),
},
};