Skip to main content

ic_query/nns/
render.rs

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