use compact_str::CompactString;
use std::collections::{HashMap, HashSet};
use crate::value::ElementProperty;
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ExecutionPolicy {
pub(crate) defaults: HashMap<CompactString, ElementProperty>,
pub(crate) locked: HashSet<CompactString>,
}
impl ExecutionPolicy {
pub fn permissive() -> Self {
Self::default()
}
pub fn with_default(
mut self,
key: impl Into<CompactString>,
value: impl Into<ElementProperty>,
) -> Self {
self.defaults.insert(key.into(), value.into());
self
}
pub fn with_locked(mut self, key: impl Into<CompactString>) -> Self {
self.locked.insert(key.into());
self
}
pub fn is_locked(&self, key: &str) -> bool {
self.locked.contains(key)
}
pub fn default_for(&self, key: &str) -> Option<&ElementProperty> {
self.defaults.get(key)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn permissive_locks_and_defaults_nothing() {
let policy = ExecutionPolicy::permissive();
assert!(!policy.is_locked("evaluationTimeout"));
assert!(policy.default_for("evaluationTimeout").is_none());
assert_eq!(policy, ExecutionPolicy::default());
}
#[test]
fn builder_sets_defaults_and_locks_independently() {
let policy = ExecutionPolicy::permissive()
.with_default("evaluationTimeout", 2000)
.with_locked("repeat.max_loops");
assert_eq!(
policy.default_for("evaluationTimeout"),
Some(&ElementProperty::from(2000))
);
assert!(!policy.is_locked("evaluationTimeout"));
assert!(policy.is_locked("repeat.max_loops"));
assert!(policy.default_for("repeat.max_loops").is_none());
}
}