Skip to main content

qn/commands/kv/
render.rs

1//! Renderers for `qn kv …`.
2
3use comfy_table::Cell;
4use serde::Serialize;
5
6use crate::output::{new_table, set_header_bold, write_table, OutputCtx, Render};
7
8#[derive(Serialize)]
9pub(super) struct SetsView(pub quicknode_sdk::GetSetsResponse);
10
11impl Render for SetsView {
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(&mut t, ctx, vec!["KEY", "VALUE"]);
15        for e in &self.0.data {
16            t.add_row(vec![Cell::new(&e.key), Cell::new(&e.value)]);
17        }
18        write_table(w, &t)?;
19        if !self.0.cursor.is_empty() {
20            writeln!(w, "next: --cursor {}", self.0.cursor)?;
21        }
22        Ok(())
23    }
24}
25
26#[derive(Serialize)]
27pub(super) struct ListsView(pub quicknode_sdk::GetListsResponse);
28
29impl Render for ListsView {
30    fn render_table(&self, w: &mut dyn std::io::Write, ctx: &OutputCtx) -> std::io::Result<()> {
31        let mut t = new_table(ctx);
32        set_header_bold(&mut t, ctx, vec!["KEY"]);
33        for k in &self.0.data.keys {
34            t.add_row(vec![Cell::new(k)]);
35        }
36        write_table(w, &t)?;
37        if !self.0.cursor.is_empty() {
38            writeln!(w, "next: --cursor {}", self.0.cursor)?;
39        }
40        Ok(())
41    }
42}
43
44#[derive(Serialize)]
45pub(super) struct ListView(pub quicknode_sdk::GetListResponse);
46
47impl Render for ListView {
48    fn render_table(&self, w: &mut dyn std::io::Write, _ctx: &OutputCtx) -> std::io::Result<()> {
49        for item in &self.0.data.items {
50            writeln!(w, "{item}")?;
51        }
52        if !self.0.cursor.is_empty() {
53            writeln!(w, "next: --cursor {}", self.0.cursor)?;
54        }
55        Ok(())
56    }
57}
58
59impl Render for quicknode_sdk::GetSetResponse {
60    fn render_table(&self, w: &mut dyn std::io::Write, _ctx: &OutputCtx) -> std::io::Result<()> {
61        writeln!(w, "{}", self.value)
62    }
63}
64
65impl Render for quicknode_sdk::ListContainsItemResponse {
66    fn render_table(&self, w: &mut dyn std::io::Write, _ctx: &OutputCtx) -> std::io::Result<()> {
67        writeln!(w, "{}", self.exists)
68    }
69}