2016-06-20 10:06:49 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-06-20 00:10:34 +02:00
|
|
|
//! Universaly unique identifier.
|
2016-09-28 15:47:52 +02:00
|
|
|
use std::{fmt, str};
|
2016-06-20 00:10:34 +02:00
|
|
|
use rustc_serialize::hex::{ToHex, FromHex};
|
|
|
|
use serde::{Deserialize, Serialize, Deserializer, Serializer, Error as SerdeError};
|
|
|
|
use serde::de::Visitor;
|
|
|
|
use super::Error;
|
|
|
|
|
|
|
|
/// Universaly unique identifier.
|
|
|
|
#[derive(Debug, PartialEq)]
|
2016-12-09 23:01:43 +01:00
|
|
|
pub struct Uuid([u8; 16]);
|
2016-06-20 00:10:34 +02:00
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl From<[u8; 16]> for Uuid {
|
2016-06-20 00:10:34 +02:00
|
|
|
fn from(uuid: [u8; 16]) -> Self {
|
2016-12-09 23:01:43 +01:00
|
|
|
Uuid(uuid)
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl<'a> Into<String> for &'a Uuid {
|
2016-06-20 00:10:34 +02:00
|
|
|
fn into(self) -> String {
|
|
|
|
let d1 = &self.0[0..4];
|
|
|
|
let d2 = &self.0[4..6];
|
|
|
|
let d3 = &self.0[6..8];
|
|
|
|
let d4 = &self.0[8..10];
|
|
|
|
let d5 = &self.0[10..16];
|
|
|
|
[d1, d2, d3, d4, d5].into_iter().map(|d| d.to_hex()).collect::<Vec<String>>().join("-")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl Into<String> for Uuid {
|
2016-06-20 00:10:34 +02:00
|
|
|
fn into(self) -> String {
|
|
|
|
Into::into(&self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl Into<[u8; 16]> for Uuid {
|
2016-06-20 00:10:34 +02:00
|
|
|
fn into(self) -> [u8; 16] {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl fmt::Display for Uuid {
|
2016-06-20 00:10:34 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
2016-12-09 23:01:43 +01:00
|
|
|
let s: String = (self as &Uuid).into();
|
2016-06-20 00:10:34 +02:00
|
|
|
write!(f, "{}", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy_into(from: &str, into: &mut [u8]) -> Result<(), Error> {
|
2016-12-09 23:01:43 +01:00
|
|
|
let from = try!(from.from_hex().map_err(|_| Error::InvalidUuid));
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
if from.len() != into.len() {
|
2016-12-09 23:01:43 +01:00
|
|
|
return Err(Error::InvalidUuid);
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
into.copy_from_slice(&from);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl str::FromStr for Uuid {
|
2016-06-20 00:10:34 +02:00
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let parts: Vec<&str> = s.split("-").collect();
|
|
|
|
|
|
|
|
if parts.len() != 5 {
|
2016-12-09 23:01:43 +01:00
|
|
|
return Err(Error::InvalidUuid);
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut uuid = [0u8; 16];
|
|
|
|
|
|
|
|
try!(copy_into(parts[0], &mut uuid[0..4]));
|
|
|
|
try!(copy_into(parts[1], &mut uuid[4..6]));
|
|
|
|
try!(copy_into(parts[2], &mut uuid[6..8]));
|
|
|
|
try!(copy_into(parts[3], &mut uuid[8..10]));
|
|
|
|
try!(copy_into(parts[4], &mut uuid[10..16]));
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
Ok(Uuid(uuid))
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl From<&'static str> for Uuid {
|
2016-09-28 15:47:52 +02:00
|
|
|
fn from(s: &'static str) -> Self {
|
|
|
|
s.parse().expect(&format!("invalid string literal for {}: '{}'", stringify!(Self), s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl Serialize for Uuid {
|
2016-06-20 00:10:34 +02:00
|
|
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
|
|
|
where S: Serializer {
|
|
|
|
let s: String = self.into();
|
|
|
|
serializer.serialize_str(&s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl Deserialize for Uuid {
|
2016-06-20 00:10:34 +02:00
|
|
|
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
|
|
|
|
where D: Deserializer {
|
2016-12-09 23:01:43 +01:00
|
|
|
deserializer.deserialize(UuidVisitor)
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
struct UuidVisitor;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
impl Visitor for UuidVisitor {
|
|
|
|
type Value = Uuid;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: SerdeError {
|
2016-09-28 15:47:52 +02:00
|
|
|
value.parse().map_err(SerdeError::custom)
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: SerdeError {
|
|
|
|
self.visit_str(value.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-12-09 23:01:43 +01:00
|
|
|
use super::Uuid;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uuid_from_str() {
|
2016-12-09 23:01:43 +01:00
|
|
|
let uuid: Uuid = "3198bc9c-6672-5ab3-d995-4942343ae5b6".into();
|
|
|
|
assert_eq!(uuid, Uuid::from([0x31, 0x98, 0xbc, 0x9c, 0x66, 0x72, 0x5a, 0xb3, 0xd9, 0x95, 0x49, 0x42, 0x34, 0x3a, 0xe5, 0xb6]));
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uuid_from_and_to_str() {
|
|
|
|
let from = "3198bc9c-6672-5ab3-d995-4942343ae5b6";
|
2016-12-09 23:01:43 +01:00
|
|
|
let uuid: Uuid = from.into();
|
2016-06-20 00:10:34 +02:00
|
|
|
let to: String = uuid.into();
|
|
|
|
assert_eq!(from, &to);
|
|
|
|
}
|
|
|
|
}
|