use crate::canonical::{ContentKind, Event};
use crate::protocol::{DecodeState, OpenBlock};
pub(crate) fn next_index(state: &DecodeState) -> u32 {
state.open.len() as u32
}
fn open_lazy(state: &mut DecodeState, out: &mut Vec<Event>, kind: ContentKind) -> u32 {
if let Some((i, _)) = state.open.iter().find(|(_, b)| b.kind == kind) {
return *i;
}
let i = next_index(state);
state.open.insert(i, OpenBlock { kind: kind.clone() });
out.push(Event::ContentStart { index: i, kind });
i
}
pub(crate) fn open_text(state: &mut DecodeState, out: &mut Vec<Event>) -> u32 {
open_lazy(state, out, ContentKind::Text {})
}
pub(crate) fn open_thinking(state: &mut DecodeState, out: &mut Vec<Event>) -> u32 {
open_lazy(state, out, ContentKind::Thinking { id: None }) }
pub(crate) fn drain(state: &mut DecodeState, out: &mut Vec<Event>) {
let mut open: Vec<u32> = state.open.keys().copied().collect();
open.sort_unstable();
for index in open {
state.open.remove(&index);
out.push(Event::ContentStop { index });
}
}