use std::collections::HashSet;
use crate::{
compiler::fact_table::FactTableMetadata,
error::{FraiseQLError, Result},
};
#[derive(Debug, Clone, Default)]
pub struct WindowAllowlist {
fields: HashSet<String>,
}
impl WindowAllowlist {
#[must_use]
pub fn from_metadata(metadata: &FactTableMetadata) -> Self {
let mut fields = HashSet::new();
for m in &metadata.measures {
fields.insert(m.name.clone());
}
for f in &metadata.denormalized_filters {
fields.insert(f.name.clone());
}
for p in &metadata.dimensions.paths {
fields.insert(p.name.clone());
fields.insert(p.json_path.clone());
}
Self { fields }
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.fields.is_empty()
}
pub fn validate(&self, identifier: &str, context: &str) -> Result<()> {
if self.fields.is_empty() || self.fields.contains(identifier) {
Ok(())
} else {
Err(FraiseQLError::Validation {
message: format!(
"Field '{identifier}' is not a known {context} field for this window query. \
Only fields declared in the compiled schema are permitted."
),
path: None,
})
}
}
}