Skip to content

Commit

Permalink
simplify internal module layout
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Oct 29, 2021
1 parent 40c4b16 commit 9c07eb0
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 225 deletions.
50 changes: 29 additions & 21 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,30 @@
//!
//! [`Uuid`]: ../struct.Uuid.html

use crate::error::*;
use crate::prelude::*;
use crate::{error::*, Bytes, Uuid, Variant, Version};

/// A builder struct for creating a UUID.
///
/// # Examples
///
/// Creating a v4 UUID from externally generated bytes:
///
/// ```
/// use uuid::{Builder, Variant, Version};
///
/// # let rng = || [
/// # 70, 235, 208, 238, 14, 109, 67, 201, 185, 13, 204, 195, 90,
/// # 145, 63, 62,
/// # ];
/// let random_bytes = rng();
/// let uuid = Builder::from_bytes(random_bytes)
/// .set_variant(Variant::RFC4122)
/// .set_version(Version::Random)
/// .build();
/// ```
#[allow(missing_copy_implementations)]
#[derive(Debug)]
pub struct Builder(Bytes);

impl Uuid {
/// The 'nil UUID'.
Expand Down Expand Up @@ -63,12 +85,7 @@ impl Uuid {
/// "ab3f1097-501e-b736-0c03-0938362b0809"
/// );
/// ```
pub const fn from_fields(
d1: u32,
d2: u16,
d3: u16,
d4: &[u8; 8],
) -> Uuid {
pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid {
Uuid::from_bytes([
(d1 >> 24) as u8,
(d1 >> 16) as u8,
Expand Down Expand Up @@ -113,12 +130,7 @@ impl Uuid {
/// "97103fab-1e50-36b7-0c03-0938362b0809"
/// );
/// ```
pub const fn from_fields_le(
d1: u32,
d2: u16,
d3: u16,
d4: &[u8],
) -> Uuid {
pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: &[u8]) -> Uuid {
Uuid::from_bytes([
d1 as u8,
(d1 >> 8) as u8,
Expand Down Expand Up @@ -307,7 +319,8 @@ impl Uuid {
return Err(ErrorKind::InvalidLength {
expected: ExpectedLength::Exact(16),
found: b.len(),
}.into());
}
.into());
}

let mut bytes: Bytes = [0; 16];
Expand Down Expand Up @@ -427,12 +440,7 @@ impl Builder {
/// "ab3f1097-501e-b736-0c03-0938362b0809"
/// );
/// ```
pub const fn from_fields(
d1: u32,
d2: u16,
d3: u16,
d4: &[u8; 8],
) -> Self {
pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self {
Builder::from_bytes(*Uuid::from_fields(d1, d2, d3, d4).as_bytes())
}

Expand Down
19 changes: 12 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ impl From<ErrorKind> for Error {

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: ", match self.0 {
ErrorKind::InvalidCharacter { .. } => "invalid character",
ErrorKind::InvalidGroupCount { .. } => "invalid number of groups",
ErrorKind::InvalidGroupLength { .. } => "invalid group length",
ErrorKind::InvalidLength { .. } => "invalid length",
})?;
write!(
f,
"{}: ",
match self.0 {
ErrorKind::InvalidCharacter { .. } => "invalid character",
ErrorKind::InvalidGroupCount { .. } =>
"invalid number of groups",
ErrorKind::InvalidGroupLength { .. } => "invalid group length",
ErrorKind::InvalidLength { .. } => "invalid length",
}
)?;

match self.0 {
ErrorKind::InvalidCharacter {
Expand Down Expand Up @@ -139,5 +144,5 @@ mod std_support {
use super::*;
use crate::std::error;

impl error::Error for Error { }
impl error::Error for Error {}
}
8 changes: 5 additions & 3 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

//! Adapters for various formats for UUIDs

use crate::prelude::*;
use crate::std::{borrow::Borrow, fmt, str};
use crate::{
std::{borrow::Borrow, fmt, str},
Uuid, Variant,
};

impl std::fmt::Debug for Uuid {
#[inline]
Expand Down Expand Up @@ -1060,7 +1062,7 @@ impl_fmt_traits! {

#[cfg(test)]
mod tests {
use crate::prelude::*;
use super::*;

#[test]
fn hyphenated_trailing() {
Expand Down

0 comments on commit 9c07eb0

Please sign in to comment.