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

renaming rfc4122 functions #754

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
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
53 changes: 41 additions & 12 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 @@ -550,14 +550,16 @@ impl Builder {
}

/// Creates a `Builder` for a version 1 UUID using the supplied timestamp and node ID.
pub const fn from_rfc4122_timestamp(ticks: u64, counter: u16, node_id: &[u8; 6]) -> Self {
Builder(timestamp::encode_rfc4122_timestamp(ticks, counter, node_id))
pub const fn from_gregorian_timestamp(ticks: u64, counter: u16, node_id: &[u8; 6]) -> Self {
Builder(timestamp::encode_gregorian_timestamp(
ticks, counter, node_id,
))
}

/// 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 @@ -578,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 @@ -592,19 +594,19 @@ 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)
}

/// Creates a `Builder` for a version 6 UUID using the supplied timestamp and node ID.
///
/// This method will encode the ticks, counter, and node ID in a sortable UUID.
pub const fn from_sorted_rfc4122_timestamp(
pub const fn from_sorted_gregorian_timestamp(
ticks: u64,
counter: u16,
node_id: &[u8; 6],
) -> Self {
Builder(timestamp::encode_sorted_rfc4122_timestamp(
Builder(timestamp::encode_sorted_gregorian_timestamp(
ticks, counter, node_id,
))
}
Expand Down Expand Up @@ -634,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 @@ -651,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 @@ -844,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 Expand Up @@ -903,3 +908,27 @@ impl Builder {
self.0
}
}

// Deprecations. Remove when major version changes (2.0.0)
#[doc(hidden)]
impl Builder {
#[deprecated(
since = "1.10.0",
note = "Deprecated! Use `from_gregorian_timestamp()` instead!"
)]
pub const fn from_rfc4122_timestamp(ticks: u64, counter: u16, node_id: &[u8; 6]) -> Self {
Builder::from_gregorian_timestamp(ticks, counter, node_id)
}

#[deprecated(
since = "1.10.0",
note = "Deprecated! Use `from_sorted_gregorian_timestamp()` instead!"
)]
pub const fn from_sorted_rfc4122_timestamp(
ticks: u64,
counter: u16,
node_id: &[u8; 6],
) -> Self {
Builder::from_sorted_gregorian_timestamp(ticks, counter, node_id)
}
}
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!(format!("{}", Variant::RFC4122), "RFC4122");
}
}
51 changes: 39 additions & 12 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.10.0", 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 @@ -889,14 +908,14 @@ impl Uuid {
pub const fn get_timestamp(&self) -> Option<Timestamp> {
match self.get_version() {
Some(Version::Mac) => {
let (ticks, counter) = timestamp::decode_rfc4122_timestamp(self);
let (ticks, counter) = timestamp::decode_gregorian_timestamp(self);

Some(Timestamp::from_rfc4122(ticks, counter))
Some(Timestamp::from_gregorian(ticks, counter))
}
Some(Version::SortMac) => {
let (ticks, counter) = timestamp::decode_sorted_rfc4122_timestamp(self);
let (ticks, counter) = timestamp::decode_sorted_gregorian_timestamp(self);

Some(Timestamp::from_rfc4122(ticks, counter))
Some(Timestamp::from_gregorian(ticks, counter))
}
Some(Version::SortRand) => {
let millis = timestamp::decode_unix_timestamp_millis(self);
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
Loading
Loading