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

Wrap getrandom::Error in uuid::Error for Uuid::new_v4 #502

Closed
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use crate::{builder, parser};
/// A general error that can occur when working with UUIDs.
// TODO: improve the doc
// BODY: This detail should be fine for initial merge
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Error(Inner);

// TODO: write tests for Error
// BODY: not immediately blocking, but should be covered for 1.0
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
enum Inner {
/// An error occurred while handling [`Uuid`] bytes.
///
Expand All @@ -26,6 +26,12 @@ enum Inner {
/// [`parser::ParseError`]: parser/enum.ParseError.html
/// [`Uuid`]: struct.Uuid.html
Parser(parser::Error),

#[cfg(feature = "getrandom")]
/// An error occurred while getting random bytes.
///
/// See [`getrandom::Error`]
Random(getrandom::Error),
}

impl From<builder::Error> for Error {
Expand All @@ -40,11 +46,20 @@ impl From<parser::Error> for Error {
}
}

#[cfg(feature = "getrandom")]
impl From<getrandom::Error> for Error {
fn from(err: getrandom::Error) -> Self {
Error(Inner::Random(err))
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
Inner::Build(ref err) => fmt::Display::fmt(&err, f),
Inner::Parser(ref err) => fmt::Display::fmt(&err, f),
#[cfg(feature = "getrandom")]
Inner::Random(ref err) => fmt::Display::fmt(&err, f),
}
}
}
Expand All @@ -59,6 +74,13 @@ mod std_support {
match self.0 {
Inner::Build(ref err) => Some(err),
Inner::Parser(ref err) => Some(err),
#[cfg(feature = "getrandom")]
// NB: getrandom::Error only impls the Error trait with the
// getrandom/std feature which we cannot activate via our std
// feature without also activating the optional dependency, even
// when not desired (not directly activated). So we just don't
// forward the source for now, as the less bad option.
Inner::Random(_) => None,
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ impl Uuid {
///
/// [`getrandom`]: https://crates.io/crates/getrandom
/// [from_bytes]: struct.Builder.html#method.from_bytes
// TODO: change signature to support uuid's Error.
pub fn new_v4() -> Result<Uuid, getrandom::Error> {
pub fn new_v4() -> Result<Uuid, crate::Error> {
let mut bytes = [0u8; 16];
getrandom::getrandom(&mut bytes)?;

Expand Down