2023-05-03 10:42:00 +00:00
|
|
|
use hex::ToHex;
|
2023-05-07 11:03:59 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use sha2::{Digest, Sha256};
|
2023-05-03 10:42:00 +00:00
|
|
|
|
2023-05-07 11:03:59 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2023-05-03 10:42:00 +00:00
|
|
|
/// A unique description of an origin, from where a domain might be served.
|
2023-05-06 13:38:43 +00:00
|
|
|
pub enum Descr {
|
2023-05-07 11:03:59 +00:00
|
|
|
Git { url: String, branch_name: String },
|
2023-05-03 10:42:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 13:38:43 +00:00
|
|
|
impl Descr {
|
2023-05-03 10:42:00 +00:00
|
|
|
/// Returns a globally unique string for the Descr.
|
2023-05-06 13:38:43 +00:00
|
|
|
pub fn id(&self) -> String {
|
2023-05-03 10:42:00 +00:00
|
|
|
let mut h = Sha256::new();
|
|
|
|
|
|
|
|
let mut h_update = |b: &str| {
|
|
|
|
h.update(b);
|
|
|
|
h.update("\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
match self {
|
2023-05-07 11:03:59 +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-07 11:03:59 +00:00
|
|
|
super::Descr::Git {
|
2023-05-06 13:38:43 +00:00
|
|
|
url: String::from("foo"),
|
|
|
|
branch_name: String::from("bar"),
|
2023-05-07 11:03:59 +00:00
|
|
|
}
|
|
|
|
.id(),
|
2023-05-03 10:42:00 +00:00
|
|
|
"424130b9e6c1675c983b4b97579877e16d8a6377e4fe10970e6d210811c3b7ac",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|