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#[derive(Clone, Copy, Debug, Eq, PartialEq)]
26pub struct NnsLeafRefreshText<'a> {
27    pub network: &'a str,
28    pub cache_path: &'a str,
29    pub refresh_lock_path: &'a str,
30    pub governance_canister_id: Option<&'a str>,
31    pub registry_canister_id: &'a str,
32    pub registry_version: u64,
33    pub fetched_at: &'a str,
34    pub source_endpoint: &'a str,
35    pub fetched_by: &'a str,
36    pub dry_run: bool,
37    pub wrote_cache: bool,
38    pub replaced_existing_cache: bool,
39    pub count_label: &'a str,
40    pub count: usize,
41}
42
43#[must_use]
44pub fn nns_leaf_refresh_report_text(report: NnsLeafRefreshText<'_>) -> String {
45    let mut lines = vec![
46        format!("network: {}", sanitize_text(report.network)),
47        format!("cache_path: {}", sanitize_text(report.cache_path)),
48        format!(
49            "refresh_lock_path: {}",
50            sanitize_text(report.refresh_lock_path)
51        ),
52    ];
53    if let Some(governance_canister_id) = report.governance_canister_id {
54        lines.push(format!(
55            "governance_canister_id: {}",
56            sanitize_text(governance_canister_id)
57        ));
58    }
59    lines.extend([
60        format!("registry_canister_id: {}", report.registry_canister_id),
61        format!("registry_version: {}", report.registry_version),
62        format!("fetched_at: {}", sanitize_text(report.fetched_at)),
63        format!("source_endpoint: {}", sanitize_text(report.source_endpoint)),
64        format!("fetched_by: {}", sanitize_text(report.fetched_by)),
65        format!("dry_run: {}", yes_no(report.dry_run)),
66        format!("wrote_cache: {}", yes_no(report.wrote_cache)),
67        format!(
68            "replaced_existing_cache: {}",
69            yes_no(report.replaced_existing_cache)
70        ),
71        format!("{}: {}", sanitize_text(report.count_label), report.count),
72    ]);
73    lines.join("\n")
74}