| 1 | use std::time::Duration; |
| 2 | |
| 3 | const RECENT_WINDOW: usize = 16; |
| 4 | |
| 5 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 6 | pub 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)] |
| 14 | pub struct Transcript { |
| 15 | entries: Vec<TranscriptEntry>, |
| 16 | } |
| 17 | |
| 18 | impl 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 | |
| 51 | pub 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)] |
| 57 | mod 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.filtered("").iter().map(|entry| entry.id.as_str()).collect(); |
| 81 | assert_eq!(ids, ["a", "b", "c"]); |
| 82 | assert_eq!(transcript.filtered("").len(), 3); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn a_growing_partial_updates_its_entry_and_keeps_its_time_and_place() { |
| 87 | let mut transcript = sample(); |
| 88 | transcript.note(entry("b", "Bo Sample", "The build is green again", 20)); |
| 89 | assert_eq!(transcript.filtered("").len(), 3); |
| 90 | let updated = &transcript.filtered("")[1]; |
| 91 | assert_eq!(updated.text, "The build is green again"); |
| 92 | assert_eq!(updated.offset, Duration::from_secs(9)); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn an_old_id_far_back_is_a_new_entry() { |
| 97 | let mut transcript = sample(); |
| 98 | for index in 0..RECENT_WINDOW { |
| 99 | transcript.note(entry(&format!("x{index}"), "Ana Example", "filler", 30)); |
| 100 | } |
| 101 | transcript.note(entry("a", "Ana Example", "again", 99)); |
| 102 | assert_eq!(transcript.filtered("").len(), 3 + RECENT_WINDOW + 1); |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn the_filter_matches_text_and_speaker_ignoring_case() { |
| 107 | let transcript = sample(); |
| 108 | assert_eq!(transcript.filtered("BUILD").len(), 1); |
| 109 | assert_eq!(transcript.filtered("ana").len(), 2); |
| 110 | assert_eq!(transcript.filtered(" ship ").len(), 1); |
| 111 | assert!(transcript.filtered("nothing like this").is_empty()); |
| 112 | assert_eq!(transcript.filtered(" ").len(), 3); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn the_clock_counts_minutes_and_seconds_since_the_call_start() { |
| 117 | assert_eq!(clock_label(Duration::from_secs(5)), "00:05"); |
| 118 | assert_eq!(clock_label(Duration::from_secs(75)), "01:15"); |
| 119 | assert_eq!(clock_label(Duration::from_secs(4_503)), "75:03"); |
| 120 | } |
| 121 | } |