use std::sync::Arc;
use crate::ids::{DistributionRef, EnvironmentId};
use super::error::QueryError;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum PredicateExpr {
Named(Arc<str>),
Rows(Arc<[usize]>),
}
impl PredicateExpr {
#[must_use]
pub fn named(id: impl Into<Arc<str>>) -> Self {
Self::Named(id.into())
}
#[must_use]
pub fn rows(rows: impl Into<Arc<[usize]>>) -> Self {
Self::Rows(rows.into())
}
pub fn validate(&self) -> Result<(), QueryError> {
match self {
Self::Named(name) => {
if name.is_empty() {
Err(QueryError::EmptyPredicateName)
} else {
Ok(())
}
}
Self::Rows(rows) => {
if rows.is_empty() {
Err(QueryError::EmptyPopulationRows)
} else {
Ok(())
}
}
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum TargetPopulation {
AllObserved,
Treated,
Untreated,
Environment(EnvironmentId),
Predicate(PredicateExpr),
CustomDistribution(DistributionRef),
}
impl TargetPopulation {
pub fn validate(&self) -> Result<(), QueryError> {
match self {
Self::Predicate(expr) => expr.validate(),
Self::AllObserved
| Self::Treated
| Self::Untreated
| Self::Environment(_)
| Self::CustomDistribution(_) => Ok(()),
}
}
}