ralph_workflow/json_parser/gemini/
parser.rs1pub struct GeminiParser {
3 colors: Colors,
4 verbosity: Verbosity,
5 log_path: Option<std::path::PathBuf>,
7 display_name: String,
8 streaming_session: Rc<RefCell<StreamingSession>>,
10 terminal_mode: RefCell<TerminalMode>,
12 last_rendered_content: RefCell<std::collections::HashMap<String, String>>,
14 show_streaming_metrics: bool,
16 printer: SharedPrinter,
18}
19
20impl GeminiParser {
21 pub(crate) fn new(colors: Colors, verbosity: Verbosity) -> Self {
22 Self::with_printer(colors, verbosity, super::printer::shared_stdout())
23 }
24
25 pub(crate) fn with_printer(
27 colors: Colors,
28 verbosity: Verbosity,
29 printer: SharedPrinter,
30 ) -> Self {
31 let verbose_warnings = matches!(verbosity, Verbosity::Debug);
32 let streaming_session = StreamingSession::new().with_verbose_warnings(verbose_warnings);
33
34 let _printer_is_terminal = printer.borrow().is_terminal();
36
37 Self {
38 colors,
39 verbosity,
40 log_path: None,
41 display_name: "Gemini".to_string(),
42 streaming_session: Rc::new(RefCell::new(streaming_session)),
43 terminal_mode: RefCell::new(TerminalMode::detect()),
44 last_rendered_content: RefCell::new(std::collections::HashMap::new()),
45 show_streaming_metrics: false,
46 printer,
47 }
48 }
49
50 pub(crate) const fn with_show_streaming_metrics(mut self, show: bool) -> Self {
51 self.show_streaming_metrics = show;
52 self
53 }
54
55 pub(crate) fn with_display_name(mut self, display_name: &str) -> Self {
56 self.display_name = display_name.to_string();
57 self
58 }
59
60 pub(crate) fn with_log_file(mut self, path: &str) -> Self {
61 self.log_path = Some(std::path::PathBuf::from(path));
62 self
63 }
64
65 #[cfg(any(test, feature = "test-utils"))]
66 #[must_use]
67 pub fn with_terminal_mode(self, mode: TerminalMode) -> Self {
68 *self.terminal_mode.borrow_mut() = mode;
69 self
70 }
71
72 #[cfg(feature = "test-utils")]
81 pub fn with_printer_for_test(
82 colors: Colors,
83 verbosity: Verbosity,
84 printer: SharedPrinter,
85 ) -> Self {
86 Self::with_printer(colors, verbosity, printer).with_terminal_mode(TerminalMode::Full)
87 }
88
89 #[cfg(feature = "test-utils")]
93 #[must_use]
94 pub fn with_log_file_for_test(mut self, path: &str) -> Self {
95 self.log_path = Some(std::path::PathBuf::from(path));
96 self
97 }
98
99 #[cfg(feature = "test-utils")]
107 pub fn parse_stream_for_test<R: std::io::BufRead>(
108 &self,
109 reader: R,
110 workspace: &dyn crate::workspace::Workspace,
111 ) -> std::io::Result<()> {
112 self.parse_stream(reader, workspace)
113 }
114
115 #[cfg(feature = "test-utils")]
121 pub fn printer(&self) -> SharedPrinter {
122 Rc::clone(&self.printer)
123 }
124
125 #[cfg(feature = "test-utils")]
130 pub fn streaming_metrics(&self) -> StreamingQualityMetrics {
131 self.streaming_session
132 .borrow()
133 .get_streaming_quality_metrics()
134 }
135}