oxi_tui/content/
streaming.rs1pub type StreamId = u64;
2
3#[derive(Debug, Clone, Default)]
4pub struct StreamingState {
5 pub active_stream: Option<StreamId>,
6 pub partial_text: String,
7}
8
9impl StreamingState {
10 #[must_use]
11 pub const fn is_streaming(&self) -> bool {
12 self.active_stream.is_some()
13 }
14
15 pub fn start(&mut self, id: StreamId) {
16 self.active_stream = Some(id);
17 self.partial_text.clear();
18 }
19
20 pub fn push_token(&mut self, token: &str) {
21 self.partial_text.push_str(token);
22 }
23
24 pub fn finalize(&mut self) -> String {
25 self.active_stream = None;
26 std::mem::take(&mut self.partial_text)
27 }
28}
29
30#[cfg(test)]
31mod tests {
32 use super::StreamingState;
33
34 #[test]
35 fn streaming_lifecycle_resets_collects_and_finalizes_text() {
36 let mut state = StreamingState::default();
37 assert!(!state.is_streaming());
38
39 state.partial_text.push_str("stale");
40 state.start(42);
41 assert!(state.is_streaming());
42 assert_eq!(state.active_stream, Some(42));
43 assert!(state.partial_text.is_empty());
44
45 state.push_token("hel");
46 state.push_token("lo");
47 assert_eq!(state.finalize(), "hello");
48 assert!(!state.is_streaming());
49 assert!(state.partial_text.is_empty());
50 }
51}