Skip to content

Commit

Permalink
Re-export errors from ops and unexport ops module (#282, #274)
Browse files Browse the repository at this point in the history
Re-export error types from the `ops` module from the root of the crate.
And unexport the `ops` module itself.

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.

Co-authored-by: Kai Ren <tyranron@gmail.com>
  • Loading branch information
JelteF and tyranron authored Jul 27, 2023
1 parent f067867 commit 77f8002
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 83 deletions.
12 changes: 6 additions & 6 deletions impl/doc/add.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ Code like this will be generated:
# Unit,
# }
impl ::core::ops::Add for MixedInts {
type Output = Result<MixedInts, ::derive_more::ops::BinaryError>;
fn add(self, rhs: MixedInts) -> Result<MixedInts, ::derive_more::ops::BinaryError> {
type Output = Result<MixedInts, ::derive_more::BinaryError>;
fn add(self, rhs: MixedInts) -> Result<MixedInts, ::derive_more::BinaryError> {
match (self, rhs) {
(MixedInts::SmallInt(__l_0), MixedInts::SmallInt(__r_0)) => {
Ok(MixedInts::SmallInt(__l_0.add(__r_0)))
Expand All @@ -138,11 +138,11 @@ impl ::core::ops::Add for MixedInts {
(MixedInts::UnsignedTwo(__l_0), MixedInts::UnsignedTwo(__r_0)) => {
Ok(MixedInts::UnsignedTwo(__l_0.add(__r_0)))
}
(MixedInts::Unit, MixedInts::Unit) => Err(::derive_more::ops::BinaryError::Unit(
::derive_more::ops::UnitError::new("add"),
(MixedInts::Unit, MixedInts::Unit) => Err(::derive_more::BinaryError::Unit(
::derive_more::UnitError::new("add"),
)),
_ => Err(::derive_more::ops::BinaryError::Mismatch(
::derive_more::ops::WrongVariantError::new("add"),
_ => Err(::derive_more::BinaryError::Mismatch(
::derive_more::WrongVariantError::new("add"),
)),
}
}
Expand Down
6 changes: 3 additions & 3 deletions impl/doc/not.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ Code like this will be generated:
# Unit,
# }
impl ::core::ops::Not for EnumWithUnit {
type Output = Result<EnumWithUnit, ::derive_more::ops::UnitError>;
fn not(self) -> Result<EnumWithUnit, ::derive_more::ops::UnitError> {
type Output = Result<EnumWithUnit, ::derive_more::UnitError>;
fn not(self) -> Result<EnumWithUnit, ::derive_more::UnitError> {
match self {
EnumWithUnit::SmallInt(__0) => Ok(EnumWithUnit::SmallInt(__0.not())),
EnumWithUnit::Unit => Err(::derive_more::ops::UnitError::new("not")),
EnumWithUnit::Unit => Err(::derive_more::UnitError::new("not")),
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions impl/src/add_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {
},
Data::Enum(ref data_enum) => (
quote! {
::core::result::Result<#input_type #ty_generics, ::derive_more::ops::BinaryError>
::core::result::Result<#input_type #ty_generics, ::derive_more::BinaryError>
},
enum_content(input_type, data_enum, &method_ident),
),
Expand Down Expand Up @@ -126,8 +126,8 @@ fn enum_content(
let operation_name = method_ident.to_string();
matches.push(quote! {
(#subtype, #subtype) => ::core::result::Result::Err(
::derive_more::ops::BinaryError::Unit(
::derive_more::ops::UnitError::new(#operation_name)
::derive_more::BinaryError::Unit(
::derive_more::UnitError::new(#operation_name)
)
)
});
Expand All @@ -140,8 +140,8 @@ fn enum_content(
// match.
let operation_name = method_ident.to_string();
matches.push(quote! {
_ => ::core::result::Result::Err(::derive_more::ops::BinaryError::Mismatch(
::derive_more::ops::WrongVariantError::new(#operation_name)
_ => ::core::result::Result::Err(::derive_more::BinaryError::Mismatch(
::derive_more::WrongVariantError::new(#operation_name)
))
});
}
Expand Down
4 changes: 2 additions & 2 deletions impl/src/not_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn enum_output_type_and_content(
let operation_name = method_ident.to_string();
matches.push(quote! {
#subtype => ::core::result::Result::Err(
::derive_more::ops::UnitError::new(#operation_name)
::derive_more::UnitError::new(#operation_name)
)
});
}
Expand All @@ -162,7 +162,7 @@ fn enum_output_type_and_content(
};

let output_type = if has_unit_type {
quote! { ::core::result::Result<#input_type #ty_generics, ::derive_more::ops::UnitError> }
quote! { ::core::result::Result<#input_type #ty_generics, ::derive_more::UnitError> }
} else {
quote! { #input_type #ty_generics }
};
Expand Down
64 changes: 64 additions & 0 deletions src/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Definitions used in derived implementations of [`core::ops::Add`]-like traits.

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(),
}
}
}
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,17 @@ pub mod __private {
pub use crate::vendor::thiserror::aserror::AsDynError;
}

// The modules containing error types and other helpers
// The modules containing error types and other helpers.

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

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

#[cfg(feature = "debug")]
mod fmt;
Expand All @@ -83,7 +91,7 @@ pub use crate::convert::TryIntoError;
mod try_unwrap;
#[cfg(feature = "try_unwrap")]
#[doc(inline)]
pub use self::try_unwrap::TryUnwrapError;
pub use crate::try_unwrap::TryUnwrapError;

// When re-exporting traits from std we need to do a pretty crazy trick, because we ONLY want
// to re-export the traits and not derives that are called the same in the std module,
Expand All @@ -99,6 +107,7 @@ macro_rules! re_export_traits((
$feature:literal, $new_module_name:ident, $module:path $(, $traits:ident)* $(,)?) => {
#[cfg(all(feature = $feature, any(not(docsrs), ci)))]
mod $new_module_name {
#[doc(hidden)]
pub use $module::{$($traits),*};
}

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 77f8002

Please sign in to comment.