diff --git a/protocol/src/protocol/mod.rs b/protocol/src/protocol/mod.rs index f05fe35..347c8c4 100644 --- a/protocol/src/protocol/mod.rs +++ b/protocol/src/protocol/mod.rs @@ -346,6 +346,28 @@ impl Serializable for f64 { #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub struct UUID(u64, u64); +impl UUID { + pub fn from_str(s: &str) -> UUID { + use rustc_serialize::hex::FromHex; + // TODO: Panics aren't the best idea here + if s.len() != 36 { + panic!("Invalid UUID format"); + } + let mut parts = s[..8].from_hex().unwrap(); + parts.extend_from_slice(&s[9..13].from_hex().unwrap()); + parts.extend_from_slice(&s[14..18].from_hex().unwrap()); + parts.extend_from_slice(&s[19..23].from_hex().unwrap()); + parts.extend_from_slice(&s[24..36].from_hex().unwrap()); + let mut high = 0u64; + let mut low = 0u64; + for i in 0 .. 8 { + high |= (parts[i] as u64) << (56 - i*8); + low |= (parts[i + 8] as u64) << (56 - i*8); + } + UUID(high, low) + } +} + impl Default for UUID { fn default() -> Self { UUID(0, 0)