BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-call-model / src / transcript.rs

transcript.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
transcript.rs 4.0 KiB Download Raw
1use std::time::Duration;
2
3const RECENT_WINDOW: usize = 16;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct TranscriptEntry {
7 pub id: String,
8 pub speaker: String,
9 pub text: String,
10 pub offset: Duration,
11}
12
13#[derive(Debug, Clone, Default)]
14pub struct Transcript {
15 entries: Vec<TranscriptEntry>,
16}
17
18impl Transcript {
19 pub fn note(&mut self, entry: TranscriptEntry) {
20 let start = self.entries.len().saturating_sub(RECENT_WINDOW);
21 let known = self.entries[start..]
22 .iter()
23 .rposition(|existing| existing.id == entry.id);
24 match known {
25 Some(position) => {
26 let existing = &mut self.entries[start + position];
27 existing.speaker = entry.speaker;
28 existing.text = entry.text;
29 }
30 None => self.entries.push(entry),
31 }
32 }
33
34 pub fn is_empty(&self) -> bool {
35 self.entries.is_empty()
36 }
37
38 pub fn filtered(&self, query: &str) -> Vec<&TranscriptEntry> {
39 let needle = query.trim().to_lowercase();
40 self.entries
41 .iter()
42 .filter(|entry| {
43 needle.is_empty()
44 || entry.text.to_lowercase().contains(&needle)
45 || entry.speaker.to_lowercase().contains(&needle)
46 })
47 .collect()
48 }
49}
50
51pub fn clock_label(offset: Duration) -> String {
52 let seconds = offset.as_secs();
53 format!("{:02}:{:02}", seconds / 60, seconds % 60)
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 fn entry(id: &str, speaker: &str, text: &str, seconds: u64) -> TranscriptEntry {
61 TranscriptEntry {
62 id: id.into(),
63 speaker: speaker.into(),
64 text: text.into(),
65 offset: Duration::from_secs(seconds),
66 }
67 }
68
69 fn sample() -> Transcript {
70 let mut transcript = Transcript::default();
71 transcript.note(entry("a", "Ana Example", "Welcome to the standup", 3));
72 transcript.note(entry("b", "Bo Sample", "The build is green", 9));
73 transcript.note(entry("c", "Ana Example", "Great, then we ship", 14));
74 transcript
75 }
76
77 #[test]
78 fn entries_keep_the_order_they_first_arrived_in() {
79 let transcript = sample();
80 let ids: Vec<&str> = transcript
81 .filtered("")
82 .iter()
83 .map(|entry| entry.id.as_str())
84 .collect();
85 assert_eq!(ids, ["a", "b", "c"]);
86 assert_eq!(transcript.filtered("").len(), 3);
87 }
88
89 #[test]
90 fn a_growing_partial_updates_its_entry_and_keeps_its_time_and_place() {
91 let mut transcript = sample();
92 transcript.note(entry("b", "Bo Sample", "The build is green again", 20));
93 assert_eq!(transcript.filtered("").len(), 3);
94 let updated = &transcript.filtered("")[1];
95 assert_eq!(updated.text, "The build is green again");
96 assert_eq!(updated.offset, Duration::from_secs(9));
97 }
98
99 #[test]
100 fn an_old_id_far_back_is_a_new_entry() {
101 let mut transcript = sample();
102 for index in 0..RECENT_WINDOW {
103 transcript.note(entry(&format!("x{index}"), "Ana Example", "filler", 30));
104 }
105 transcript.note(entry("a", "Ana Example", "again", 99));
106 assert_eq!(transcript.filtered("").len(), 3 + RECENT_WINDOW + 1);
107 }
108
109 #[test]
110 fn the_filter_matches_text_and_speaker_ignoring_case() {
111 let transcript = sample();
112 assert_eq!(transcript.filtered("BUILD").len(), 1);
113 assert_eq!(transcript.filtered("ana").len(), 2);
114 assert_eq!(transcript.filtered(" ship ").len(), 1);
115 assert!(transcript.filtered("nothing like this").is_empty());
116 assert_eq!(transcript.filtered(" ").len(), 3);
117 }
118
119 #[test]
120 fn the_clock_counts_minutes_and_seconds_since_the_call_start() {
121 assert_eq!(clock_label(Duration::from_secs(5)), "00:05");
122 assert_eq!(clock_label(Duration::from_secs(75)), "01:15");
123 assert_eq!(clock_label(Duration::from_secs(4_503)), "75:03");
124 }
125}