diff --git a/Cargo.toml b/Cargo.toml index 2e0a78e..63576d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,4 +25,3 @@ serde = { version = "1.0.203", features = ["derive"] } sysinfo = "0.30.12" toml = "0.8.14" xdg = "2.5.2" - diff --git a/src/bin/proto_condition.rs b/src/bin/proto_condition.rs index afe2537..5a7980d 100644 --- a/src/bin/proto_condition.rs +++ b/src/bin/proto_condition.rs @@ -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, last_seen: Option, state: ProcState, @@ -19,7 +19,7 @@ struct ProcessWatch { pid: Option, } -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 for Process { + type Condition = LifetimeCond; + + 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 { + LifetimeCond{span: d, ty: PhantomData{}} + } +} + +struct ConditionNotSeen {} +impl ConditionNotSeen { + fn from_duration(d: Duration) -> LifetimeCond { + LifetimeCond{span: d, ty: PhantomData{}} + } +} + +trait Condition {} +impl Condition for LifetimeCond{} + +struct LifetimeCond { + span: Duration, + ty: PhantomData +} + +impl LifetimeCond { + fn new(span: Duration) -> Self { + Self{ span, ty: PhantomData{} } + } } -enum Condition { - Seen(Duration), - NotSeen(Duration), + + +trait Matcher { + type Condition: Condition; + + 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)); + } +} diff --git a/src/watch/process.rs b/src/watch/process.rs index d1f0527..095f8f3 100644 --- a/src/watch/process.rs +++ b/src/watch/process.rs @@ -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();