use crate::boundary::consumer::ConsumerCtx;
use serde_json::Value;
use std::sync::Arc;
pub struct Intake {
ctx: Arc<ConsumerCtx>,
}
impl Intake {
pub fn new(ctx: Arc<ConsumerCtx>) -> Self {
Self { ctx }
}
}
impl super::listener::Answerer for Intake {
fn answer(
&self,
peer: &crate::registry::Peer,
request: Value,
) -> Box<dyn Iterator<Item = Value>> {
self.ctx.seen(peer);
let root = &self.ctx.state_root;
if crate::registry::offer::store::held(root, &peer.client) {
let refusal = crate::registry::offer::store::refusal(&peer.client);
return Box::new(std::iter::once(crate::boundary::reply::refusal(&refusal)));
}
match self.ctx.follow(peer, &request) {
Some(frames) => frames,
None => Box::new(std::iter::once(self.ctx.answer_as(peer, &request))),
}
}
fn register(&self, code: &str, body: &[u8]) -> Option<Value> {
crate::registry::offer::claim::claim(
&self.ctx.state_root,
&crate::wire::material::dir(&self.ctx.world),
self.ctx.clock.unix(),
code,
body,
)
}
}
#[cfg(test)]
mod tests;