Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Nil variant #181

Merged
merged 6 commits into from
Mar 25, 2018
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ pub type UuidBytes = [u8; 16];
/// The version of the UUID, denoting the generating algorithm.
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum UuidVersion {
/// Special case for `nil` [`struct.Uuid.html`].
Copy link
Member

@KodrAus KodrAus Mar 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we have the [struct.Uuid.html] here for? Does this render in the docs as struct.Uuid.html?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops I forgot to link it will fix it ASAP

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was supposed to be Uuid

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KodrAus fixed.. can you approve

Nil,
/// Version 1: MAC address
Mac = 1,
/// Version 2: DCE Security
Expand Down Expand Up @@ -697,6 +699,7 @@ impl Uuid {
pub fn get_version(&self) -> Option<UuidVersion> {
let v = self.bytes[6] >> 4;
match v {
0 if self.is_nil() => Some(UuidVersion::Nil),
1 => Some(UuidVersion::Mac),
2 => Some(UuidVersion::Dce),
3 => Some(UuidVersion::Md5),
Expand Down Expand Up @@ -1044,29 +1047,27 @@ impl<'a> fmt::Display for Hyphenated<'a> {
}

macro_rules! hyphnated_write {
($f:expr, $format:expr, $bytes:expr) => {{
let data1 = (($bytes[0] as u32) << 24) |
(($bytes[1] as u32) << 16) |
(($bytes[2] as u32) << 8) |
(($bytes[3] as u32) << 0);
let data2 = (($bytes[4] as u16) << 8) |
(($bytes[5] as u16) << 0);
let data3 = (($bytes[6] as u16) << 8) |
(($bytes[7] as u16) << 0);

write!($f,
$format,
data1,
data2,
data3,
$bytes[8],
$bytes[9],
$bytes[10],
$bytes[11],
$bytes[12],
$bytes[13],
$bytes[14],
$bytes[15])
($f: expr, $format: expr, $bytes: expr) => {{
let data1 = (($bytes[0] as u32) << 24) | (($bytes[1] as u32) << 16)
| (($bytes[2] as u32) << 8) | (($bytes[3] as u32) << 0);
let data2 = (($bytes[4] as u16) << 8) | (($bytes[5] as u16) << 0);
let data3 = (($bytes[6] as u16) << 8) | (($bytes[7] as u16) << 0);

write!(
$f,
$format,
data1,
data2,
data3,
$bytes[8],
$bytes[9],
$bytes[10],
$bytes[11],
$bytes[12],
$bytes[13],
$bytes[14],
$bytes[15]
)
}};
}

Expand Down Expand Up @@ -1272,9 +1273,16 @@ mod tests {
fn test_nil() {
let nil = Uuid::nil();
let not_nil = new();
let from_bytes =
Uuid::from_uuid_bytes([4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87]);

assert_eq!(from_bytes.get_version(), None);

assert!(nil.is_nil());
assert!(!not_nil.is_nil());

assert_eq!(nil.get_version(), Some(UuidVersion::Nil));
assert_eq!(not_nil.get_version(), Some(UuidVersion::Random))
}

#[test]
Expand Down Expand Up @@ -1571,7 +1579,7 @@ mod tests {
let u = new();

macro_rules! check {
($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
($buf: ident, $format: expr, $target: expr, $len: expr, $cond: expr) => {
$buf.clear();
write!($buf, $format, $target).unwrap();
assert!(buf.len() == $len);
Expand Down