1use rite_model::StepFact;
15
16use crate::protocol::{Icon, UiSignal};
17
18#[must_use]
27pub fn fact_summary(fact: &StepFact) -> Option<(Icon, String)> {
28 match fact {
29 StepFact::CeremonyStarted { name, .. } => Some((Icon::Info, format!("Ceremony: {name}"))),
30 StepFact::ActStarted { label, .. } => Some((Icon::Info, format!("Act: {label}"))),
31 StepFact::StepStarted {
32 id,
33 label,
34 role_name,
35 ..
36 } => Some((
37 Icon::Info,
38 format!("Step {label} ({id}), role: {role_name}"),
39 )),
40 StepFact::PromptAnswered { .. }
41 | StepFact::BackendOperation { .. }
42 | StepFact::AttestationRecorded { .. }
43 | StepFact::CeremonyCompleted { .. } => None,
44 StepFact::ArtifactWritten { path, .. } => Some((
45 Icon::Checkmark,
46 format!("Artifact written: {}", path.display()),
47 )),
48 StepFact::DeviationRecorded { text, .. } => {
49 Some((Icon::Warning, format!("Deviation: {text}")))
50 }
51 StepFact::StepCompleted { outcome, .. } => match outcome {
52 rite_model::StepOutcome::Completed { message } => {
53 Some((Icon::Checkmark, message.clone()))
54 }
55 _ => Some((Icon::Checkmark, "Step completed".to_string())),
56 },
57 StepFact::CeremonyFailed { error, .. } => {
58 Some((Icon::Cross, format!("Ceremony failed: {}", error.message)))
59 }
60 StepFact::EntropySeeded { source, .. } => {
61 Some((Icon::Info, format!("Entropy source seeded ({source})")))
62 }
63 StepFact::EntropyContributed { epoch, .. } => Some((
64 Icon::Info,
65 format!("Entropy contribution folded (epoch {epoch})"),
66 )),
67 StepFact::EntropyDrawn { path, .. } => {
68 Some((Icon::Info, format!("Random value drawn: {path}")))
69 }
70 _ => Some((Icon::Info, "unknown fact variant".to_string())),
74 }
75}
76
77#[must_use]
84pub fn signal_summary(signal: &UiSignal) -> Option<(Icon, String)> {
85 match signal {
86 UiSignal::LogLine { icon, text, .. } => Some((*icon, text.clone())),
87 UiSignal::Progress {
88 phase, fraction, ..
89 } => {
90 let text = match fraction {
91 Some(f) => format!("{phase}: {:>5.1}%", f * 100.0),
92 None => format!("{phase}…"),
93 };
94 Some((Icon::Spinner, text))
95 }
96 UiSignal::CeremonyOverview { .. } | UiSignal::SystemInfo(_) | UiSignal::Environment(_) => {
97 None
98 }
99 }
100}
101
102#[must_use]
107pub fn truncate_for_display(s: &str, max_chars: usize) -> String {
108 match s.char_indices().nth(max_chars) {
109 Some((boundary, _)) => format!("{}...", &s[..boundary]),
110 None => s.to_string(),
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::truncate_for_display;
117
118 #[test]
119 fn truncate_passes_through_short_input() {
120 assert_eq!(truncate_for_display("short", 10), "short");
121 }
122
123 #[test]
124 fn truncate_adds_ellipsis_when_too_long() {
125 assert_eq!(
126 truncate_for_display("this is a long string", 10),
127 "this is a ..."
128 );
129 }
130
131 #[test]
132 fn truncate_respects_utf8_code_point_boundaries() {
133 let s = "🦀🦀🦀🦀🦀";
135 assert_eq!(truncate_for_display(s, 2), "🦀🦀...");
136 assert_eq!(truncate_for_display(s, 5), "🦀🦀🦀🦀🦀");
137 }
138}