aboutsummaryrefslogtreecommitdiff
path: root/src/hooks/sed/mod.rs
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2021-10-17 17:07:09 +0200
committerMax Audron <audron@cocaine.farm>2021-10-19 14:37:14 +0200
commit4754b420ced2503eb2641d6ddf678736e1aa7369 (patch)
tree511e69d8753cdea469a57152a483d209d16a5fa8 /src/hooks/sed/mod.rs
parentadd formatting trait for irc codes (diff)
replace sedregex crate8-rework-sed
This replaces the sedregex crate with our own implementation for multiple reasons: 1. We required to access the parsed regex, this required a patch to the sedregex crate which did not get merged due to an inactive dev, blocking us from publishing on crates.Io 2. We wanted to highlight the changes done in bold 3. We want to add execution of multiple chained sed commands in the future which would require more modification
Diffstat (limited to '')
-rw-r--r--src/hooks/sed/mod.rs (renamed from src/hooks/sed.rs)19
1 files changed, 9 insertions, 10 deletions
diff --git a/src/hooks/sed.rs b/src/hooks/sed/mod.rs
index 120ac7d..3128372 100644
--- a/src/hooks/sed.rs
+++ b/src/hooks/sed/mod.rs
@@ -1,10 +1,11 @@
use anyhow::{anyhow, bail, Context, Result};
use irc::client::prelude::*;
-use sedregex::ReplaceCommand;
-
use std::collections::HashMap;
+#[allow(dead_code)]
+mod parser;
+
static LOG_MAX_SIZE: usize = 10000;
thread_local!(static RE: regex::Regex = regex::Regex::new(r"^s/").unwrap());
@@ -59,10 +60,8 @@ impl Sed {
fn find_and_replace(&mut self, msg: &Message) -> Result<String> {
if let Command::PRIVMSG(target, text) = msg.command.clone() {
- let cmd = match ReplaceCommand::new(text.as_str()) {
- Ok(cmd) => cmd,
- Err(_) => return Err(anyhow!("building replace command failed")),
- };
+ let cmd =
+ parser::Command::from_str(text.as_str()).context("failed to parse sed command")?;
let log = self
.0
@@ -72,13 +71,13 @@ impl Sed {
return log
.iter()
.rev()
- .find(|(_, text)| cmd.expr.is_match(text) && !RE.with(|re| re.is_match(text)))
+ .find(|(_, text)| cmd.regex().is_match(text) && !RE.with(|re| re.is_match(text)))
.and_then(|(nick, text)| {
if text.starts_with("\x01\x01") {
Some(format!(
"* {}{}",
nick,
- cmd.execute(text.replace("\x01", ""))
+ cmd.execute(&text.replace("\x01", ""))
))
} else {
Some(format!("<{}> {}", nick, cmd.execute(text)))
@@ -203,7 +202,7 @@ mod tests {
command: Command::PRIVMSG("user".to_string(), "s/will be/has been/".to_string(),),
})
.unwrap(),
- "<user> this is a long message which has been replaced"
+ "<user> this is a long message which \x02has been\x02 replaced"
)
}
@@ -217,7 +216,7 @@ mod tests {
command: Command::PRIVMSG("user".to_string(), "s/(will).*(be)/$2 $1/".to_string(),),
})
.unwrap(),
- "<user> this is a long message which be will replaced"
+ "<user> this is a long message which \x02be will\x02 replaced"
)
}
}