Skip to main content

ralph_workflow/json_parser/claude/
io.rs

1use std::cell::RefCell;
2use std::collections::{BTreeSet, HashMap};
3
4use crate::json_parser::streaming_state::StreamingSession;
5use crate::json_parser::terminal::TerminalMode;
6
7pub struct ParserState {
8    pub terminal_mode: RefCell<TerminalMode>,
9    pub streaming_session: std::rc::Rc<RefCell<StreamingSession>>,
10    pub thinking_active_index: RefCell<Option<u64>>,
11    pub thinking_non_tty_indices: RefCell<BTreeSet<u64>>,
12    pub suppress_thinking_for_message: RefCell<bool>,
13    pub text_line_active: RefCell<bool>,
14    pub cursor_up_active: RefCell<bool>,
15    pub last_rendered_content: RefCell<HashMap<String, String>>,
16}
17
18impl ParserState {
19    pub fn new(verbose_warnings: bool) -> Self {
20        let streaming_session = StreamingSession::new().with_verbose_warnings(verbose_warnings);
21        Self {
22            terminal_mode: RefCell::new(TerminalMode::detect()),
23            streaming_session: std::rc::Rc::new(RefCell::new(streaming_session)),
24            thinking_active_index: RefCell::new(None),
25            thinking_non_tty_indices: RefCell::new(BTreeSet::new()),
26            suppress_thinking_for_message: RefCell::new(false),
27            text_line_active: RefCell::new(false),
28            cursor_up_active: RefCell::new(false),
29            last_rendered_content: RefCell::new(HashMap::new()),
30        }
31    }
32
33    pub fn with_session_mut<R>(&self, f: impl FnOnce(&mut StreamingSession) -> R) -> R {
34        f(&mut self.streaming_session.borrow_mut())
35    }
36
37    pub fn with_cursor_up_active_mut<R>(&self, f: impl FnOnce(&mut bool) -> R) -> R {
38        f(&mut self.cursor_up_active.borrow_mut())
39    }
40
41    pub fn with_thinking_active_index_mut<R>(&self, f: impl FnOnce(&mut Option<u64>) -> R) -> R {
42        f(&mut self.thinking_active_index.borrow_mut())
43    }
44
45    pub fn with_thinking_non_tty_indices_mut<R>(
46        &self,
47        f: impl FnOnce(&mut BTreeSet<u64>) -> R,
48    ) -> R {
49        f(&mut self.thinking_non_tty_indices.borrow_mut())
50    }
51
52    pub fn with_suppress_thinking_for_message_mut<R>(&self, f: impl FnOnce(&mut bool) -> R) -> R {
53        f(&mut self.suppress_thinking_for_message.borrow_mut())
54    }
55
56    pub fn with_text_line_active_mut<R>(&self, f: impl FnOnce(&mut bool) -> R) -> R {
57        f(&mut self.text_line_active.borrow_mut())
58    }
59
60    pub fn with_last_rendered_content_mut<R>(
61        &self,
62        f: impl FnOnce(&mut HashMap<String, String>) -> R,
63    ) -> R {
64        f(&mut self.last_rendered_content.borrow_mut())
65    }
66
67    pub fn with_terminal_mode_mut<R>(&self, f: impl FnOnce(&mut TerminalMode) -> R) -> R {
68        f(&mut self.terminal_mode.borrow_mut())
69    }
70}