Skip to content

Commit

Permalink
Re-export errors from ops and unexport ops module
Browse files Browse the repository at this point in the history
This also moves errors that are specific to add to a separate file.
Otherwise rustdoc would show them to be included for the features:
"(add or not) and add", which is obviously equivalent to add but reads
super weird.
  • Loading branch information
JelteF committed Jul 26, 2023
1 parent f067867 commit 0d40176
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 65 deletions.
61 changes: 61 additions & 0 deletions src/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use core::fmt;
use crate::UnitError;

/// Error returned by the derived implementations when an arithmetic or logic
/// operation is invoked on mismatched enum variants.
#[derive(Clone, Copy, Debug)]
pub struct WrongVariantError {
operation_name: &'static str,
}

impl WrongVariantError {
#[doc(hidden)]
#[must_use]
#[inline]
pub const fn new(operation_name: &'static str) -> Self {
Self { operation_name }
}
}

impl fmt::Display for WrongVariantError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Trying to {}() mismatched enum variants",
self.operation_name
)
}
}

#[cfg(feature = "std")]
impl std::error::Error for WrongVariantError {}

/// Possible errors returned by the derived implementations of binary
/// arithmetic or logic operations.
#[derive(Clone, Copy, Debug)]
pub enum BinaryError {
/// Operation is attempted between mismatched enum variants.
Mismatch(WrongVariantError),

/// Operation is attempted on unit-like enum variants.
Unit(UnitError),
}

impl fmt::Display for BinaryError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Mismatch(e) => write!(f, "{e}"),
Self::Unit(e) => write!(f, "{e}"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for BinaryError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Mismatch(e) => e.source(),
Self::Unit(e) => e.source(),
}
}
}
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ pub mod __private {

// The modules containing error types and other helpers
#[cfg(any(feature = "add", feature = "not"))]
pub mod ops;
mod ops;

#[cfg(any(feature = "add", feature = "not"))]
pub use crate::ops::UnitError;

#[cfg(feature = "add")]
mod add;
#[cfg(feature = "add")]
pub use crate::add::{BinaryError, WrongVariantError};

#[cfg(feature = "debug")]
mod fmt;
Expand Down
64 changes: 0 additions & 64 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,3 @@ impl fmt::Display for UnitError {

#[cfg(feature = "std")]
impl std::error::Error for UnitError {}

#[cfg(feature = "add")]
/// Error returned by the derived implementations when an arithmetic or logic
/// operation is invoked on mismatched enum variants.
#[derive(Clone, Copy, Debug)]
pub struct WrongVariantError {
operation_name: &'static str,
}

#[cfg(feature = "add")]
impl WrongVariantError {
#[doc(hidden)]
#[must_use]
#[inline]
pub const fn new(operation_name: &'static str) -> Self {
Self { operation_name }
}
}

#[cfg(feature = "add")]
impl fmt::Display for WrongVariantError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Trying to {}() mismatched enum variants",
self.operation_name
)
}
}

#[cfg(all(feature = "add", feature = "std"))]
impl std::error::Error for WrongVariantError {}

#[cfg(feature = "add")]
/// Possible errors returned by the derived implementations of binary
/// arithmetic or logic operations.
#[derive(Clone, Copy, Debug)]
pub enum BinaryError {
/// Operation is attempted between mismatched enum variants.
Mismatch(WrongVariantError),

/// Operation is attempted on unit-like enum variants.
Unit(UnitError),
}

#[cfg(feature = "add")]
impl fmt::Display for BinaryError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Mismatch(e) => write!(f, "{e}"),
Self::Unit(e) => write!(f, "{e}"),
}
}
}

#[cfg(all(feature = "add", feature = "std"))]
impl std::error::Error for BinaryError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Mismatch(e) => e.source(),
Self::Unit(e) => e.source(),
}
}
}

0 comments on commit 0d40176

Please sign in to comment.