use super::super::prelude::*;
use crate::errors::error_event_ref_not_allowed;
pub(crate) struct TargetEventRef<'script> {
rewritten: bool,
group_expressions: Vec<ImutExpr<'script>>,
}
impl<'script> TargetEventRef<'script> {
pub(crate) fn new(group_expressions: Vec<ImutExpr<'script>>) -> Self {
Self {
rewritten: false,
group_expressions,
}
}
pub(crate) fn rewrite_target(&mut self, target: &mut ImutExpr<'script>) -> Result<bool> {
self.walk_expr(target)?;
Ok(self.rewritten)
}
}
impl<'script> ImutExprWalker<'script> for TargetEventRef<'script> {}
impl<'script> ImutExprVisitor<'script> for TargetEventRef<'script> {
fn visit_expr(&mut self, e: &mut ImutExpr<'script>) -> Result<VisitRes> {
for (idx, group_expr) in self.group_expressions.iter().enumerate() {
if e.ast_eq(group_expr) {
*e = ImutExpr::Path(Path::Reserved(crate::ast::ReservedPath::Group {
mid: Box::new(e.meta().clone()),
segments: vec![crate::ast::Segment::Idx {
mid: Box::new(e.meta().clone()),
idx,
}],
}));
self.rewritten = true;
return Ok(VisitRes::Stop);
}
}
Ok(VisitRes::Walk)
}
fn visit_path(&mut self, path: &mut Path<'script>) -> Result<VisitRes> {
match path {
Path::Event(_) | Path::Meta(_) => {
return error_event_ref_not_allowed(path, path);
}
_ => {}
}
Ok(VisitRes::Walk)
}
}