use rite_model::StepFact;
use crate::protocol::{Icon, UiSignal};
#[must_use]
pub fn fact_summary(fact: &StepFact) -> Option<(Icon, String)> {
match fact {
StepFact::CeremonyStarted { name, .. } => Some((Icon::Info, format!("Ceremony: {name}"))),
StepFact::ActStarted { label, .. } => Some((Icon::Info, format!("Act: {label}"))),
StepFact::StepStarted {
id,
label,
role_name,
..
} => Some((
Icon::Info,
format!("Step {label} ({id}), role: {role_name}"),
)),
StepFact::PromptAnswered { .. }
| StepFact::BackendOperation { .. }
| StepFact::AttestationRecorded { .. }
| StepFact::CeremonyCompleted { .. } => None,
StepFact::ArtifactWritten { path, .. } => Some((
Icon::Checkmark,
format!("Artifact written: {}", path.display()),
)),
StepFact::DeviationRecorded { text, .. } => {
Some((Icon::Warning, format!("Deviation: {text}")))
}
StepFact::StepCompleted { outcome, .. } => match outcome {
rite_model::StepOutcome::Completed { message } => {
Some((Icon::Checkmark, message.clone()))
}
_ => Some((Icon::Checkmark, "Step completed".to_string())),
},
StepFact::StepAttemptFailed { attempt, error, .. } => Some((
Icon::Cross,
format!("Attempt {attempt} failed: {}", error.message),
)),
StepFact::CeremonyFailed { error, .. } => {
Some((Icon::Cross, format!("Ceremony failed: {}", error.message)))
}
StepFact::EntropySeeded { source, .. } => {
Some((Icon::Info, format!("Entropy source seeded ({source})")))
}
StepFact::EntropyContributed { epoch, .. } => Some((
Icon::Info,
format!("Entropy contribution folded (epoch {epoch})"),
)),
StepFact::EntropyDrawn { path, .. } => {
Some((Icon::Info, format!("Random value drawn: {path}")))
}
_ => Some((Icon::Info, "unknown fact variant".to_string())),
}
}
#[must_use]
pub fn signal_summary(signal: &UiSignal) -> Option<(Icon, String)> {
match signal {
UiSignal::LogLine { icon, text, .. } => Some((*icon, text.clone())),
UiSignal::Progress {
phase, fraction, ..
} => {
let text = match fraction {
Some(f) => format!("{phase}: {:>5.1}%", f * 100.0),
None => format!("{phase}…"),
};
Some((Icon::Spinner, text))
}
UiSignal::CeremonyOverview { .. } | UiSignal::SystemInfo(_) | UiSignal::Environment(_) => {
None
}
}
}
#[must_use]
pub fn truncate_for_display(s: &str, max_chars: usize) -> String {
match s.char_indices().nth(max_chars) {
Some((boundary, _)) => format!("{}...", &s[..boundary]),
None => s.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::truncate_for_display;
#[test]
fn truncate_passes_through_short_input() {
assert_eq!(truncate_for_display("short", 10), "short");
}
#[test]
fn truncate_adds_ellipsis_when_too_long() {
assert_eq!(
truncate_for_display("this is a long string", 10),
"this is a ..."
);
}
#[test]
fn truncate_respects_utf8_code_point_boundaries() {
let s = "🦀🦀🦀🦀🦀";
assert_eq!(truncate_for_display(s, 2), "🦀🦀...");
assert_eq!(truncate_for_display(s, 5), "🦀🦀🦀🦀🦀");
}
}