use core::mem;
use super::{error::Error, frame::stream_id::StreamId};
#[derive(Clone, Copy)]
pub(crate) enum LastStreamId {
Incrementable(StreamId),
GoAway(StreamId),
}
impl LastStreamId {
pub(crate) const fn new() -> Self {
Self::Incrementable(StreamId::ZERO)
}
pub(crate) fn check_idle(self, id: StreamId) -> bool {
match self {
Self::Incrementable(last_id) | Self::GoAway(last_id) => id > last_id,
}
}
pub(crate) fn try_set(&mut self, id: StreamId) -> Result<Option<()>, Error> {
match self {
Self::GoAway(_) => Ok(None),
Self::Incrementable(last_id) if !id.is_client_initiated() || id <= *last_id => Err(Error::InvalidStreamId),
Self::Incrementable(last_id) => {
*last_id = id;
Ok(Some(()))
}
}
}
pub(crate) fn try_go_away(&mut self) -> Option<StreamId> {
match *self {
Self::Incrementable(id) => {
let _ = mem::replace(self, LastStreamId::GoAway(id));
Some(id)
}
_ => None,
}
}
}