ic-query-cli 0.102.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: human_quantity
//!
//! Responsibility: format exact cycle and byte counts for human-facing text.
//! Does not own: report fields, JSON serialization, or terminal styling.
//! Boundary: uses decimal cycle units and binary IEC byte units with bounded precision.

use ic_host_text::escape::terminal_text;
use ic_host_text::quantity::{scaled_decimal_quantity_text, scaled_quantity_text};

const CYCLE_UNITS: &[(u64, &str)] = &[
    (1_000_000_000_000_000_000, "E"),
    (1_000_000_000_000_000, "P"),
    (1_000_000_000_000, "T"),
    (1_000_000_000, "B"),
    (1_000_000, "M"),
    (1_000, "k"),
    (1, ""),
];
const BYTE_UNITS: &[(u64, &str)] = &[
    (1_u64 << 60, "EiB"),
    (1_u64 << 50, "PiB"),
    (1_u64 << 40, "TiB"),
    (1_u64 << 30, "GiB"),
    (1_u64 << 20, "MiB"),
    (1_u64 << 10, "KiB"),
    (1, "B"),
];

pub fn cycle_count_text(value: u128) -> String {
    scaled_quantity_text(value, CYCLE_UNITS).expect("cycle unit divisors form a valid scale")
}

pub fn decimal_cycle_count_text(value: &str) -> String {
    value
        .parse::<u128>()
        .map_or_else(|_| terminal_text(value).into_owned(), cycle_count_text)
}

pub fn decimal_cycle_rate_text(value: &str) -> String {
    scaled_decimal_quantity_text(value, CYCLE_UNITS)
        .unwrap_or_else(|| terminal_text(value).into_owned())
}

pub fn byte_count_text(value: u128) -> String {
    scaled_quantity_text(value, BYTE_UNITS).expect("byte unit divisors form a valid scale")
}

#[cfg(test)]
mod tests;