condition matcher trait

Signed-off-by: blob42 <contact@blob42.xyz>
one-off-cmd
blob42 3 months ago
parent fdcddeb399
commit ff393791b3

@ -25,4 +25,3 @@ serde = { version = "1.0.203", features = ["derive"] }
sysinfo = "0.30.12"
toml = "0.8.14"
xdg = "2.5.2"

@ -1,17 +1,17 @@
#![allow(dead_code)]
#![allow(unused_variables)]
use std::time::{Duration, Instant};
use std::{marker::PhantomData, time::{Duration, Instant}};
use sysinfo::System;
#[derive(Debug)]
enum ProcState {
NeverSeen,
Seen,
NotSeen,
NeverSeen,
}
#[derive(Debug)]
struct ProcessWatch {
struct Process {
first_seen: Option<Instant>,
last_seen: Option<Instant>,
state: ProcState,
@ -19,7 +19,7 @@ struct ProcessWatch {
pid: Option<usize>,
}
impl ProcessWatch {
impl Process {
fn new() -> Self {
Self {
first_seen: None,
@ -30,23 +30,84 @@ impl ProcessWatch {
}
}
fn refresh(&mut self, sysinfo: &System, last_refresh: Instant) {}
fn refresh(&mut self, sysinfo: &System, last_refresh: Instant) {
}
}
impl Matcher<ConditionSeen> for Process {
type Condition = LifetimeCond<ConditionSeen>;
fn matches(&self, c: Self::Condition) -> bool {
if let Some(last_seen) = self.last_seen {
last_seen.elapsed() > c.span
} else { false }
}
}
struct ConditionSeen {}
impl ConditionSeen {
fn from_duration(d: Duration) -> LifetimeCond<Self> {
LifetimeCond{span: d, ty: PhantomData{}}
}
}
struct ConditionNotSeen {}
impl ConditionNotSeen {
fn from_duration(d: Duration) -> LifetimeCond<Self> {
LifetimeCond{span: d, ty: PhantomData{}}
}
}
trait Condition<Type> {}
impl<T> Condition<T> for LifetimeCond<T>{}
struct LifetimeCond<CondType> {
span: Duration,
ty: PhantomData<CondType>
}
impl<T> LifetimeCond<T> {
fn new(span: Duration) -> Self {
Self{ span, ty: PhantomData{} }
}
}
enum Condition {
Seen(Duration),
NotSeen(Duration),
trait Matcher<CondType> {
type Condition: Condition<CondType>;
fn matches(&self, c: Self::Condition) -> bool;
}
trait CheckCondition {
fn check_condition(&self, c: Condition) -> bool;
fn main() {
unimplemented!()
}
// fn main() {
// let p = ProcessWatch::new();
// println!("{:?}", p);
// for cmd in watched_cmds {
//
// }
//
// }
#[cfg(test)]
mod test {
use sysinfo::{ProcessRefreshKind, RefreshKind, UpdateKind};
use super::*;
#[test]
fn process_watch_condition() {
let cond = ConditionSeen::from_duration(Duration::from_secs(5));
let process_refresh_kind = ProcessRefreshKind::new()
.with_cmd(UpdateKind::Always)
.with_cwd(UpdateKind::Always)
.with_exe(UpdateKind::Always);
let process_refresh = RefreshKind::new().with_processes(process_refresh_kind);
let mut sys_info = System::new();
sys_info.refresh_specifics(process_refresh);
let mut p = Process::new();
p.refresh(&sys_info, Instant::now());
assert!(!p.matches(cond));
}
}

@ -288,6 +288,7 @@ impl ProcessState {
}
}
}
//HACK: the `t` field on NeverSeen is only used to make this match pattern work
ProcessState::NotSeen(since) | ProcessState::NeverSeen { t: since } => match cond {
ProcState::NotSeen(duration) if !detected => {
let elapsed_since = since.elapsed();

Loading…
Cancel
Save