use crate::pep::IntoPropertyValue;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Constraint {
pub predicates: Vec<Predicate>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Predicate {
Eq(EqPredicate),
In(InPredicate),
InGroup(InGroupPredicate),
InGroupSubtree(InGroupSubtreePredicate),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EqPredicate {
pub property: String,
pub value: Value,
}
impl EqPredicate {
#[must_use]
pub fn new(property: impl Into<String>, value: impl IntoPropertyValue) -> Self {
Self {
property: property.into(),
value: value.into_filter_value(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InPredicate {
pub property: String,
pub values: Vec<Value>,
}
impl InPredicate {
#[must_use]
pub fn new<V: IntoPropertyValue>(
property: impl Into<String>,
values: impl IntoIterator<Item = V>,
) -> Self {
Self {
property: property.into(),
values: values
.into_iter()
.map(IntoPropertyValue::into_filter_value)
.collect(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InGroupPredicate {
pub property: String,
pub group_ids: Vec<Value>,
}
impl InGroupPredicate {
#[must_use]
pub fn new<V: IntoPropertyValue>(
property: impl Into<String>,
group_ids: impl IntoIterator<Item = V>,
) -> Self {
Self {
property: property.into(),
group_ids: group_ids
.into_iter()
.map(IntoPropertyValue::into_filter_value)
.collect(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InGroupSubtreePredicate {
pub property: String,
pub ancestor_ids: Vec<Value>,
}
impl InGroupSubtreePredicate {
#[must_use]
pub fn new<V: IntoPropertyValue>(
property: impl Into<String>,
ancestor_ids: impl IntoIterator<Item = V>,
) -> Self {
Self {
property: property.into(),
ancestor_ids: ancestor_ids
.into_iter()
.map(IntoPropertyValue::into_filter_value)
.collect(),
}
}
}
#[cfg(test)]
#[path = "constraints_tests.rs"]
mod constraints_tests;