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
5#[cfg(feature = "nns-host")]
6use crate::text_value::yes_no;
7
8#[must_use]
9pub fn compact_text(value: &str, chars: usize) -> String {
10    terminal_text(value).chars().take(chars).collect()
11}
12
13#[must_use]
14pub fn text_or_dash(value: Option<&str>) -> String {
15    crate::text_value::text_or_dash(value.unwrap_or_default())
16}
17
18#[must_use]
19pub fn optional_f32_text(value: Option<f32>) -> String {
20    value.map_or_else(|| "-".to_string(), |value| value.to_string())
21}
22
23#[must_use]
24pub fn optional_node_count_text(value: Option<u32>) -> String {
25    value.map_or_else(|| "unknown".to_string(), |count| count.to_string())
26}
27
28#[cfg(feature = "nns-host")]
29#[must_use]
30/// Render a Registry inventory refresh publication outcome.
31pub fn nns_inventory_refresh_report_text(report: &crate::nns::NnsInventoryRefreshReport) -> String {
32    let mut lines = vec![
33        format!("network: {}", terminal_text(&report.network)),
34        format!("cache_path: {}", terminal_text(&report.cache_path)),
35        format!(
36            "refresh_lock_path: {}",
37            terminal_text(&report.refresh_lock_path)
38        ),
39    ];
40    if let Some(governance_canister_id) = &report.governance_canister_id {
41        lines.push(format!(
42            "governance_canister_id: {}",
43            terminal_text(governance_canister_id)
44        ));
45    }
46    lines.extend([
47        format!("registry_canister_id: {}", report.registry_canister_id),
48        format!("registry_version: {}", report.registry_version),
49        format!("fetched_at: {}", terminal_text(&report.fetched_at)),
50        format!(
51            "source_endpoint: {}",
52            terminal_text(&report.source_endpoint)
53        ),
54        format!("fetched_by: {}", terminal_text(&report.fetched_by)),
55        format!("dry_run: {}", yes_no(report.dry_run)),
56        format!("wrote_cache: {}", yes_no(report.wrote_cache)),
57        format!(
58            "replaced_existing_cache: {}",
59            yes_no(report.replaced_existing_cache)
60        ),
61        format!("collection: {}", terminal_text(&report.collection)),
62        format!("row_count: {}", report.row_count),
63    ]);
64    lines.join("\n")
65}
66
67#[must_use]
68pub(super) fn registry_provenance_lines(
69    registry_canister_id: &str,
70    registry_version: u64,
71    network: &str,
72    fetched_at: &str,
73    source_endpoint: &str,
74    fetched_by: &str,
75) -> [String; 6] {
76    [
77        format!("registry_canister_id: {registry_canister_id}"),
78        format!("registry_version: {registry_version}"),
79        format!("network: {}", terminal_text(network)),
80        format!("fetched_at: {}", terminal_text(fetched_at)),
81        format!("source_endpoint: {}", terminal_text(source_endpoint)),
82        format!("fetched_by: {}", terminal_text(fetched_by)),
83    ]
84}