Skip to main content

qn/commands/stream/
render.rs

1//! Renderers for `qn stream …`.
2
3use comfy_table::Cell;
4use serde::Serialize;
5
6use crate::output::{new_table, opt_cell, set_header_bold, write_table, OutputCtx, Render};
7
8#[derive(Serialize)]
9pub(super) struct StreamsListView(pub quicknode_sdk::streams::ListStreamsResponse);
10
11impl Render for StreamsListView {
12    fn render_table(&self, w: &mut dyn std::io::Write, ctx: &OutputCtx) -> std::io::Result<()> {
13        let mut t = new_table(ctx);
14        set_header_bold(
15            &mut t,
16            ctx,
17            vec!["ID", "NAME", "STATUS", "NETWORK", "DATASET", "REGION"],
18        );
19        for s in &self.0.data {
20            t.add_row(vec![
21                Cell::new(&s.id),
22                Cell::new(&s.name),
23                Cell::new(&s.status),
24                Cell::new(&s.network),
25                Cell::new(&s.dataset),
26                Cell::new(&s.region),
27            ]);
28        }
29        write_table(w, &t)?;
30        crate::output::write_pagination_footer(
31            w,
32            self.0.page_info.offset,
33            self.0.data.len(),
34            self.0.page_info.total,
35        )
36    }
37}
38
39#[derive(Serialize)]
40pub(super) struct StreamView(pub quicknode_sdk::streams::Stream);
41
42impl Render for StreamView {
43    fn render_table(&self, w: &mut dyn std::io::Write, ctx: &OutputCtx) -> std::io::Result<()> {
44        let s = &self.0;
45        let mut t = new_table(ctx);
46        set_header_bold(&mut t, ctx, vec!["FIELD", "VALUE"]);
47        t.add_row(vec![Cell::new("id"), Cell::new(&s.id)]);
48        t.add_row(vec![Cell::new("name"), Cell::new(&s.name)]);
49        t.add_row(vec![Cell::new("status"), Cell::new(&s.status)]);
50        t.add_row(vec![Cell::new("network"), Cell::new(&s.network)]);
51        t.add_row(vec![Cell::new("dataset"), Cell::new(&s.dataset)]);
52        t.add_row(vec![Cell::new("region"), Cell::new(&s.region)]);
53        t.add_row(vec![Cell::new("start_range"), Cell::new(s.start_range)]);
54        t.add_row(vec![Cell::new("end_range"), Cell::new(s.end_range)]);
55        t.add_row(vec![Cell::new("created_at"), Cell::new(&s.created_at)]);
56        t.add_row(vec![Cell::new("updated_at"), Cell::new(&s.updated_at)]);
57        t.add_row(vec![Cell::new("plan"), opt_cell(&s.plan)]);
58        write_table(w, &t)
59    }
60}
61
62#[derive(Serialize)]
63pub(super) struct TestFilterView(pub quicknode_sdk::streams::TestFilterResponse);
64
65impl Render for TestFilterView {
66    fn render_table(&self, w: &mut dyn std::io::Write, _ctx: &OutputCtx) -> std::io::Result<()> {
67        if !self.0.logs.is_empty() {
68            writeln!(w, "== logs ==")?;
69            for line in &self.0.logs {
70                writeln!(w, "{line}")?;
71            }
72        }
73        writeln!(w, "== result ==")?;
74        writeln!(w, "{}", self.0.result)
75    }
76}
77
78impl Render for quicknode_sdk::streams::EnabledCountResponse {
79    fn render_table(&self, w: &mut dyn std::io::Write, _ctx: &OutputCtx) -> std::io::Result<()> {
80        writeln!(w, "{}", self.total)
81    }
82}