Skip to content

Commit

Permalink
renaming obsolete Variant field RFC4122
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikopet committed Jun 24, 2024
1 parent dacbc9e commit 6f6cafb
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 33 deletions.
2 changes: 1 addition & 1 deletion examples/src/windows_guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn uuid_from_cocreateguid() {

let uuid = Uuid::from_fields(guid.data1, guid.data2, guid.data3, &guid.data4);

assert_eq!(Variant::RFC4122, uuid.get_variant());
assert_eq!(Variant::RFC, uuid.get_variant());
assert_eq!(Some(Version::Random), uuid.get_version());
}

Expand Down
19 changes: 11 additions & 8 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::{error::*, timestamp, Bytes, Uuid, Variant, Version};
/// let uuid = Builder::from_random_bytes(random_bytes).into_uuid();
///
/// assert_eq!(Some(Version::Random), uuid.get_version());
/// assert_eq!(Variant::RFC4122, uuid.get_variant());
/// assert_eq!(Variant::RFC, uuid.get_variant());
/// ```
#[allow(missing_copy_implementations)]
#[derive(Debug)]
Expand Down Expand Up @@ -559,7 +559,7 @@ impl Builder {
/// Creates a `Builder` for a version 3 UUID using the supplied MD5 hashed bytes.
pub const fn from_md5_bytes(md5_bytes: Bytes) -> Self {
Builder(Uuid::from_bytes(md5_bytes))
.with_variant(Variant::RFC4122)
.with_variant(Variant::RFC)
.with_version(Version::Md5)
}

Expand All @@ -580,11 +580,11 @@ impl Builder {
/// let uuid = Builder::from_random_bytes(random_bytes).into_uuid();
///
/// assert_eq!(Some(Version::Random), uuid.get_version());
/// assert_eq!(Variant::RFC4122, uuid.get_variant());
/// assert_eq!(Variant::RFC, uuid.get_variant());
/// ```
pub const fn from_random_bytes(random_bytes: Bytes) -> Self {
Builder(Uuid::from_bytes(random_bytes))
.with_variant(Variant::RFC4122)
.with_variant(Variant::RFC)
.with_version(Version::Random)
}

Expand All @@ -594,7 +594,7 @@ impl Builder {
/// bits for the UUID version and variant.
pub const fn from_sha1_bytes(sha1_bytes: Bytes) -> Self {
Builder(Uuid::from_bytes(sha1_bytes))
.with_variant(Variant::RFC4122)
.with_variant(Variant::RFC)
.with_version(Version::Sha1)
}

Expand Down Expand Up @@ -636,7 +636,7 @@ impl Builder {
/// let uuid = Builder::from_unix_timestamp_millis(ts.as_millis().try_into()?, &random_bytes).into_uuid();
///
/// assert_eq!(Some(Version::SortRand), uuid.get_version());
/// assert_eq!(Variant::RFC4122, uuid.get_variant());
/// assert_eq!(Variant::RFC, uuid.get_variant());
/// # Ok(())
/// # }
/// ```
Expand All @@ -653,7 +653,7 @@ impl Builder {
/// bits for the UUID version and variant.
pub const fn from_custom_bytes(custom_bytes: Bytes) -> Self {
Builder::from_bytes(custom_bytes)
.with_variant(Variant::RFC4122)
.with_variant(Variant::RFC)
.with_version(Version::Custom)
}

Expand Down Expand Up @@ -846,9 +846,12 @@ impl Builder {

(self.0).0[8] = match v {
Variant::NCS => byte & 0x7f,
Variant::RFC4122 => (byte & 0x3f) | 0x80,
Variant::RFC => (byte & 0x3f) | 0x80,
Variant::Microsoft => (byte & 0x1f) | 0xc0,
Variant::Future => byte | 0xe0,
// Deprecated. Remove when major version changes (2.0.0)
#[allow(deprecated)]
Variant::RFC4122 => (byte & 0x3f) | 0x80,
};

self
Expand Down
2 changes: 1 addition & 1 deletion src/external/arbitrary_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod tests {
let uuid = Uuid::arbitrary(&mut bytes).unwrap();

assert_eq!(Some(Version::Random), uuid.get_version());
assert_eq!(Variant::RFC4122, uuid.get_variant());
assert_eq!(Variant::RFC, uuid.get_variant());
}

#[test]
Expand Down
12 changes: 11 additions & 1 deletion src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ impl fmt::Display for Variant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Variant::NCS => write!(f, "NCS"),
Variant::RFC4122 => write!(f, "RFC4122"),
Variant::RFC => write!(f, "RFC"),
Variant::Microsoft => write!(f, "Microsoft"),
Variant::Future => write!(f, "Future"),
// Deprecated. Remove when major version changes (2.0.0)
#[allow(deprecated)]
Variant::RFC4122 => write!(f, "RFC4122"),
}
}
}
Expand Down Expand Up @@ -924,6 +927,7 @@ impl_fmt_traits! {

#[cfg(test)]
mod tests {

use super::*;

#[test]
Expand Down Expand Up @@ -1037,4 +1041,10 @@ mod tests {
let braced = Uuid::nil().braced();
assert_eq!(Uuid::from(braced), Uuid::nil());
}

#[test]
#[allow(deprecated)]
fn fmt_variant() {
assert_eq!(Variant::RFC4122.to_string(), "RFC4122");
}
}
43 changes: 35 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,38 @@ pub enum Version {
/// # References
///
/// * [Variant Field in RFC 9562](https://www.ietf.org/rfc/rfc9562.html#section-4.1)
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u8)]
pub enum Variant {
/// Reserved by the NCS for backward compatibility.
NCS = 0u8,
/// As described in the RFC 9562 Specification (default).
/// (for backward compatibility it is not yet renamed)
RFC4122,
RFC,
/// Reserved by Microsoft for backward compatibility.
Microsoft,
/// Reserved for future expansion.
Future,
/// As described in the RFC 4122 Specification (deprecated).
#[deprecated(since = "1.9.1", note = "Deprecated! Use `Variant::RFC` instead!")]
RFC4122 = 255u8,
}

// Deprecations:
// - Remove when major version changes (2.0.0)
// - put back `PartialEq` derive macro
#[doc(hidden)]
impl PartialEq for Variant {
fn eq(&self, other: &Self) -> bool {
#[allow(deprecated)]
match self {
Variant::RFC4122 | Variant::RFC => match other {
Variant::RFC | Variant::RFC4122 => true,
_ => *self as u8 == *other as u8,
},
_ => *self as u8 == *other as u8,
}
}
}

/// A Universally Unique Identifier (UUID).
Expand Down Expand Up @@ -491,7 +510,7 @@ impl Uuid {
/// # fn main() -> Result<(), uuid::Error> {
/// let my_uuid = Uuid::parse_str("02f09a3f-1624-3b1d-8409-44eff7708208")?;
///
/// assert_eq!(Variant::RFC4122, my_uuid.get_variant());
/// assert_eq!(Variant::RFC, my_uuid.get_variant());
/// # Ok(())
/// # }
/// ```
Expand All @@ -502,7 +521,7 @@ impl Uuid {
pub const fn get_variant(&self) -> Variant {
match self.as_bytes()[8] {
x if x & 0x80 == 0x00 => Variant::NCS,
x if x & 0xc0 == 0x80 => Variant::RFC4122,
x if x & 0xc0 == 0x80 => Variant::RFC,
x if x & 0xe0 == 0xc0 => Variant::Microsoft,
x if x & 0xe0 == 0xe0 => Variant::Future,
// The above match arms are actually exhaustive
Expand Down Expand Up @@ -1311,12 +1330,20 @@ mod tests {
let uuid5 = Uuid::parse_str("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap();
let uuid6 = Uuid::parse_str("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap();

assert_eq!(uuid1.get_variant(), Variant::RFC4122);
assert_eq!(uuid2.get_variant(), Variant::RFC4122);
assert_eq!(uuid3.get_variant(), Variant::RFC4122);
assert_eq!(uuid1.get_variant(), Variant::RFC);
assert_eq!(uuid2.get_variant(), Variant::RFC);
assert_eq!(uuid3.get_variant(), Variant::RFC);
assert_eq!(uuid4.get_variant(), Variant::Microsoft);
assert_eq!(uuid5.get_variant(), Variant::Microsoft);
assert_eq!(uuid6.get_variant(), Variant::NCS);

// test deprecation
#[allow(deprecated)]
{
assert_eq!(uuid1.get_variant(), Variant::RFC4122);
assert_eq!(uuid2.get_variant(), Variant::RFC4122);
assert_eq!(uuid3.get_variant(), Variant::RFC4122);
}
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Uuid {
/// let uuid = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000")?;
///
/// assert_eq!(Some(Version::Random), uuid.get_version());
/// assert_eq!(Variant::RFC4122, uuid.get_variant());
/// assert_eq!(Variant::RFC, uuid.get_variant());
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Uuid {
/// let uuid = Uuid::try_parse("550e8400-e29b-41d4-a716-446655440000")?;
///
/// assert_eq!(Some(Version::Random), uuid.get_version());
/// assert_eq!(Variant::RFC4122, uuid.get_variant());
/// assert_eq!(Variant::RFC, uuid.get_variant());
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Uuid {
/// let uuid = Uuid::try_parse_ascii(b"550e8400-e29b-41d4-a716-446655440000")?;
///
/// assert_eq!(Some(Version::Random), uuid.get_version());
/// assert_eq!(Variant::RFC4122, uuid.get_variant());
/// assert_eq!(Variant::RFC, uuid.get_variant());
/// # Ok(())
/// # }
/// ```
Expand Down
4 changes: 2 additions & 2 deletions src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ mod tests {
let uuid = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);

assert_eq!(uuid.get_version(), Some(Version::Mac));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
assert_eq!(
uuid.hyphenated().to_string(),
"20616934-4ba2-11e7-8000-010203040506"
Expand Down Expand Up @@ -164,6 +164,6 @@ mod tests {
let uuid = Uuid::now_v1(&node);

assert_eq!(uuid.get_version(), Some(Version::Mac));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
}
}
2 changes: 1 addition & 1 deletion src/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ mod tests {
for &(ref ns, ref name, _) in FIXTURE {
let uuid = Uuid::new_v3(*ns, name.as_bytes());
assert_eq!(uuid.get_version(), Some(Version::Md5));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod tests {
let uuid = Uuid::new_v4();

assert_eq!(uuid.get_version(), Some(Version::Random));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod tests {
let uuid = Uuid::new_v5(*ns, name.as_bytes());

assert_eq!(uuid.get_version(), Some(Version::Sha1));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
assert_eq!(Ok(uuid), u.parse());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/v6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mod tests {
let uuid = Uuid::new_v6(Timestamp::from_unix(context, time, time_fraction), &node);

assert_eq!(uuid.get_version(), Some(Version::SortMac));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
assert_eq!(
uuid.hyphenated().to_string(),
"1e74ba22-0616-6934-8000-010203040506"
Expand Down Expand Up @@ -166,6 +166,6 @@ mod tests {
let uuid = Uuid::now_v6(&node);

assert_eq!(uuid.get_version(), Some(Version::SortMac));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
}
}
6 changes: 3 additions & 3 deletions src/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mod tests {
let uustr = uuid.hyphenated().to_string();

assert_eq!(uuid.get_version(), Some(Version::SortRand));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
assert!(uuid.hyphenated().to_string().starts_with("017f22e2-79b0-7"));

// Ensure parsing the same UUID produces the same timestamp
Expand All @@ -150,7 +150,7 @@ mod tests {
let uuid = Uuid::now_v7();

assert_eq!(uuid.get_version(), Some(Version::SortRand));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
}

#[test]
Expand Down Expand Up @@ -232,7 +232,7 @@ mod tests {
let uuid = Uuid::new_v7(ts);

assert_eq!(uuid.get_version(), Some(Version::SortRand));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);

let decoded_ts = uuid.get_timestamp().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod tests {
];
let uuid = Uuid::new_v8(buf);
assert_eq!(uuid.get_version(), Some(Version::Custom));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
assert_eq!(uuid.get_variant(), Variant::RFC);
assert_eq!(uuid.get_version_num(), 8);
assert_eq!(
uuid.hyphenated().to_string(),
Expand Down

0 comments on commit 6f6cafb

Please sign in to comment.