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

introduce UuidError for general Uuid use #284

Merged
merged 4 commits into from
Jul 24, 2018
Merged
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
11 changes: 11 additions & 0 deletions src/core_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ impl fmt::Display for UuidVariant {
}
}

impl fmt::Display for ::UuidError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"invalid bytes length: expected {}, found {}",
self.expected(),
self.found()
)
}
}

impl fmt::LowerHex for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(&self.to_hyphenated_ref(), f)
Expand Down
94 changes: 81 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ mod v5;
/// A 128-bit (16 byte) buffer containing the ID.
pub type UuidBytes = [u8; 16];

/// The error that can occur when creating a [`Uuid`].
///
/// [`Uuid`]: struct.Uuid.html
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct UuidError {
expected: usize,
found: usize,
}

/// The version of the UUID, denoting the generating algorithm.
#[derive(Debug, PartialEq, Copy, Clone)]
#[repr(C)]
Expand Down Expand Up @@ -252,6 +261,60 @@ const GROUP_LENS: [u8; 5] = [8, 4, 4, 4, 12];
// Accumulated length of each hyphenated group in hex digits.
const ACC_GROUP_LENS: [u8; 5] = [8, 12, 16, 20, 32];

impl UuidError {
/// The expected number of bytes.
#[cfg(feature = "const_fn")]
#[inline]
pub const fn expected(&self) -> usize {
self.expected
}

/// The expected number of bytes.
#[cfg(not(feature = "const_fn"))]
#[inline]
pub fn expected(&self) -> usize {
self.expected
}

/// The number of bytes found.
#[cfg(feature = "const_fn")]
#[inline]
pub const fn found(&self) -> usize {
self.found
}

/// The number of bytes found.
#[cfg(not(feature = "const_fn"))]
#[inline]
pub fn found(&self) -> usize {
self.found
}

/// Create a new [`UuidError`].
///
/// [`UuidError`]: struct.UuidError.html
#[cfg(feature = "const_fn")]
#[inline]
pub const fn new(expected: usize, found: usize) -> Self {
UuidError {
expected: expected,
found: found,
}
}

/// Create a new [`UuidError`].
///
/// [`UuidError`]: struct.UuidError.html
#[cfg(not(feature = "const_fn"))]
#[inline]
pub fn new(expected: usize, found: usize) -> Self {
UuidError {
expected: expected,
found: found,
}
}
}

impl Uuid {
/// The 'nil UUID'.
///
Expand Down Expand Up @@ -332,14 +395,13 @@ impl Uuid {
/// An invalid length:
///
/// ```
/// use uuid::ParseError;
/// use uuid::Uuid;
/// use uuid::prelude::*;
///
/// let d4 = [12];
///
/// let uuid = Uuid::from_fields(42, 12, 5, &d4);
/// let uuid = uuid::Uuid::from_fields(42, 12, 5, &d4);
///
/// let expected_uuid = Err(ParseError::InvalidLength(1));
/// let expected_uuid = Err(uuid::UuidError::new(8, d4.len()));
///
/// assert_eq!(expected_uuid, uuid);
/// ```
Expand All @@ -348,9 +410,13 @@ impl Uuid {
d2: u16,
d3: u16,
d4: &[u8],
) -> Result<Uuid, ParseError> {
if d4.len() != 8 {
return Err(ParseError::InvalidLength(d4.len()));
) -> Result<Uuid, UuidError> {
const D4_LEN: usize = 8;

let len = d4.len();

if len != D4_LEN {
return Err(UuidError::new(D4_LEN, len));
}

Ok(Uuid::from_uuid_bytes([
Expand Down Expand Up @@ -400,21 +466,23 @@ impl Uuid {
/// An incorrect number of bytes:
///
/// ```
/// use uuid::ParseError;
/// use uuid::Uuid;
/// use uuid::prelude::*;
///
/// let bytes = [4, 54, 67, 12, 43, 2, 98, 76];
///
/// let uuid = Uuid::from_bytes(&bytes);
///
/// let expected_uuid = Err(ParseError::InvalidLength(8));
/// let expected_uuid = Err(uuid::UuidError::new(16, 8));
///
/// assert_eq!(expected_uuid, uuid);
/// ```
pub fn from_bytes(b: &[u8]) -> Result<Uuid, ParseError> {
pub fn from_bytes(b: &[u8]) -> Result<Uuid, UuidError> {
const BYTES_LEN: usize = 16;

let len = b.len();
if len != 16 {
return Err(ParseError::InvalidLength(len));

if len != BYTES_LEN {
return Err(UuidError::new(BYTES_LEN, len));
}

let mut bytes: UuidBytes = [0; 16];
Expand Down
6 changes: 6 additions & 0 deletions src/std_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ impl error::Error for ParseError {
"UUID parse error"
}
}

impl error::Error for ::UuidError {
fn description(&self) -> &str {
"Uuid error"
}
}
9 changes: 6 additions & 3 deletions src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ impl Uuid {
seconds: u64,
nano_seconds: u32,
node_id: &[u8],
) -> Result<Self, super::ParseError>
) -> Result<Self, ::UuidError>
where
T: UuidClockSequence,
{
if node_id.len() != 6 {
return Err(super::ParseError::InvalidLength(node_id.len()));
const NODE_ID_LEN: usize = 6;

let len = node_id.len();
if len != NODE_ID_LEN {
return Err(::UuidError::new(NODE_ID_LEN, len));
}

let time_low;
Expand Down