use serde::{Deserialize, Serialize};
use super::{Item, Position};
use crate::conversation::turn_records::{DraftCheck, Refusal, UnitDigest};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Current {
records: u64,
bytes: u64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Placed {
New,
Repeat,
}
#[derive(Debug, PartialEq, Eq)]
pub enum StreamRefusal {
Gap,
Invalid(String),
Bound { bytes: u64, maximum: u64 },
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct RestoreStream {
position: Position,
current: Option<Current>,
check: DraftCheck,
digest: UnitDigest,
}
fn invalid(why: String) -> StreamRefusal {
StreamRefusal::Invalid(why)
}
impl RestoreStream {
pub fn position(&self) -> Position {
self.position
}
pub fn piece_complete(&self) -> bool {
self.check.piece_complete()
}
pub fn digest(&self) -> String {
self.digest.clone().finish()
}
pub fn admit(&mut self, item: &Item<'_>, maximum: u64) -> Result<Placed, StreamRefusal> {
let at = self.position;
match *item {
Item::Header(header) => {
if header.turn <= at.turn {
return Ok(Placed::Repeat);
}
if at.projections > 0 || at.redactions > 0 {
return Err(invalid(format!(
"turn {}: turns come before projections and trail acts",
header.turn
)));
}
if !at.complete {
return Err(StreamRefusal::Gap);
}
if header.records == 0 && header.bytes != 0 {
return Err(invalid(format!(
"turn {}: a turn without records holds no bytes",
header.turn
)));
}
if header.bytes > maximum {
let bytes = header.bytes;
return Err(StreamRefusal::Bound { bytes, maximum });
}
self.position = Position {
turn: header.turn,
seq: 0,
complete: header.records == 0,
..at
};
self.current = Some(Current {
records: header.records,
bytes: header.bytes,
});
self.check = DraftCheck::default();
self.digest.header(header);
}
Item::Record(turn, record) => {
if turn < at.turn || (turn == at.turn && record.seq < at.seq) {
return Ok(Placed::Repeat);
}
let current = self.current.filter(|_| turn == at.turn);
let Some(current) = current.filter(|_| record.seq == at.seq) else {
return Err(StreamRefusal::Gap);
};
if at.complete {
return Err(invalid(format!(
"turn {turn}: record {} is past its header's {} records",
record.seq, current.records
)));
}
self.check
.admit(record, Some(current.bytes), maximum)
.map_err(|refusal| match refusal {
Refusal::Invalid(why) => invalid(format!("turn {turn}: {why}")),
Refusal::Stated { bytes, .. } => invalid(format!(
"turn {turn}: its records reach {bytes} bytes, past its header's {}",
current.bytes
)),
Refusal::Bound { bytes, maximum } => {
StreamRefusal::Bound { bytes, maximum }
}
})?;
let seq = at
.seq
.checked_add(1)
.ok_or_else(|| invalid("seq overflows".into()))?;
let complete = seq == current.records;
if complete {
self.check
.sealable()
.map_err(|why| invalid(format!("turn {turn}: {why}")))?;
if self.check.bytes() != current.bytes {
return Err(invalid(format!(
"turn {turn}: its records hold {} bytes where its header says {}",
self.check.bytes(),
current.bytes
)));
}
}
self.position = Position {
seq,
complete,
..at
};
self.digest.record(turn, record);
}
Item::Projection(index, entry) => {
if index < at.projections {
return Ok(Placed::Repeat);
}
if index > at.projections || !at.complete {
return Err(StreamRefusal::Gap);
}
if at.redactions > 0 {
return Err(invalid("projections come before trail acts".into()));
}
self.position.projections = index + 1;
self.digest.projection(entry);
}
Item::Act(index, act) => {
if index < at.redactions {
return Ok(Placed::Repeat);
}
if index > at.redactions || !at.complete {
return Err(StreamRefusal::Gap);
}
self.position.redactions = index + 1;
self.digest.act(act);
}
}
Ok(Placed::New)
}
}