domani/src/origin/descr.rs

47 lines
959 B
Rust
Raw Normal View History

2023-05-03 10:42:00 +00:00
use sha2::{Sha256, Digest};
use hex::ToHex;
2023-05-04 12:56:31 +00:00
#[derive(Clone,Copy)]
2023-05-03 10:42:00 +00:00
/// A unique description of an origin, from where a domain might be served.
pub enum Descr<'a> {
2023-05-04 12:56:31 +00:00
Git{url: &'a str, branch_name: &'a str},
2023-05-03 10:42:00 +00:00
}
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 {
2023-05-04 12:56:31 +00:00
Descr::Git{url, branch_name} => {
2023-05-03 10:42:00 +00:00
h_update("git");
h_update(url);
2023-05-04 12:56:31 +00:00
h_update(branch_name);
2023-05-03 10:42:00 +00:00
}
}
h.finalize().encode_hex::<String>()
}
}
#[cfg(test)]
mod descr_tests {
#[test]
fn id() {
assert_eq!(
2023-05-04 12:56:31 +00:00
super::Descr::Git{url:"foo", branch_name:"bar"}.id(),
2023-05-03 10:42:00 +00:00
"424130b9e6c1675c983b4b97579877e16d8a6377e4fe10970e6d210811c3b7ac",
)
}
}