use crate::{expr::Expr, value::Value};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq)]
pub struct Rule {
pub(super) name: String,
metadata: BTreeMap<String, Value>,
expr: Expr,
}
impl Rule {
pub fn new(name: impl Into<String>, metadata: BTreeMap<String, Value>, expr: Expr) -> Self {
Self {
name: name.into(),
metadata,
expr,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn get_metadata(&self, field: &str) -> Option<&Value> {
self.metadata.get(field)
}
pub fn iter_metadata(&self) -> impl Iterator<Item = (&str, &Value)> {
self.metadata
.iter()
.map(|(key, value)| (key.as_ref(), value))
}
pub fn description(&self) -> Option<&str> {
match self.metadata.get("description") {
Some(Value::String(description)) => Some(description.as_str()),
_ => None,
}
}
pub fn expr(&self) -> &Expr {
&self.expr
}
}