mirror of
https://framagit.org/ppom/reaction
synced 2026-03-14 12:45:47 +01:00
less owned data, more borrowed data
This commit is contained in:
parent
d776667c80
commit
838ad1b18a
6 changed files with 37 additions and 37 deletions
|
|
@ -52,7 +52,7 @@ fn lil_main() -> Result<(), E> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn import(json_path: String, write_path: String) -> Result<(), E> {
|
||||
fn import(json_path: &str, write_path: &str) -> Result<(), E> {
|
||||
let json_file = BufReader::new(File::open(json_path.clone())?);
|
||||
let mut write_file = BufWriter::new(File::create(write_path)?);
|
||||
let bin = bincode_options();
|
||||
|
|
@ -81,7 +81,7 @@ fn import(json_path: String, write_path: String) -> Result<(), E> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn collect_stream_filters(json_path: String) -> Result<WriteHeader, E> {
|
||||
fn collect_stream_filters(json_path: &str) -> Result<WriteHeader, E> {
|
||||
let mut header = BTreeMap::new();
|
||||
let mut count = 0;
|
||||
let json_file = BufReader::new(File::open(json_path)?);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use std::{
|
|||
collections::BTreeSet,
|
||||
error::Error,
|
||||
io::{stdin, BufRead, BufReader},
|
||||
path::PathBuf,
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
|
|
@ -26,7 +26,7 @@ macro_rules! or_quit {
|
|||
};
|
||||
}
|
||||
|
||||
async fn send_retrieve(socket: &PathBuf, req: &ClientRequest) -> Result<DaemonResponse, String> {
|
||||
async fn send_retrieve(socket: &Path, req: &ClientRequest) -> Result<DaemonResponse, String> {
|
||||
let bin = bincode_options();
|
||||
let conn = or_quit!(
|
||||
"opening connection to daemon",
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ pub struct Pattern {
|
|||
}
|
||||
|
||||
impl Pattern {
|
||||
pub fn setup(&mut self, name: &String) -> Result<(), String> {
|
||||
pub fn setup(&mut self, name: &str) -> Result<(), String> {
|
||||
self._setup(name)
|
||||
.map_err(|msg| format!("pattern {}: {}", name, msg))
|
||||
}
|
||||
|
|
@ -35,8 +35,8 @@ impl Pattern {
|
|||
&self.name_with_braces
|
||||
}
|
||||
|
||||
pub fn _setup(&mut self, name: &String) -> Result<(), String> {
|
||||
self.name = name.clone();
|
||||
pub fn _setup(&mut self, name: &str) -> Result<(), String> {
|
||||
self.name = name.to_string();
|
||||
self.name_with_braces = format!("<{}>", name);
|
||||
|
||||
if self.name.is_empty() {
|
||||
|
|
@ -151,16 +151,16 @@ pub mod tests {
|
|||
// Empty name
|
||||
pattern = default_pattern();
|
||||
pattern.regex = "abc".into();
|
||||
assert!(pattern.setup(&"".into()).is_err());
|
||||
assert!(pattern.setup("").is_err());
|
||||
|
||||
// '.' in name
|
||||
pattern = default_pattern();
|
||||
pattern.regex = "abc".into();
|
||||
assert!(pattern.setup(&"na.me".into()).is_err());
|
||||
assert!(pattern.setup("na.me").is_err());
|
||||
|
||||
// Empty regex
|
||||
pattern = default_pattern();
|
||||
assert!(pattern.setup(&"name".into()).is_err());
|
||||
assert!(pattern.setup("name").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -169,17 +169,17 @@ pub mod tests {
|
|||
|
||||
// regex ok
|
||||
pattern = ok_pattern();
|
||||
assert!(pattern.setup(&"name".into()).is_ok());
|
||||
assert!(pattern.setup("name").is_ok());
|
||||
|
||||
// regex ok
|
||||
pattern = default_pattern();
|
||||
pattern.regex = "abc".into();
|
||||
assert!(pattern.setup(&"name".into()).is_ok());
|
||||
assert!(pattern.setup("name").is_ok());
|
||||
|
||||
// regex ko
|
||||
pattern = default_pattern();
|
||||
pattern.regex = "[abc".into();
|
||||
assert!(pattern.setup(&"name".into()).is_err());
|
||||
assert!(pattern.setup("name").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -191,13 +191,13 @@ pub mod tests {
|
|||
pattern.regex = "[abc]".into();
|
||||
pattern.ignore.push("a".into());
|
||||
pattern.ignore.push("b".into());
|
||||
assert!(pattern.setup(&"name".into()).is_ok());
|
||||
assert!(pattern.setup("name").is_ok());
|
||||
|
||||
// ignore ko
|
||||
pattern = default_pattern();
|
||||
pattern.regex = "[abc]".into();
|
||||
pattern.ignore.push("d".into());
|
||||
assert!(pattern.setup(&"name".into()).is_err());
|
||||
assert!(pattern.setup("name").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -209,13 +209,13 @@ pub mod tests {
|
|||
pattern.regex = "[abc]".into();
|
||||
pattern.ignore_regex.push("[a]".into());
|
||||
pattern.ignore_regex.push("a".into());
|
||||
assert!(pattern.setup(&"name".into()).is_ok());
|
||||
assert!(pattern.setup("name").is_ok());
|
||||
|
||||
// ignore_regex ko
|
||||
pattern = default_pattern();
|
||||
pattern.regex = "[abc]".into();
|
||||
pattern.ignore.push("[a".into());
|
||||
assert!(pattern.setup(&"name".into()).is_err());
|
||||
assert!(pattern.setup("name").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -230,7 +230,7 @@ pub mod tests {
|
|||
pattern.ignore_regex.push("c".into());
|
||||
pattern.ignore_regex.push("[de]".into());
|
||||
|
||||
pattern.setup(&"name".into()).unwrap();
|
||||
pattern.setup("name").unwrap();
|
||||
assert!(!pattern.not_an_ignore("a"));
|
||||
assert!(!pattern.not_an_ignore("b"));
|
||||
assert!(!pattern.not_an_ignore("c"));
|
||||
|
|
|
|||
|
|
@ -16,24 +16,24 @@ struct State {
|
|||
}
|
||||
|
||||
impl State {
|
||||
fn add_match(&mut self, m: &Match, t: Time) {
|
||||
self.pending.entry(m.clone()).or_default().insert(t);
|
||||
self.ordered_times.insert(t, m.clone());
|
||||
fn add_match(&mut self, m: &Match, t: &Time) {
|
||||
self.pending.entry(m.clone()).or_default().insert(*t);
|
||||
self.ordered_times.insert(*t, m.clone());
|
||||
}
|
||||
|
||||
fn remove(&mut self, m: Match, t: Time) -> bool {
|
||||
self.pending.entry(m).and_modify(|times| {
|
||||
times.remove(&t);
|
||||
fn remove(&mut self, m: &Match, t: &Time) -> bool {
|
||||
self.pending.entry(m.clone()).and_modify(|times| {
|
||||
times.remove(t);
|
||||
});
|
||||
self.ordered_times.remove(&t).is_some()
|
||||
self.ordered_times.remove(t).is_some()
|
||||
}
|
||||
|
||||
fn clear_past_times(&mut self, now: Time, after: Option<TimeDelta>) {
|
||||
fn clear_past_times(&mut self, now: &Time, after: Option<TimeDelta>) {
|
||||
let after = after.unwrap_or_default();
|
||||
while self
|
||||
.ordered_times
|
||||
.first_key_value()
|
||||
.is_some_and(|(k, _)| *k + after < now)
|
||||
.is_some_and(|(k, _)| *k + after < *now)
|
||||
{
|
||||
#[allow(clippy::unwrap_used)] // we just checked in the condition that first is_some
|
||||
let (_, m) = self.ordered_times.pop_first().unwrap();
|
||||
|
|
@ -81,8 +81,8 @@ impl ActionManager {
|
|||
{
|
||||
#[allow(clippy::unwrap_used)] // propagating panics is ok
|
||||
let mut state = self.state.lock().unwrap();
|
||||
state.clear_past_times(t, self.action.after_duration());
|
||||
state.add_match(&m, exec_t);
|
||||
state.clear_past_times(&t, self.action.after_duration());
|
||||
state.add_match(&m, &exec_t);
|
||||
}
|
||||
let this = self.clone();
|
||||
tokio::spawn(async move {
|
||||
|
|
@ -90,7 +90,7 @@ impl ActionManager {
|
|||
tokio::time::sleep(dur).await;
|
||||
#[allow(clippy::unwrap_used)] // propagating panics is ok
|
||||
let mut state = this.state.lock().unwrap();
|
||||
if state.remove(m.clone(), t) {
|
||||
if state.remove(&m, &t) {
|
||||
this.exec_now(m);
|
||||
}
|
||||
});
|
||||
|
|
@ -115,7 +115,7 @@ impl ActionManager {
|
|||
.iter()
|
||||
.map(|time| {
|
||||
if let Order::Flush = order {
|
||||
if state.remove(match_.clone(), *time) {
|
||||
if state.remove(&match_, time) {
|
||||
self.exec_now(match_.clone());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -165,11 +165,11 @@ impl WriteDB {
|
|||
|
||||
pub fn write(&mut self, entry: LogEntry) -> Result<(), DBError> {
|
||||
let computed = ComputedLogEntry::from(entry, &self.h)?;
|
||||
self._write(computed)
|
||||
self._write(&computed)
|
||||
}
|
||||
|
||||
fn _write<T: Serialize + std::fmt::Debug>(&mut self, data: T) -> Result<(), DBError> {
|
||||
let encoded = self.bin.serialize(&data)?;
|
||||
fn _write<T: Serialize + std::fmt::Debug>(&mut self, data: &T) -> Result<(), DBError> {
|
||||
let encoded = self.bin.serialize(data)?;
|
||||
// debug!("writing this: {:?}, {:?}", &data, &encoded);
|
||||
self.f.write_all(&encoded)?;
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -65,17 +65,17 @@ async fn answer_order(
|
|||
// Compute the Vec<(pattern_name: String, regex: String)> into a BTreeMap<Arc<Pattern>, Regex>
|
||||
let patterns = options
|
||||
.patterns
|
||||
.iter()
|
||||
.into_iter()
|
||||
.map(|(name, reg)| {
|
||||
// lookup pattern in config.patterns
|
||||
config
|
||||
.patterns()
|
||||
.iter()
|
||||
// retrieve or Err
|
||||
.find(|(pattern_name, _)| name == *pattern_name)
|
||||
.find(|(pattern_name, _)| &name == *pattern_name)
|
||||
.ok_or_else(|| format!("pattern '{name}' doesn't exist"))
|
||||
// compile Regex or Err
|
||||
.and_then(|(_, pattern)| match Regex::new(reg) {
|
||||
.and_then(|(_, pattern)| match Regex::new(®) {
|
||||
Ok(reg) => Ok((pattern.clone(), reg)),
|
||||
Err(err) => Err(format!("pattern '{name}' regex doesn't compile: {err}")),
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue