| 1 | use chrono::{DateTime, Datelike, FixedOffset, NaiveDate, Utc, Weekday}; |
| 2 | |
| 3 | use crate::theme; |
| 4 | |
| 5 | const BADGE_CAP: u32 = 99; |
| 6 | const WEEKDAY_WINDOW_DAYS: i64 = 6; |
| 7 | |
| 8 | pub fn palette_index(key: &str) -> usize { |
| 9 | let mut hash: u32 = 0x811c_9dc5; |
| 10 | for byte in key.bytes() { |
| 11 | hash ^= u32::from(byte); |
| 12 | hash = hash.wrapping_mul(0x0100_0193); |
| 13 | } |
| 14 | hash as usize % theme::avatar_palette_len() |
| 15 | } |
| 16 | |
| 17 | pub fn initials(name: &str) -> String { |
| 18 | let words: Vec<&str> = name |
| 19 | .split(|character: char| !character.is_alphanumeric()) |
| 20 | .filter(|word| !word.is_empty()) |
| 21 | .collect(); |
| 22 | let letters: Vec<char> = match words.as_slice() { |
| 23 | [] => return "?".to_owned(), |
| 24 | [single] => single.chars().take(2).collect(), |
| 25 | [first, .., last] => first.chars().take(1).chain(last.chars().take(1)).collect(), |
| 26 | }; |
| 27 | letters.into_iter().flat_map(char::to_uppercase).collect() |
| 28 | } |
| 29 | |
| 30 | pub fn first_name(name: &str) -> &str { |
| 31 | name.split_whitespace().next().unwrap_or(name) |
| 32 | } |
| 33 | |
| 34 | pub fn badge_text(count: u32) -> String { |
| 35 | if count > BADGE_CAP { |
| 36 | format!("{BADGE_CAP}+") |
| 37 | } else { |
| 38 | count.to_string() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn weekday_label(weekday: Weekday) -> &'static str { |
| 43 | match weekday { |
| 44 | Weekday::Mon => "Mo", |
| 45 | Weekday::Tue => "Di", |
| 46 | Weekday::Wed => "Mi", |
| 47 | Weekday::Thu => "Do", |
| 48 | Weekday::Fri => "Fr", |
| 49 | Weekday::Sat => "Sa", |
| 50 | Weekday::Sun => "So", |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn file_size_label(bytes: u64) -> String { |
| 55 | const KIB: f64 = 1024.; |
| 56 | let value = bytes as f64; |
| 57 | if value < KIB { |
| 58 | format!("{bytes} B") |
| 59 | } else if value < KIB * KIB { |
| 60 | format!("{:.0} KB", value / KIB) |
| 61 | } else { |
| 62 | format!("{:.1} MB", value / (KIB * KIB)) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub fn short_version(build_id: &str, package_version: &str) -> String { |
| 67 | let commit = build_id.split('-').next().unwrap_or(build_id); |
| 68 | format!("v{package_version} ({commit})") |
| 69 | } |
| 70 | |
| 71 | pub fn list_time_label(time: DateTime<Utc>, today: NaiveDate, offset: FixedOffset) -> String { |
| 72 | let local = time.with_timezone(&offset); |
| 73 | let day = local.date_naive(); |
| 74 | let days_ago = (today - day).num_days(); |
| 75 | if days_ago <= 0 { |
| 76 | local.format("%H:%M").to_string() |
| 77 | } else if days_ago <= WEEKDAY_WINDOW_DAYS { |
| 78 | weekday_label(local.weekday()).to_owned() |
| 79 | } else { |
| 80 | local.format("%d.%m.").to_string() |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub fn day_label(day: NaiveDate, today: NaiveDate) -> String { |
| 85 | if day == today { |
| 86 | "Heute".to_owned() |
| 87 | } else if today.pred_opt() == Some(day) { |
| 88 | "Gestern".to_owned() |
| 89 | } else { |
| 90 | format!( |
| 91 | "{} {}", |
| 92 | weekday_label(day.weekday()), |
| 93 | day.format("%d.%m.%Y") |
| 94 | ) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[cfg(test)] |
| 99 | mod tests { |
| 100 | use chrono::TimeZone; |
| 101 | |
| 102 | use super::*; |
| 103 | |
| 104 | fn utc(day: u32, hour: u32) -> DateTime<Utc> { |
| 105 | Utc.with_ymd_and_hms(2026, 10, day, hour, 5, 0).unwrap() |
| 106 | } |
| 107 | |
| 108 | fn today() -> NaiveDate { |
| 109 | NaiveDate::from_ymd_opt(2026, 10, 7).unwrap() |
| 110 | } |
| 111 | |
| 112 | fn zero() -> FixedOffset { |
| 113 | FixedOffset::east_opt(0).unwrap() |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn palette_index_is_stable_and_in_range() { |
| 118 | assert_eq!(palette_index("user-1"), palette_index("user-1")); |
| 119 | for key in [ |
| 120 | "a", |
| 121 | "b", |
| 122 | "demo-ada", |
| 123 | "", |
| 124 | "00000000-0000-0000-0000-000000000001", |
| 125 | ] { |
| 126 | assert!(palette_index(key) < theme::avatar_palette_len()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn palette_index_spreads_over_several_colors() { |
| 132 | let used: std::collections::HashSet<usize> = (0..40) |
| 133 | .map(|number| palette_index(&format!("user-{number}"))) |
| 134 | .collect(); |
| 135 | assert!(used.len() >= 4); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn initials_use_first_and_last_word() { |
| 140 | assert_eq!(initials("Mara Lindqvist"), "ML"); |
| 141 | assert_eq!(initials("Ada"), "AD"); |
| 142 | assert_eq!(initials("anna maria von Berg"), "AB"); |
| 143 | assert_eq!(initials(" "), "?"); |
| 144 | assert_eq!(initials("Release planning"), "RP"); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn first_name_takes_the_first_word() { |
| 149 | assert_eq!(first_name("Priya Nair"), "Priya"); |
| 150 | assert_eq!(first_name("Priya"), "Priya"); |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn file_sizes_use_binary_units() { |
| 155 | assert_eq!(file_size_label(900), "900 B"); |
| 156 | assert_eq!(file_size_label(48_213), "47 KB"); |
| 157 | assert_eq!(file_size_label(5 * 1024 * 1024 + 300_000), "5.3 MB"); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn badge_caps_at_99_plus() { |
| 162 | assert_eq!(badge_text(1), "1"); |
| 163 | assert_eq!(badge_text(99), "99"); |
| 164 | assert_eq!(badge_text(100), "99+"); |
| 165 | assert_eq!(badge_text(4000), "99+"); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn list_time_is_clock_today_weekday_this_week_date_before() { |
| 170 | assert_eq!(list_time_label(utc(7, 13), today(), zero()), "13:05"); |
| 171 | assert_eq!(list_time_label(utc(5, 13), today(), zero()), "Mo"); |
| 172 | assert_eq!(list_time_label(utc(6, 9), today(), zero()), "Di"); |
| 173 | assert_eq!(list_time_label(utc(1, 13), today(), zero()), "Do"); |
| 174 | let september = Utc.with_ymd_and_hms(2026, 9, 30, 13, 0, 0).unwrap(); |
| 175 | assert_eq!(list_time_label(september, today(), zero()), "30.09."); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn list_time_respects_the_offset_for_the_day_boundary() { |
| 180 | let plus_two = FixedOffset::east_opt(2 * 3600).unwrap(); |
| 181 | assert_eq!(list_time_label(utc(6, 23), today(), plus_two), "01:05"); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn short_version_keeps_the_commit_only() { |
| 186 | assert_eq!( |
| 187 | short_version("687e2dc-20261007T124355Z", "0.1.0"), |
| 188 | "v0.1.0 (687e2dc)" |
| 189 | ); |
| 190 | assert_eq!(short_version("dev", "0.1.0"), "v0.1.0 (dev)"); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn day_labels() { |
| 195 | assert_eq!(day_label(today(), today()), "Heute"); |
| 196 | assert_eq!(day_label(today().pred_opt().unwrap(), today()), "Gestern"); |
| 197 | assert_eq!( |
| 198 | day_label(NaiveDate::from_ymd_opt(2026, 10, 1).unwrap(), today()), |
| 199 | "Do 01.10.2026" |
| 200 | ); |
| 201 | } |
| 202 | } |