1use 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 crate::text_value::compact_text(value, chars)
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]
30pub fn nns_inventory_refresh_report_text(report: &crate::nns::NnsInventoryRefreshReport) -> String {
32 let [
33 registry_id,
34 version,
35 network,
36 fetched_at,
37 endpoint,
38 fetched_by,
39 ] = registry_provenance_lines(&report.provenance);
40 let mut lines = vec![
41 network,
42 format!("cache_path: {}", terminal_text(&report.cache_path)),
43 format!(
44 "refresh_lock_path: {}",
45 terminal_text(&report.refresh_lock_path)
46 ),
47 ];
48 if let Some(governance_canister_id) = &report.governance_canister_id {
49 lines.push(format!(
50 "governance_canister_id: {}",
51 terminal_text(governance_canister_id)
52 ));
53 }
54 lines.extend([
55 registry_id,
56 version,
57 fetched_at,
58 endpoint,
59 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!("collection: {}", terminal_text(&report.collection)),
67 format!("row_count: {}", report.row_count),
68 ]);
69 lines.join("\n")
70}
71
72#[must_use]
73pub(super) fn registry_provenance_lines(
74 provenance: &super::registry::NnsRegistryInventoryProvenance,
75) -> [String; 6] {
76 [
77 format!("registry_canister_id: {}", provenance.registry_canister_id),
78 format!("registry_version: {}", provenance.registry_version),
79 format!("network: {}", terminal_text(&provenance.network)),
80 format!("fetched_at: {}", terminal_text(&provenance.fetched_at)),
81 format!(
82 "source_endpoint: {}",
83 terminal_text(&provenance.source_endpoint)
84 ),
85 format!("fetched_by: {}", terminal_text(&provenance.fetched_by)),
86 ]
87}