Skip to main content

heddle_cli_render/cli/render/
query.rs

1// SPDX-License-Identifier: Apache-2.0
2use anyhow::{Context, Result};
3use chrono::{TimeZone, Utc};
4use heddle_core::QueryReport;
5
6use crate::cli::render::write_stdout;
7
8pub fn query_json(report: &QueryReport) -> Result<()> {
9    let mut text = serde_json::to_string(report).context("serialize query output")?;
10    text.push('\n');
11    write_stdout(&text)
12}
13
14pub fn query_text(report: &QueryReport) -> Result<()> {
15    let mut text = String::new();
16    if report.hits.is_empty() {
17        text.push_str("(no matches)\n");
18    } else {
19        for hit in &report.hits {
20            let ts = Utc
21                .timestamp_opt(hit.timestamp_secs, 0)
22                .single()
23                .map(|d| d.to_rfc3339())
24                .unwrap_or_else(|| hit.timestamp_secs.to_string());
25            text.push_str(&format!(
26                "#{} {} {} <{}>",
27                hit.seq, ts, hit.verb, hit.actor_email
28            ));
29            if let Some(thread) = &hit.thread {
30                text.push_str(&format!(" thread={thread}"));
31            }
32            if let Some(state_id) = &hit.state_id {
33                text.push_str(&format!(" -> {state_id}"));
34            }
35            text.push('\n');
36        }
37    }
38    write_stdout(&text)
39}