use crate::error::CompileError;
use crate::ir::AtomKey;
use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use elenchus_parser::Atom;
pub(crate) struct DomainCtx {
pub(crate) current: String,
pub(crate) aliases: BTreeMap<String, String>,
}
impl DomainCtx {
pub(crate) fn resolve(&self, prefix: Option<&str>) -> Result<String, CompileError> {
match prefix {
None => Ok(self.current.clone()),
Some(p) => self
.aliases
.get(p)
.cloned()
.ok_or_else(|| CompileError::UnknownDomain {
domain: p.to_string(),
}),
}
}
pub(crate) fn key(&self, a: &Atom) -> Result<AtomKey, CompileError> {
Ok(AtomKey {
domain: self.resolve(a.domain)?,
subject: a.subject.to_string(),
predicate: a.predicate.map(|p| p.to_string()),
object: a.object.map(|o| o.to_string()),
})
}
}