use std::fmt::{Display, Formatter};
#[derive(Debug, PartialEq, Eq, PartialOrd, Hash, Clone, Copy)]
pub enum Version {
Google,
RfcDraft14,
}
struct VersionData {
wire: &'static [u8],
display: &'static str,
dele_prefix: &'static [u8],
srep_prefix: &'static [u8],
}
impl Version {
const fn data(&self) -> VersionData {
match self {
Version::Google => VersionData {
wire: &[0x00, 0x00, 0x00, 0x00],
display: "Google",
dele_prefix: b"RoughTime v1 delegation signature--\x00",
srep_prefix: b"RoughTime v1 response signature\x00",
},
Version::RfcDraft14 => VersionData {
wire: &[0x0c, 0x00, 0x00, 0x80],
display: "RfcDraft14",
dele_prefix: b"RoughTime v1 delegation signature\x00",
srep_prefix: b"RoughTime v1 response signature\x00",
},
}
}
pub fn supported_versions_wire() -> Vec<u8> {
[
Version::Google.wire_bytes(),
Version::RfcDraft14.wire_bytes(),
]
.concat()
}
pub const fn wire_bytes(&self) -> &'static [u8] {
self.data().wire
}
pub const fn as_string(&self) -> &'static str {
self.data().display
}
pub const fn dele_prefix(&self) -> &'static [u8] {
self.data().dele_prefix
}
pub const fn sign_prefix(&self) -> &'static [u8] {
self.data().srep_prefix
}
}
impl Display for Version {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_string())
}
}
#[cfg(test)]
mod tests {
use super::Version;
#[test]
fn test_supported_versions_wire() {
let expected = [
0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x80, ];
assert_eq!(Version::supported_versions_wire(), expected);
}
#[test]
fn test_dele_prefix() {
assert_eq!(
Version::Google.dele_prefix(),
b"RoughTime v1 delegation signature--\x00"
);
assert_eq!(
Version::RfcDraft14.dele_prefix(),
b"RoughTime v1 delegation signature\x00"
);
}
}