domani/src/origin.rs
2023-05-03 12:53:33 +02:00

45 lines
918 B
Rust

use sha2::{Sha256, Digest};
use hex::ToHex;
/// A unique description of an origin, from where a domain might be served.
pub enum Descr<'a> {
Git{url: &'a str, branch: &'a str},
}
impl<'a> Descr<'a> {
/// Returns a globally unique string for the Descr.
pub fn id(&'a self) -> String {
let mut h = Sha256::new();
let mut h_update = |b: &str| {
h.update(b);
h.update("\n");
};
match self {
Descr::Git{url, branch} => {
h_update("git");
h_update(url);
h_update(branch);
}
}
h.finalize().encode_hex::<String>()
}
}
#[cfg(test)]
mod descr_tests {
#[test]
fn id() {
assert_eq!(
super::Descr::Git{url:"foo", branch:"bar"}.id(),
"424130b9e6c1675c983b4b97579877e16d8a6377e4fe10970e6d210811c3b7ac",
)
}
//}