use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Scope {
Global,
Local(PathBuf),
}
impl Scope {
pub fn kind(&self) -> ScopeKind {
match self {
Scope::Global => ScopeKind::Global,
Scope::Local(_) => ScopeKind::Local,
}
}
pub fn local_root(&self) -> Option<&Path> {
match self {
Scope::Local(p) => Some(p),
Scope::Global => None,
}
}
pub fn ensure_contained(&self, path: &Path) -> Result<(), crate::error::AgentConfigError> {
match self {
Scope::Global => crate::util::fs_atomic::reject_symlink_components(path),
Scope::Local(root) => crate::util::fs_atomic::ensure_contained(path, root),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ScopeKind {
Global,
Local,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scope_accessors() {
assert_eq!(Scope::Global.kind(), ScopeKind::Global);
assert_eq!(Scope::Local(PathBuf::from("/tmp")).kind(), ScopeKind::Local);
assert!(Scope::Global.local_root().is_none());
assert_eq!(
Scope::Local(PathBuf::from("/project")).local_root(),
Some(Path::new("/project"))
);
assert_ne!(ScopeKind::Global, ScopeKind::Local);
}
}