Skip to main content

ralph_workflow/json_parser/gemini/
parser.rs

1use super::printer::{shared_stdout, Printable, SharedPrinter};
2use io::GeminiParserState;
3
4/// Gemini event parser
5pub struct GeminiParser {
6    colors: Colors,
7    verbosity: Verbosity,
8    log_path: Option<std::path::PathBuf>,
9    display_name: String,
10    state: GeminiParserState,
11    show_streaming_metrics: bool,
12    printer: SharedPrinter,
13}
14
15impl GeminiParser {
16    pub(crate) fn new(colors: Colors, verbosity: Verbosity) -> Self {
17        Self::with_printer(colors, verbosity, shared_stdout())
18    }
19
20    pub(crate) fn with_printer(
21        colors: Colors,
22        verbosity: Verbosity,
23        printer: SharedPrinter,
24    ) -> Self {
25        let verbose_warnings = matches!(verbosity, Verbosity::Debug);
26
27        Self {
28            colors,
29            verbosity,
30            log_path: None,
31            display_name: "Gemini".to_string(),
32            state: GeminiParserState::new(verbose_warnings),
33            show_streaming_metrics: false,
34            printer,
35        }
36    }
37
38    pub(crate) const fn with_show_streaming_metrics(mut self, show: bool) -> Self {
39        self.show_streaming_metrics = show;
40        self
41    }
42
43    pub(crate) fn with_display_name(mut self, display_name: &str) -> Self {
44        self.display_name = display_name.to_string();
45        self
46    }
47
48    pub(crate) fn with_log_file(mut self, path: &str) -> Self {
49        self.log_path = Some(std::path::PathBuf::from(path));
50        self
51    }
52
53    #[cfg(any(test, feature = "test-utils"))]
54    #[must_use]
55    pub fn with_terminal_mode(self, mode: crate::json_parser::TerminalMode) -> Self {
56        *self.state.terminal_mode.borrow_mut() = mode;
57        self
58    }
59
60    #[cfg(feature = "test-utils")]
61    pub fn with_printer_for_test(
62        colors: Colors,
63        verbosity: Verbosity,
64        printer: SharedPrinter,
65    ) -> Self {
66        Self::with_printer(colors, verbosity, printer)
67            .with_terminal_mode(crate::json_parser::TerminalMode::Full)
68    }
69
70    #[cfg(feature = "test-utils")]
71    #[must_use]
72    pub fn with_log_file_for_test(mut self, path: &str) -> Self {
73        self.log_path = Some(std::path::PathBuf::from(path));
74        self
75    }
76
77    #[cfg(feature = "test-utils")]
78    pub fn parse_stream_for_test<R: std::io::BufRead>(
79        &mut self,
80        reader: R,
81        workspace: &dyn crate::workspace::Workspace,
82    ) -> std::io::Result<()> {
83        self.parse_stream(reader, workspace)
84    }
85
86    #[cfg(feature = "test-utils")]
87    pub fn printer(&self) -> SharedPrinter {
88        self.printer.clone()
89    }
90
91    pub(crate) fn with_printer_mut<R>(&mut self, f: impl FnOnce(&mut dyn Printable) -> R) -> R {
92        let mut printer_ref = self.printer.borrow_mut();
93        f(&mut *printer_ref)
94    }
95
96    #[cfg(feature = "test-utils")]
97    pub fn streaming_metrics(&self) -> crate::json_parser::health::StreamingQualityMetrics {
98        self.state
99            .streaming_session
100            .borrow()
101            .get_streaming_quality_metrics()
102    }
103}