Skip to content

Commit

Permalink
triggers: Introduce basic Manager type to pull in all handlers
Browse files Browse the repository at this point in the history
Signed-off-by: Ikey Doherty <[email protected]>
  • Loading branch information
ikeycode committed Jan 5, 2024
1 parent 59a5d7a commit d1d1fc0
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions crates/triggers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,62 @@

//! System trigger management facilities
use format::Trigger;
use thiserror::Error;

pub mod format;

pub struct Manager {
handlers: Vec<ExtractedHandler>,
}

#[derive(Debug)]
struct ExtractedHandler {
trigger: String,
handler: format::Handler,
pattern: fnmatch::Pattern,
}

#[derive(Debug, Error)]
pub enum Error {
#[error("missing handler reference in {0}: {1}")]
MissingHandler(String, String),
}

impl Manager {
/// Create a new [Manager] using the given triggers
pub fn new(triggers: Vec<Trigger>) -> Result<Self, Error> {
let mut handlers = vec![];
for trigger in triggers.iter() {
for (p, def) in trigger.paths.iter() {
for used_handler in def.handlers.iter() {
let found = trigger
.handlers
.get(used_handler)
.ok_or(Error::MissingHandler(
trigger.name.clone(),
used_handler.clone(),
))?;
handlers.push(ExtractedHandler {
trigger: trigger.name.clone(),
handler: found.clone(),
pattern: p.clone(),
});
}
}
}

Ok(Self { handlers })
}

/// Push a path, building up our matches
pub fn push_path(&mut self, path: &str) {
for (h, m) in self
.handlers
.iter()
.filter_map(|h| h.pattern.match_path(path).map(|m| (h, m)))
{
eprintln!("Matching [{}]: {:?} : {:?}", h.trigger, m, h.handler);
}
}
}

0 comments on commit d1d1fc0

Please sign in to comment.