use std::cell::Cell;
use relate_core::Result;
use relate_ir::PathPattern;
use crate::ctx::ExecCtx;
use super::super::pattern_policies::{MatchTail, Matched};
use super::super::pushdown::Residual;
use super::super::rider::Rider;
use super::sink::MatchedSink;
use super::{complete_state, GpmCtx};
pub(super) struct GpmEmit<'a, 'data> {
ctx: ExecCtx<'a>,
path: &'a PathPattern,
sink: MatchedSink<'a, 'data>,
}
pub(super) type StateSink<'a> = dyn FnMut(super::GpmState) -> Result<()> + 'a;
pub(super) fn deliver(
gctx: &GpmCtx<'_>,
sink: &mut StateSink<'_>,
state: super::GpmState,
) -> Result<bool> {
sink(state)?;
Ok(gctx.halted())
}
impl GpmEmit<'_, '_> {
pub(super) fn offer(&mut self, gctx: &GpmCtx<'_>, mut state: super::GpmState) -> Result<()> {
if !complete_state(gctx, self.path, &mut state)? {
return Ok(());
}
self.sink.offer(self.ctx, state.row, || state.used)
}
}
pub(super) fn mount_rider<'a, 'g, 'cap>(
ctx: ExecCtx<'a>,
rider: Option<&'a mut Rider<'g, 'cap>>,
path: &'a PathPattern,
where_clause: &'a Residual<'a>,
cap_halt: &'a Cell<bool>,
tail: MatchTail<'a>,
emitted: &'a mut Matched,
) -> (Option<GpmEmit<'a, 'g>>, &'a Cell<bool>)
where
'cap: 'a,
'g: 'a,
{
let Some(rider) = rider else {
return (None, cap_halt);
};
if path.selector.is_some() {
return (None, cap_halt);
}
let path = match &*rider {
Rider::Probe(probe) => probe.pattern,
Rider::Cap(_) => path,
};
let sink = MatchedSink::new(Some(rider), cap_halt, where_clause, tail, emitted);
let halt = sink.halt_cell();
(Some(GpmEmit { ctx, path, sink }), halt)
}