watchexec/lib/src/event.rs

91 lines
2.4 KiB
Rust
Raw Normal View History

//! Synthetic event type, derived from inputs, triggers actions.
//!
2021-08-16 11:49:12 +02:00
//! Fundamentally, events in watchexec have three purposes:
//!
//! 1. To trigger the launch, restart, or other interruption of a process;
//! 2. To be filtered upon according to whatever set of criteria is desired;
//! 3. To carry information about what caused the event, which may be provided to the process.
2021-08-22 12:06:12 +02:00
use std::{
collections::HashMap,
2021-09-02 21:57:59 +02:00
fmt,
2021-08-22 12:06:12 +02:00
path::{Path, PathBuf},
process::ExitStatus,
2021-08-22 12:06:12 +02:00
};
2021-08-16 11:49:12 +02:00
2021-09-02 22:14:04 +02:00
use notify::EventKind;
2021-08-17 11:41:13 +02:00
use crate::signal::Signal;
2021-08-16 11:49:12 +02:00
/// An event, as far as watchexec cares about.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
2021-08-16 11:49:12 +02:00
pub struct Event {
pub particulars: Vec<Particle>,
pub metadata: HashMap<String, Vec<String>>,
}
// TODO: this really needs a better name (along with "particulars")
/// Something which can be used to filter or qualify an event.
2021-08-16 11:49:12 +02:00
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Particle {
Path(PathBuf),
2021-09-02 22:14:04 +02:00
FileEventKind(EventKind),
2021-08-16 11:49:12 +02:00
Source(Source),
Process(u32),
2021-08-17 11:41:13 +02:00
Signal(Signal),
ProcessCompletion(Option<ExitStatus>),
2021-08-16 11:49:12 +02:00
}
/// The general origin of the event.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Source {
Filesystem,
Keyboard,
Mouse,
2021-08-17 11:41:13 +02:00
Os,
2021-08-16 11:49:12 +02:00
Time,
2021-09-02 21:57:45 +02:00
Internal,
2021-08-16 11:49:12 +02:00
}
2021-08-22 12:06:12 +02:00
impl Event {
/// Return all paths in the event's particulars.
pub fn paths(&self) -> impl Iterator<Item = &Path> {
self.particulars.iter().filter_map(|p| match p {
Particle::Path(p) => Some(p.as_path()),
_ => None,
})
}
/// Return all signals in the event's particulars.
pub fn signals(&self) -> impl Iterator<Item = Signal> + '_ {
self.particulars.iter().filter_map(|p| match p {
Particle::Signal(s) => Some(*s),
_ => None,
})
}
2021-08-22 12:06:12 +02:00
}
2021-09-02 21:57:59 +02:00
impl fmt::Display for Event {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Event")?;
for p in &self.particulars {
match p {
Particle::Path(p) => write!(f, " path={}", p.display())?,
2021-09-02 22:14:04 +02:00
Particle::FileEventKind(kind) => write!(f, " kind={:?}", kind)?,
2021-09-02 21:57:59 +02:00
Particle::Source(s) => write!(f, " source={:?}", s)?,
Particle::Process(p) => write!(f, " process={}", p)?,
Particle::Signal(s) => write!(f, " signal={:?}", s)?,
Particle::ProcessCompletion(None) => write!(f, " command-completed")?,
Particle::ProcessCompletion(Some(c)) => write!(f, " command-completed({})", c)?,
}
}
if !self.metadata.is_empty() {
write!(f, " meta: {:?}", self.metadata)?;
}
Ok(())
}
}