use super::super::doubt::{UNHEARD, WENT_QUIET, outside, redelivered, unheard, unknown};
use super::super::{Offsets, Post, Standing};
use super::run::{LISTEN_SECONDS, Run};
use super::{Mailbox, lock_mail};
impl Mailbox {
pub fn complete(
&self,
now: i64,
client: &str,
invocation: &str,
post: &Post,
) -> Result<Standing, String> {
let mut slots = lock_mail(&self.cell);
let slot = slots
.live
.get_mut(invocation)
.filter(|slot| slot.client == client)
.ok_or_else(|| unknown(invocation))?;
if slot.run.over() {
if slot.run.acknowledged() {
return Err(unknown(invocation));
}
slot.run = Run::default();
}
if matches!(post, Post::Partial(_)) && now - slot.heard > LISTEN_SECONDS {
slot.doubt(UNHEARD);
return Err(unheard(invocation));
}
slot.run.absorb(now, redelivered(client, slot.handed), post);
Ok(slot.run.receipt())
}
pub fn collect(
&self,
now: i64,
by: &str,
invocation: &str,
since: Option<Offsets>,
) -> Result<Standing, String> {
let mut slots = lock_mail(&self.cell);
let slot = slots
.live
.get_mut(invocation)
.filter(|slot| slot.by == by)
.ok_or_else(|| unknown(invocation))?;
slot.heard = now;
if slot.run.silent(now) {
slot.doubt(WENT_QUIET);
}
let standing = slot.run.answer(since).ok_or_else(|| outside(invocation))?;
if standing.capture.is_some() {
slots.live.remove(invocation);
}
Ok(standing)
}
}