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

Add Error type #373

Merged
merged 11 commits into from
Mar 17, 2019
15 changes: 15 additions & 0 deletions src/core_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,28 @@ use core::{fmt, str};
use parser;
use prelude::*;

impl From<super::BytesError> for super::Error {
fn from(err: super::BytesError) -> Self {
super::Error::Bytes(err)
}
}

impl fmt::Debug for Uuid {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(self, f)
}
}

impl fmt::Display for super::Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
super::Error::Bytes(ref err) => fmt::Display::fmt(&err, f),
super::Error::Parse(ref err) => fmt::Display::fmt(&err, f),
kinggoesgaming marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

impl fmt::Display for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(self, f)
Expand Down
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,35 @@ pub struct BytesError {
found: usize,
}

/// A general error that can occur when handling [`Uuid`]s.
///
/// Although specialized error types exist in the crate,
/// sometimes where particular error type occurred is hidden
/// until errors need to be handled. This allows to enumerate
/// the errors.
///
/// [`Uuid`]: struct.Uuid.html
// TODO: improve the doc
// BODY: This detail should be fine for initial merge
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Error {
kinggoesgaming marked this conversation as resolved.
Show resolved Hide resolved
/// An error occurred while handling [`Uuid`] bytes.
///
/// See [`BytesError`]
///
/// [`BytesError`]: struct.BytesError.html
/// [`Uuid`]: struct.Uuid.html
Bytes(BytesError),

/// An error occurred while parsing a [`Uuid`] string.
///
/// See [`parser::ParseError`]
///
/// [`parser::ParseError`]: parser/enum.ParseError.html
/// [`Uuid`]: struct.Uuid.html
Parse(parser::ParseError),
}

/// The version of the UUID, denoting the generating algorithm.
#[derive(Debug, PartialEq, Copy, Clone)]
#[repr(C)]
Expand Down
6 changes: 6 additions & 0 deletions src/parser/core_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
use core::fmt;
use parser;

impl From<parser::ParseError> for ::Error {
fn from(err: parser::ParseError) -> Self {
::Error::Parse(err)
}
}

impl<'a> fmt::Display for parser::Expected {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down
3 changes: 2 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
//!
//! Currently the prelude reexports the following:
//!
//! [`uuid`]`::{`[`Uuid`], [`Variant`], [`Version`], builder::[`Builder`]`}`:
//! [`uuid`]`::{`[`Error`], [`Uuid`], [`Variant`], [`Version`], builder::[`Builder`]`}`:
//! The fundamental types used in [`uuid`] crate.
//!
//! [`uuid`]: ../index.html
//! [`Error`]: ../enum.Error.html
//! [`Uuid`]: ../struct.Uuid.html
//! [`Variant`]: ../enum.Variant.html
//! [`Version`]: ../enum.Version.html
Expand Down
12 changes: 11 additions & 1 deletion src/std_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@

use std::error;

impl error::Error for ::BytesError {
impl error::Error for super::BytesError {
fn description(&self) -> &str {
"invalid number of uuid bytes"
}
}

impl error::Error for super::Error {
fn description(&self) -> &str {
match *self {
super::Error::Bytes(ref err) => error::Error::description(err),
super::Error::Parse(ref err) => error::Error::description(err),
kinggoesgaming marked this conversation as resolved.
Show resolved Hide resolved
}
}
}