Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,31 @@ impl Default for Config {

/// Folder hooks configurations
#[derive(Debug, serde::Deserialize)]
#[serde(from = "RawFolderConfig")]
pub(crate) struct FolderConfig {
/// Hooks array
pub hooks: Vec<FolderHook>,
}

/// Folder hooks configurations as deserialized, before hook indexes are assigned
#[derive(serde::Deserialize)]
struct RawFolderConfig {
/// Hooks array
hooks: Vec<FolderHook>,
}

impl From<RawFolderConfig> for FolderConfig {
fn from(raw: RawFolderConfig) -> Self {
let mut hooks = raw.hooks;
// Assign hook indexes during deserialization so that every parsing path gets
// unique ones, they identify each hook at runtime
for (index, hook) in hooks.iter_mut().enumerate() {
hook.index = index;
}
Self { hooks }
}
}

/// Path string with ~ replaced, and canonicalized
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub(crate) struct NormalizedPath(PathBuf);
Expand Down Expand Up @@ -126,6 +146,9 @@ pub(crate) struct FolderHook {
pub command: Vec<String>,
/// Allow concurrent runs for the same hook
pub allow_concurrent: Option<bool>,
/// Unique hook index, assigned when parsing, used to identify the hook at runtime
#[serde(skip)]
pub index: usize,
}

/// Deserialize filter into a glob matcher to validate glob expression
Expand Down
28 changes: 24 additions & 4 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ use std::{
collections::HashSet,
path::{Path, PathBuf},
process::{Child, Command, Stdio},
ptr,
sync::{Arc, Mutex, mpsc},
time::Duration,
};

use crate::config;

/// Unique identifier for a folder hook
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, Eq, Hash, PartialEq)]
pub(crate) struct FolderHookId(usize);

impl FolderHookId {
/// Create unique identifier for hook
/// Create unique identifier for hook, from its parse time index
pub(crate) fn from_hook(hook: &config::FolderHook) -> Self {
let val = ptr::from_ref(hook) as usize;
Self(val)
Self(hook.index)
}
}

Expand Down Expand Up @@ -111,6 +110,7 @@ mod tests {
filter: None,
command: command.iter().map(|a| (*a).to_owned()).collect(),
allow_concurrent,
index: 0,
}
}

Expand Down Expand Up @@ -221,4 +221,24 @@ mod tests {
thread::sleep(Duration::from_millis(10));
}
}

/// Hook identity must be identical for clones of the same hook (the hook map
/// stores clones), and unique across different hooks
#[test]
fn hook_id_is_stable_and_unique() {
let hook0 = hook(&["true"], None);
let hook1 = config::FolderHook {
index: 1,
..hook0.clone()
};

assert_eq!(
FolderHookId::from_hook(&hook0),
FolderHookId::from_hook(&hook0.clone())
);
assert_ne!(
FolderHookId::from_hook(&hook0),
FolderHookId::from_hook(&hook1)
);
}
}
Loading