Skip to main content

ic_query/nns/
render.rs

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