use super::{Contract, Direction, Reservation, StrictReservation};
use crate::{
grant::{Lease, Operation},
EventSpec, Grant, Limits, Rights, TopicError,
};
pub struct GrantReservation {
grant: Grant,
contract: Contract,
direction: Direction,
bytes: usize,
rights: Rights,
_lease: Lease,
}
pub struct GrantAccess {
_operation: Operation,
}
impl Grant {
pub fn reserve_transport<E: EventSpec>(
&self,
direction: Direction,
bytes: usize,
) -> Result<GrantReservation, TopicError> {
let rights = match direction {
Direction::Import => Rights::PUBLISH,
Direction::Export => Rights::OBSERVE,
};
let _access = self.enter(E::ID, rights)?;
let lease = self.try_charge(Limits {
connections: 2,
retained_items: 1,
waiters: 1,
bytes,
..Limits::ZERO
})?;
Ok(GrantReservation {
grant: self.clone(),
contract: Contract::of::<E>(),
direction,
bytes,
rights,
_lease: lease,
})
}
}
impl Reservation for GrantReservation {
type Access<'a> = GrantAccess;
fn check(
&self,
contract: Contract,
direction: Direction,
bytes: usize,
) -> Result<(), TopicError> {
if contract != self.contract || direction != self.direction {
return Err(TopicError::Denied);
}
if bytes > self.bytes {
return Err(TopicError::Capacity);
}
Ok(())
}
fn enter(&self) -> Result<GrantAccess, TopicError> {
self.grant
.enter(self.contract.event, self.rights)
.map(|operation| GrantAccess {
_operation: operation,
})
}
fn is_revoked(&self) -> bool {
self.grant.is_revoked()
}
fn decode_context(&self) -> impl crate::DecodeContext + 'static {
self.grant.clone()
}
fn maintain(&self) {
self.grant.maintain_operations();
}
}
impl super::sealed::StrictReservation for GrantReservation {}
impl StrictReservation for GrantReservation {
fn enter_deferred(&self) -> Result<GrantAccess, TopicError> {
self.grant
.enter_deferred(self.contract.event, self.rights)
.map(|operation| GrantAccess {
_operation: operation,
})
}
}