Skip to main content

ic_query/nns/
render.rs

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