use super::trail::Trail;
use crate::conversation::turn_records::{Header, Position, TurnRecord, UnitDigest, each_record};
use crate::conversation::{Error, Projection, Redaction, Turn};
use crate::state::State;
pub(super) enum Piece {
Header(Header),
Record(u64, TurnRecord),
Projection(u64, Projection),
Act(u64, Redaction),
}
pub(super) struct Walked {
pub turns: usize,
pub projections: usize,
pub redactions: usize,
pub digest: String,
}
pub(super) type Visit<'v> = dyn FnMut(Piece, Position) -> Result<(), Error> + 'v;
pub(super) fn place(at: Position) -> (u64, u64, u64, u64) {
(at.turn, at.seq, at.projections, at.redactions)
}
pub(super) fn walk(
source: &dyn State,
trail: &Trail<'_>,
id: &str,
visit: &mut Visit<'_>,
) -> Result<Walked, Error> {
let mut digest = UnitDigest::default();
let mut at = Position::default();
let mut turns = 0;
source.read_turns_each(id, 0, None, &mut |turn| {
if turns > 0 && turn.turn <= at.turn {
return Err(Error::Value(format!(
"conversation {id}: turn {} follows turn {} in the archive, and a restore takes \
turns ascending",
turn.turn, at.turn
)));
}
turns += 1;
let header = header_of(id, &turn)?;
let records = header.records;
digest.header(&header);
at = Position {
turn: turn.turn,
seq: 0,
complete: records == 0,
..Position::default()
};
visit(Piece::Header(header), at)?;
let mut failed = None;
let converted = each_record(&turn.messages, &mut |record| {
if failed.is_some() {
return;
}
digest.record(turn.turn, &record);
at.seq = record.seq + 1;
at.complete = at.seq == records;
if let Err(error) = visit(Piece::Record(turn.turn, record), at) {
failed = Some(error);
}
});
converted.map_err(|why| unfit(id, turn.turn, &why))?;
failed.map_or(Ok(true), Err)
})?;
let projections = source.read_projections(id, 0, None)?;
let counted = projections.len();
for (index, entry) in projections.into_iter().enumerate() {
digest.projection(&entry);
at.projections = index as u64 + 1;
visit(Piece::Projection(index as u64, entry), at)?;
}
trail.each_page(id, &mut |page| {
for act in page {
digest.act(&act);
let index = at.redactions;
at.redactions += 1;
visit(Piece::Act(index, act), at)?;
}
Ok(())
})?;
Ok(Walked {
turns,
projections: counted,
redactions: at.redactions as usize,
digest: digest.finish(),
})
}
fn header_of(id: &str, turn: &Turn) -> Result<Header, Error> {
let (mut records, mut bytes) = (0_u64, 0_u64);
if !turn.redacted {
each_record(&turn.messages, &mut |record| {
records += 1;
bytes += record.text.len() as u64;
})
.map_err(|why| unfit(id, turn.turn, &why))?;
}
Ok(Header {
turn: turn.turn,
created_at: turn.created_at,
actor: turn.actor.clone(),
redacted: turn.redacted,
records,
bytes,
})
}
fn unfit(id: &str, turn: u64, why: &str) -> Error {
Error::Value(format!(
"conversation {id}: turn {turn} cannot be restored in parts: {why}"
))
}