use sim_kernel::{Error, Result};
use crate::{BridgeBook, BridgePacket, decode_bridge_text, encode_bridge_text};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OwnedSpan {
Structural(String),
Frame {
id: String,
text: String,
},
Part {
id: String,
kind: String,
text: String,
},
Fence {
id: String,
text: String,
},
}
impl OwnedSpan {
pub fn rendered_text(&self) -> &str {
match self {
Self::Structural(text) => text,
Self::Frame { text, .. } | Self::Part { text, .. } | Self::Fence { text, .. } => text,
}
}
}
pub fn assert_roundtrip(packet: &BridgePacket, book: &BridgeBook) -> Result<()> {
let text = encode_bridge_text(packet, book)?;
let decoded = decode_bridge_text(&text, book)?;
if decoded != *packet {
return Err(Error::Eval("bridge roundtrip mismatch".to_owned()));
}
Ok(())
}
pub fn assert_total_ownership(rendered: &str, spans: &[OwnedSpan]) -> Result<()> {
let mut cursor = 0usize;
for span in spans {
let text = span.rendered_text();
if !rendered[cursor..].starts_with(text) {
return Err(Error::Eval(format!(
"bridge render has unowned bytes at {cursor}"
)));
}
cursor += text.len();
}
if cursor != rendered.len() {
return Err(Error::Eval(
"bridge render has unowned trailing bytes".to_owned(),
));
}
Ok(())
}