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