use std::sync::Arc;
use crate::VariableId;
use super::{QueryError, ResponseQuery};
#[derive(Clone, Debug, PartialEq)]
pub struct TransportQuery {
pub response: ResponseQuery,
pub source_population: Arc<str>,
pub target_population: Arc<str>,
pub source_experiments: Arc<[VariableId]>,
}
impl TransportQuery {
#[must_use]
pub fn new(
response: ResponseQuery,
source_population: impl Into<Arc<str>>,
target_population: impl Into<Arc<str>>,
source_experiments: impl Into<Arc<[VariableId]>>,
) -> Self {
Self {
response,
source_population: source_population.into(),
target_population: target_population.into(),
source_experiments: source_experiments.into(),
}
}
pub fn validate(&self) -> Result<(), QueryError> {
self.response.validate()?;
if self.source_population.trim().is_empty()
|| self.target_population.trim().is_empty()
|| self.source_population == self.target_population
{
return Err(QueryError::InvalidTransport(
"source and target population keys must be non-empty and distinct".into(),
));
}
let mut experiments = self.source_experiments.to_vec();
experiments.sort_unstable_by_key(|id| id.raw());
if experiments.windows(2).any(|pair| pair[0] == pair[1]) {
return Err(QueryError::InvalidTransport(
"source experiment variables must be unique".into(),
));
}
Ok(())
}
}