ic-query-cli 0.104.1

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: text_value
//!
//! Responsibility: select Query's plain-table, optional-value and truncation policies.
//! Does not own: mechanical escaping, table alignment, JSON serialization or process output.
//! Boundary: delegates escaping/layout to Host while keeping report presentation choices local.

use ic_host_text::escape::terminal_text;
use ic_host_text::table::{Alignment, Options};
pub use ic_host_text::width::fit_line as truncate_text;
use std::{borrow::Cow, fmt::Display};

// Keep scalar labels beside selected fields without repeating their access paths.
macro_rules! report_fields {
    ($report:expr; $($name:tt $(= $value:expr)?),+ $(,)?) => {
        [$($crate::presentation::text_value::field($crate::presentation::text_value::report_fields!(@name $name), &$crate::presentation::text_value::report_fields!(@value $report, $name $(, $value)?))),+]
    };
    (@name $name:literal) => { $name };
    (@name $name:ident) => { stringify!($name) };
    (@value $report:expr, $name:tt, $value:expr) => { $value };
    (@value $report:expr, $name:ident) => { $report.$name };
}
pub(crate) use report_fields;

pub const REPORT_TABLE_OPTIONS: Options = Options {
    column_gap: 3,
    header_separator: true,
};

pub fn json_scalar_text(value: &serde_json::Value) -> String {
    match value {
        serde_json::Value::Null => "-".to_string(),
        serde_json::Value::String(value) => value.clone(),
        _ => value.to_string(),
    }
}

pub fn text_or_dash(value: &str) -> String {
    if value.is_empty() {
        "-".to_string()
    } else {
        terminal_text(value).into_owned()
    }
}

pub fn optional_text(value: Option<&String>) -> String {
    value.map_or_else(
        || "-".to_string(),
        |value| terminal_text(value).into_owned(),
    )
}

/// Escape and retain the selected prefix for compact identity columns.
pub fn compact_text(value: &str, chars: usize) -> String {
    terminal_text(value).chars().take(chars).collect()
}

pub fn optional_number_text(value: Option<impl Display>) -> String {
    value.map_or_else(|| "-".to_string(), |value| value.to_string())
}

pub fn optional_bool_text(value: Option<bool>) -> &'static str {
    value.map_or("-", yes_no)
}

pub const fn yes_no(value: bool) -> &'static str {
    if value { "yes" } else { "no" }
}

/// Append one titled report table, including the required section gap and empty placeholder.
pub fn push_titled_table_section(lines: &mut Vec<String>, title: &str, table: Option<String>) {
    push_section(lines, Some(title), table.unwrap_or_else(|| "-".to_string()));
}

/// Append a populated table with the section gap and optional report-owned title.
pub fn push_optional_table_section<const N: usize>(
    lines: &mut Vec<String>,
    title: Option<&str>,
    headers: &[&str; N],
    rows: impl Iterator<Item = [String; N]>,
    alignments: &[Alignment; N],
) {
    if let Some(table) = optional_table(headers, rows, alignments) {
        push_section(lines, title, table);
    }
}

/// Render the finalization outcome of a successfully published collection snapshot.

pub fn refresh_finalization_lines(error: Option<&str>) -> [String; 2] {
    [
        format!("attempt_finalized: {}", yes_no(error.is_none())),
        format!(
            "attempt_finalization_error: {}",
            terminal_text(error.unwrap_or("-"))
        ),
    ]
}

/// Render one Query-labelled record line with Host's terminal escaping.
pub fn field(name: &str, value: &dyn Display) -> String {
    escaped_line(format!("{name}: {value}"))
}

/// Escape one complete record line while retaining its existing allocation when safe.
pub fn escaped_line(line: String) -> String {
    match terminal_text(&line) {
        Cow::Borrowed(_) => line,
        Cow::Owned(escaped) => escaped,
    }
}

/// Render Query's report table style through Host.
pub fn table<const N: usize>(
    headers: &[impl AsRef<str>; N],
    rows: &[[impl AsRef<str>; N]],
    alignments: &[Alignment; N],
) -> String {
    ic_host_text::table::render(headers, rows, alignments, REPORT_TABLE_OPTIONS)
}

/// Render a populated report table; its caller owns the empty section's presentation.
pub fn optional_table<const N: usize>(
    headers: &[&str; N],
    rows: impl Iterator<Item = [String; N]>,
    alignments: &[Alignment; N],
) -> Option<String> {
    let rows = rows.collect::<Vec<_>>();
    (!rows.is_empty()).then(|| table(headers, &rows, alignments))
}

/// Summarize publication and attempt-finalization evidence in one stable order.

pub fn refresh_outcome_lines(wrote: bool, replaced: bool, error: Option<&str>) -> [String; 4] {
    let [finalized, error] = refresh_finalization_lines(error);
    [
        field("wrote_cache", &yes_no(wrote)),
        field("replaced_existing_cache", &yes_no(replaced)),
        finalized,
        error,
    ]
}

/// Identify the published cache and its coordination evidence.

pub fn refresh_path_lines(cache: &str, lock: &str, attempt: &str) -> [String; 3] {
    [
        field("cache_path", &cache),
        field("refresh_lock_path", &lock),
        field("refresh_attempt_path", &attempt),
    ]
}

/// Append a rendered section with Query's required separating blank line.
pub fn push_section(lines: &mut Vec<String>, title: Option<&str>, body: String) {
    lines.push(String::new());
    lines.extend(title.map(str::to_string));
    lines.push(body);
}