cuenv_events/renderers/
json.rs1#![allow(clippy::print_stdout)]
7
8use crate::bus::EventReceiver;
9use crate::event::CuenvEvent;
10
11#[derive(Debug, Default)]
13pub struct JsonRenderer {
14 pretty: bool,
16}
17
18impl JsonRenderer {
19 #[must_use]
21 pub fn new() -> Self {
22 Self { pretty: false }
23 }
24
25 #[must_use]
27 pub fn pretty() -> Self {
28 Self { pretty: true }
29 }
30
31 pub async fn run(self, mut receiver: EventReceiver) {
33 while let Some(event) = receiver.recv().await {
34 self.render(&event);
35 }
36 }
37
38 pub fn render(&self, event: &CuenvEvent) {
40 let json = if self.pretty {
41 serde_json::to_string_pretty(event)
42 } else {
43 serde_json::to_string(event)
44 };
45
46 if let Ok(json) = json {
47 println!("{json}");
48 }
49 }
50}