domani/src/origin/descr.rs
2023-05-10 15:12:34 +02:00

48 lines
1.1 KiB
Rust

use hex::ToHex;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// A unique description of an origin, from where a domain might be served.
pub enum Descr {
Git { url: String, branch_name: String },
}
impl Descr {
/// Returns a globally unique string for the Descr.
pub fn id(&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_name } => {
h_update("git");
h_update(url);
h_update(branch_name);
}
}
h.finalize().encode_hex::<String>()
}
}
#[cfg(test)]
mod descr_tests {
#[test]
fn id() {
assert_eq!(
super::Descr::Git {
url: String::from("foo"),
branch_name: String::from("bar"),
}
.id(),
"424130b9e6c1675c983b4b97579877e16d8a6377e4fe10970e6d210811c3b7ac",
)
}
}