OXYGENHUB
# cdb5beba7f05 oxygen / src / cli / format.rs
2.2 KiB · Rust · last changed Shallow clone: clone --depth, checkpoint and shallow endpoints, boundary-aware import and negotiation, oxy deepen; log from state metadata (spec 0.6g) 1 month ago source blame history raw
fce44ae9 Andy McCall ↶ 1
//! Shared presentation helpers.
2
3
use anyhow::Result;
4
use oxy_core::hash::ChangeHash;
5
use oxy_core::objects::Change;
6
use oxy_core::repo::{Repo, SignatureStatus};
2863456a Andy McCall ↶ 7
use oxy_core::state::ChangeMeta;
fce44ae9 Andy McCall ↶ 8
9
pub fn short_id(c: &Change) -> String {
10
    c.change_id.short(8)
11
}
12
2863456a Andy McCall ↶ 13
pub fn short_meta_id(c: &ChangeMeta) -> String {
14
    c.change_id.short(8)
15
}
16
fce44ae9 Andy McCall ↶ 17
/// `n head` / `n heads`.
18
pub fn plural(n: usize, word: &str) -> String {
19
    format!("{n} {word}{}", if n == 1 { "" } else { "s" })
20
}
21
22
/// Milliseconds since the Unix epoch as `YYYY-MM-DD HH:MM:SSZ`.
23
pub fn when(ms: u64) -> String {
24
    let secs = (ms / 1000) as i64;
25
    let days = secs.div_euclid(86_400);
26
    let rem = secs.rem_euclid(86_400);
27
    // Civil-from-days (Howard Hinnant), UTC.
28
    let z = days + 719_468;
29
    let era = z.div_euclid(146_097);
30
    let doe = z - era * 146_097;
31
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
32
    let y = yoe + era * 400;
33
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
34
    let mp = (5 * doy + 2) / 153;
35
    let d = doy - (153 * mp + 2) / 5 + 1;
36
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
37
    let y = if m <= 2 { y + 1 } else { y };
38
    format!("{y:04}-{m:02}-{d:02} {:02}:{:02}:{:02}Z", rem / 3600, (rem % 3600) / 60, rem % 60)
39
}
40
41
/// One line describing how a change is authenticated.
42
pub fn signature_line(repo: &Repo, h: &ChangeHash) -> Result<String> {
43
    Ok(match repo.authenticate_change(h)? {
44
        SignatureStatus::Unsigned => "unsigned".to_string(),
45
        SignatureStatus::Valid { key, name, email, sealed } => {
46
            format!("{} by {name} <{email}> key {}", if sealed { "sealed" } else { "signed" }, key.short(12))
47
        }
48
        SignatureStatus::Invalid(why) => format!("INVALID signature: {why}"),
49
    })
50
}
51
52
pub fn deps_line(c: &Change) -> String {
2863456a Andy McCall ↶ 53
    deps_of(&c.deps)
54
}
55
56
pub fn deps_of(deps: &[oxy_core::hash::ChangeHash]) -> String {
57
    deps.iter().map(|d| format!("#{}", d.short(12))).collect::<Vec<_>>().join(" ")
fce44ae9 Andy McCall ↶ 58
}
59
60
#[cfg(test)]
61
mod tests {
62
    use super::*;
63
64
    #[test]
65
    fn dates() {
66
        assert_eq!(when(0), "1970-01-01 00:00:00Z");
67
        assert_eq!(when(1_700_000_000_000), "2023-11-14 22:13:20Z");
68
        assert_eq!(plural(1, "head"), "1 head");
69
        assert_eq!(plural(2, "head"), "2 heads");
70
    }
71
}