Skip to main content

qn/commands/webhook/
render.rs

1//! Renderers for `qn webhook …`.
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 WebhooksListView(pub quicknode_sdk::webhooks::ListWebhooksResponse);
10
11impl Render for WebhooksListView {
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", "TEMPLATE"],
18        );
19        for h in &self.0.data {
20            t.add_row(vec![
21                Cell::new(&h.id),
22                Cell::new(&h.name),
23                Cell::new(&h.status),
24                Cell::new(&h.network),
25                opt_cell(&h.template_id),
26            ]);
27        }
28        write_table(w, &t)?;
29        crate::output::write_pagination_footer(
30            w,
31            self.0.page_info.offset,
32            self.0.data.len(),
33            self.0.page_info.total,
34        )
35    }
36}
37
38#[derive(Serialize)]
39pub(super) struct WebhookView(pub quicknode_sdk::webhooks::Webhook);
40
41impl Render for WebhookView {
42    fn render_table(&self, w: &mut dyn std::io::Write, ctx: &OutputCtx) -> std::io::Result<()> {
43        let h = &self.0;
44        let mut t = new_table(ctx);
45        set_header_bold(&mut t, ctx, vec!["FIELD", "VALUE"]);
46        t.add_row(vec![Cell::new("id"), Cell::new(&h.id)]);
47        t.add_row(vec![Cell::new("name"), Cell::new(&h.name)]);
48        t.add_row(vec![Cell::new("status"), Cell::new(&h.status)]);
49        t.add_row(vec![Cell::new("network"), Cell::new(&h.network)]);
50        t.add_row(vec![Cell::new("template_id"), opt_cell(&h.template_id)]);
51        t.add_row(vec![Cell::new("created_at"), Cell::new(&h.created_at)]);
52        t.add_row(vec![Cell::new("updated_at"), opt_cell(&h.updated_at)]);
53        t.add_row(vec![
54            Cell::new("notification_email"),
55            opt_cell(&h.notification_email),
56        ]);
57        if let Some(d) = &h.destination_attributes {
58            t.add_row(vec![Cell::new("destination_attributes"), Cell::new(d)]);
59        }
60        write_table(w, &t)
61    }
62}
63
64impl Render for quicknode_sdk::webhooks::WebhookEnabledCountResponse {
65    fn render_table(&self, w: &mut dyn std::io::Write, _ctx: &OutputCtx) -> std::io::Result<()> {
66        writeln!(w, "{}", self.total)
67    }
68}