| fce44ae9 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
use anyhow::Result; |
| 4 |
use oxy_core::hash::ChangeHash; |
| 5 |
use oxy_core::objects::Change; |
| 6 |
use oxy_core::repo::{Repo, SignatureStatus}; |
| 7 |
|
| 8 |
pub fn short_id(c: &Change) -> String { |
| 9 |
c.change_id.short(8) |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
pub fn plural(n: usize, word: &str) -> String { |
| 14 |
format!("{n} {word}{}", if n == 1 { "" } else { "s" }) |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
pub fn when(ms: u64) -> String { |
| 19 |
let secs = (ms / 1000) as i64; |
| 20 |
let days = secs.div_euclid(86_400); |
| 21 |
let rem = secs.rem_euclid(86_400); |
| 22 |
|
| 23 |
let z = days + 719_468; |
| 24 |
let era = z.div_euclid(146_097); |
| 25 |
let doe = z - era * 146_097; |
| 26 |
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; |
| 27 |
let y = yoe + era * 400; |
| 28 |
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); |
| 29 |
let mp = (5 * doy + 2) / 153; |
| 30 |
let d = doy - (153 * mp + 2) / 5 + 1; |
| 31 |
let m = if mp < 10 { mp + 3 } else { mp - 9 }; |
| 32 |
let y = if m <= 2 { y + 1 } else { y }; |
| 33 |
format!("{y:04}-{m:02}-{d:02} {:02}:{:02}:{:02}Z", rem / 3600, (rem % 3600) / 60, rem % 60) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
pub fn signature_line(repo: &Repo, h: &ChangeHash) -> Result<String> { |
| 38 |
Ok(match repo.authenticate_change(h)? { |
| 39 |
SignatureStatus::Unsigned => "unsigned".to_string(), |
| 40 |
SignatureStatus::Valid { key, name, email, sealed } => { |
| 41 |
format!("{} by {name} <{email}> key {}", if sealed { "sealed" } else { "signed" }, key.short(12)) |
| 42 |
} |
| 43 |
SignatureStatus::Invalid(why) => format!("INVALID signature: {why}"), |
| 44 |
}) |
| 45 |
} |
| 46 |
|
| 47 |
pub fn deps_line(c: &Change) -> String { |
| 48 |
c.deps.iter().map(|d| format!("#{}", d.short(12))).collect::<Vec<_>>().join(" ") |
| 49 |
} |
| 50 |
|
| 51 |
#[cfg(test)] |
| 52 |
mod tests { |
| 53 |
use super::*; |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn dates() { |
| 57 |
assert_eq!(when(0), "1970-01-01 00:00:00Z"); |
| 58 |
assert_eq!(when(1_700_000_000_000), "2023-11-14 22:13:20Z"); |
| 59 |
assert_eq!(plural(1, "head"), "1 head"); |
| 60 |
assert_eq!(plural(2, "head"), "2 heads"); |
| 61 |
} |
| 62 |
} |