use std::{
collections::HashMap,
fmt,
path::{Path, PathBuf},
};
use watchexec_signals::Signal;
#[cfg(feature = "serde")]
use crate::serde_formats::{SerdeEvent, SerdeTag};
use crate::{filekind::FileEventKind, FileType, Keyboard, ProcessEnd};
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(from = "SerdeEvent", into = "SerdeEvent"))]
pub struct Event {
pub tags: Vec<Tag>,
pub metadata: HashMap<String, Vec<String>>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(from = "SerdeTag", into = "SerdeTag"))]
#[non_exhaustive]
pub enum Tag {
Path {
path: PathBuf,
file_type: Option<FileType>,
},
FileEventKind(FileEventKind),
Source(Source),
Keyboard(Keyboard),
Process(u32),
Signal(Signal),
ProcessCompletion(Option<ProcessEnd>),
#[cfg(feature = "serde")]
Unknown,
}
impl Tag {
#[must_use]
pub const fn discriminant_name(&self) -> &'static str {
match self {
Self::Path { .. } => "Path",
Self::FileEventKind(_) => "FileEventKind",
Self::Source(_) => "Source",
Self::Keyboard(_) => "Keyboard",
Self::Process(_) => "Process",
Self::Signal(_) => "Signal",
Self::ProcessCompletion(_) => "ProcessCompletion",
#[cfg(feature = "serde")]
Self::Unknown => "Unknown",
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
#[non_exhaustive]
pub enum Source {
Filesystem,
Keyboard,
Mouse,
Os,
Time,
Internal,
}
impl fmt::Display for Source {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Filesystem => "filesystem",
Self::Keyboard => "keyboard",
Self::Mouse => "mouse",
Self::Os => "os",
Self::Time => "time",
Self::Internal => "internal",
}
)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
pub enum Priority {
Low,
Normal,
High,
Urgent,
}
impl Default for Priority {
fn default() -> Self {
Self::Normal
}
}
impl Event {
#[must_use]
pub fn is_internal(&self) -> bool {
self.tags
.iter()
.any(|tag| matches!(tag, Tag::Source(Source::Internal)))
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.tags.is_empty()
}
pub fn paths(&self) -> impl Iterator<Item = (&Path, Option<&FileType>)> {
self.tags.iter().filter_map(|p| match p {
Tag::Path { path, file_type } => Some((path.as_path(), file_type.as_ref())),
_ => None,
})
}
pub fn signals(&self) -> impl Iterator<Item = Signal> + '_ {
self.tags.iter().filter_map(|p| match p {
Tag::Signal(s) => Some(*s),
_ => None,
})
}
pub fn completions(&self) -> impl Iterator<Item = Option<ProcessEnd>> + '_ {
self.tags.iter().filter_map(|p| match p {
Tag::ProcessCompletion(s) => Some(*s),
_ => None,
})
}
}
impl fmt::Display for Event {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Event")?;
for p in &self.tags {
match p {
Tag::Path { path, file_type } => {
write!(f, " path={}", path.display())?;
if let Some(ft) = file_type {
write!(f, " filetype={ft}")?;
}
}
Tag::FileEventKind(kind) => write!(f, " kind={kind:?}")?,
Tag::Source(s) => write!(f, " source={s:?}")?,
Tag::Keyboard(k) => write!(f, " keyboard={k:?}")?,
Tag::Process(p) => write!(f, " process={p}")?,
Tag::Signal(s) => write!(f, " signal={s:?}")?,
Tag::ProcessCompletion(None) => write!(f, " command-completed")?,
Tag::ProcessCompletion(Some(c)) => write!(f, " command-completed({c:?})")?,
#[cfg(feature = "serde")]
Tag::Unknown => write!(f, " unknown")?,
}
}
if !self.metadata.is_empty() {
write!(f, " meta: {:?}", self.metadata)?;
}
Ok(())
}
}