Skip to main content

ic_query/nns/
render.rs

1//! Shared NNS text-rendering primitives for inventory and refresh reports.
2
3use ic_host_text::escape::terminal_text;
4
5use crate::text_value::yes_no;
6
7#[must_use]
8pub fn compact_text(value: &str, chars: usize) -> String {
9    terminal_text(value).chars().take(chars).collect()
10}
11
12#[must_use]
13pub fn text_or_dash(value: Option<&str>) -> String {
14    value.filter(|text| !text.is_empty()).map_or_else(
15        || "-".to_string(),
16        |value| terminal_text(value).into_owned(),
17    )
18}
19
20#[must_use]
21pub fn optional_f32_text(value: Option<f32>) -> String {
22    value.map_or_else(|| "-".to_string(), |value| value.to_string())
23}
24
25#[must_use]
26pub fn optional_node_count_text(value: Option<u32>) -> String {
27    value.map_or_else(|| "unknown".to_string(), |count| count.to_string())
28}
29
30///
31/// NnsLeafRefreshText
32///
33/// Shared fields rendered for an NNS inventory refresh result.
34///
35
36#[derive(Clone, Copy, Debug, Eq, PartialEq)]
37pub struct NnsLeafRefreshText<'a> {
38    pub network: &'a str,
39    pub cache_path: &'a str,
40    pub refresh_lock_path: &'a str,
41    pub governance_canister_id: Option<&'a str>,
42    pub registry_canister_id: &'a str,
43    pub registry_version: u64,
44    pub fetched_at: &'a str,
45    pub source_endpoint: &'a str,
46    pub fetched_by: &'a str,
47    pub dry_run: bool,
48    pub wrote_cache: bool,
49    pub replaced_existing_cache: bool,
50    pub count_label: &'a str,
51    pub count: usize,
52}
53
54#[must_use]
55pub fn nns_leaf_refresh_report_text(report: NnsLeafRefreshText<'_>) -> String {
56    let mut lines = vec![
57        format!("network: {}", terminal_text(report.network)),
58        format!("cache_path: {}", terminal_text(report.cache_path)),
59        format!(
60            "refresh_lock_path: {}",
61            terminal_text(report.refresh_lock_path)
62        ),
63    ];
64    if let Some(governance_canister_id) = report.governance_canister_id {
65        lines.push(format!(
66            "governance_canister_id: {}",
67            terminal_text(governance_canister_id)
68        ));
69    }
70    lines.extend([
71        format!("registry_canister_id: {}", report.registry_canister_id),
72        format!("registry_version: {}", report.registry_version),
73        format!("fetched_at: {}", terminal_text(report.fetched_at)),
74        format!("source_endpoint: {}", terminal_text(report.source_endpoint)),
75        format!("fetched_by: {}", terminal_text(report.fetched_by)),
76        format!("dry_run: {}", yes_no(report.dry_run)),
77        format!("wrote_cache: {}", yes_no(report.wrote_cache)),
78        format!(
79            "replaced_existing_cache: {}",
80            yes_no(report.replaced_existing_cache)
81        ),
82        format!("{}: {}", terminal_text(report.count_label), report.count),
83    ]);
84    lines.join("\n")
85}