use super::super::{SnsNeuronPermissionList, SnsTokenMetadataRow};
use crate::{
duration::display_duration_seconds,
nns::render::yes_no,
token_amount::{base_units_decimal_text, e8s_decimal_text},
};
use serde_json::Value as JsonValue;
pub(super) fn optional_text(value: Option<&String>) -> &str {
value.map_or("-", String::as_str)
}
pub(super) fn optional_e8s_text(value: Option<u64>) -> String {
value.map_or_else(|| "-".to_string(), e8s_decimal_text)
}
pub(in crate::sns::report) fn optional_e8s_decimal_text(value: Option<u64>) -> String {
value.map_or_else(|| "-".to_string(), e8s_decimal_text)
}
pub(super) fn optional_duration_text(value: Option<u64>) -> String {
value.map_or_else(|| "-".to_string(), display_duration_seconds)
}
pub(super) fn optional_percentage_text(value: Option<u64>) -> String {
value.map_or_else(|| "-".to_string(), |value| format!("{value}%"))
}
pub(super) fn optional_basis_points_text(value: Option<u64>) -> String {
value.map_or_else(|| "-".to_string(), basis_points_text)
}
pub(super) fn optional_u64_text(value: Option<u64>) -> String {
value.map_or_else(|| "-".to_string(), |value| value.to_string())
}
pub(super) fn optional_u32_text(value: Option<u32>) -> String {
value.map_or_else(|| "-".to_string(), |value| value.to_string())
}
pub(super) fn optional_bool_text(value: Option<bool>) -> String {
value.map_or_else(|| "-".to_string(), |value| yes_no(value).to_string())
}
pub(super) fn optional_permissions_text(value: Option<&SnsNeuronPermissionList>) -> String {
value.map_or_else(
|| "-".to_string(),
|permissions| {
permissions
.permissions
.iter()
.map(i32::to_string)
.collect::<Vec<_>>()
.join(",")
},
)
}
pub(super) fn comma_join_u64(values: &[u64]) -> String {
if values.is_empty() {
return "-".to_string();
}
values
.iter()
.map(u64::to_string)
.collect::<Vec<_>>()
.join(",")
}
pub(super) fn truncate_text_value(value: &str, limit: usize) -> String {
if value.chars().count() <= limit {
return value.to_string();
}
let mut truncated = value.chars().take(limit).collect::<String>();
truncated.push_str("...");
truncated
}
pub(super) fn token_metadata_value_text(row: &SnsTokenMetadataRow, decimals: u8) -> String {
let value = metadata_value_text(&row.value);
if row.key == "icrc1:fee" {
base_units_decimal_text(&value, decimals)
} else {
value
}
}
fn basis_points_text(value: u64) -> String {
let whole = value / 100;
let fractional = value % 100;
format!("{whole}.{fractional:02}%")
}
fn metadata_value_text(value: &JsonValue) -> String {
match value {
JsonValue::String(value) => value.clone(),
JsonValue::Bool(value) => value.to_string(),
JsonValue::Number(value) => value.to_string(),
JsonValue::Null => "-".to_string(),
JsonValue::Array(_) | JsonValue::Object(_) => value.to_string(),
}
}