#![cfg_attr(not(feature = "std"), no_std)]
#![deny(clippy::all, missing_docs, unsafe_code)]
#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;
use core::fmt::{self, Debug, Display};
mod printer;
pub struct Comparison<'a, TLeft, TRight>
where
TLeft: ?Sized,
TRight: ?Sized,
{
left: &'a TLeft,
right: &'a TRight,
}
impl<'a, TLeft, TRight> Comparison<'a, TLeft, TRight>
where
TLeft: ?Sized,
TRight: ?Sized,
{
pub fn new(left: &'a TLeft, right: &'a TRight) -> Comparison<'a, TLeft, TRight> {
Comparison { left, right }
}
}
impl<'a, TLeft, TRight> Display for Comparison<'a, TLeft, TRight>
where
TLeft: Debug + ?Sized,
TRight: Debug + ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let left_debug = format!("{:#?}", self.left);
let right_debug = format!("{:#?}", self.right);
printer::write_header(f)?;
printer::write_lines(f, &left_debug, &right_debug)
}
}
pub struct StrComparison<'a, TLeft, TRight>
where
TLeft: ?Sized,
TRight: ?Sized,
{
left: &'a TLeft,
right: &'a TRight,
}
impl<'a, TLeft, TRight> StrComparison<'a, TLeft, TRight>
where
TLeft: AsRef<str> + ?Sized,
TRight: AsRef<str> + ?Sized,
{
pub fn new(left: &'a TLeft, right: &'a TRight) -> StrComparison<'a, TLeft, TRight> {
StrComparison { left, right }
}
}
impl<'a, TLeft, TRight> Display for StrComparison<'a, TLeft, TRight>
where
TLeft: AsRef<str> + ?Sized,
TRight: AsRef<str> + ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
printer::write_header(f)?;
printer::write_lines(f, self.left.as_ref(), self.right.as_ref())
}
}
#[macro_export]
macro_rules! assert_eq {
($left:expr, $right:expr$(,)?) => ({
$crate::assert_eq!(@ $left, $right, "", "");
});
($left:expr, $right:expr, $($arg:tt)*) => ({
$crate::assert_eq!(@ $left, $right, ": ", $($arg)+);
});
(@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({
match (&($left), &($right)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
use $crate::private::CreateComparison;
::core::panic!("assertion failed: `(left == right)`{}{}\
\n\
\n{}\
\n",
$maybe_colon,
format_args!($($arg)*),
(left_val, right_val).create_comparison()
)
}
}
}
});
}
#[macro_export]
macro_rules! assert_str_eq {
($left:expr, $right:expr$(,)?) => ({
$crate::assert_str_eq!(@ $left, $right, "", "");
});
($left:expr, $right:expr, $($arg:tt)*) => ({
$crate::assert_str_eq!(@ $left, $right, ": ", $($arg)+);
});
(@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({
match (&($left), &($right)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
::core::panic!("assertion failed: `(left == right)`{}{}\
\n\
\n{}\
\n",
$maybe_colon,
format_args!($($arg)*),
$crate::StrComparison::new(left_val, right_val)
)
}
}
}
});
}
#[macro_export]
macro_rules! assert_ne {
($left:expr, $right:expr$(,)?) => ({
$crate::assert_ne!(@ $left, $right, "", "");
});
($left:expr, $right:expr, $($arg:tt)+) => ({
$crate::assert_ne!(@ $left, $right, ": ", $($arg)+);
});
(@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)+) => ({
match (&($left), &($right)) {
(left_val, right_val) => {
if *left_val == *right_val {
::core::panic!("assertion failed: `(left != right)`{}{}\
\n\
\nBoth sides:\
\n{:#?}\
\n\
\n",
$maybe_colon,
format_args!($($arg)+),
left_val
)
}
}
}
});
}
#[cfg(feature = "unstable")]
#[macro_export]
macro_rules! assert_matches {
($left:expr, $( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => ({
match $left {
$( $pattern )|+ $( if $guard )? => {}
ref left_val => {
$crate::assert_matches!(
@
left_val,
::core::stringify!($($pattern)|+ $(if $guard)?),
"",
""
);
}
}
});
($left:expr, $( $pattern:pat )|+ $( if $guard: expr )?, $($arg:tt)+) => ({
match $left {
$( $pattern )|+ $( if $guard )? => {}
ref left_val => {
$crate::assert_matches!(
@
left_val,
::core::stringify!($($pattern)|+ $(if $guard)?),
": ",
$($arg)+
);
}
}
});
(@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({
match (&($left), &($right)) {
(left_val, right_val) => {
struct Pattern<'a>(&'a str);
impl ::core::fmt::Debug for Pattern<'_> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
::core::fmt::Display::fmt(self.0, f)
}
}
::core::panic!("assertion failed: `(left matches right)`{}{}\
\n\
\n{}\
\n",
$maybe_colon,
format_args!($($arg)*),
$crate::Comparison::new(left_val, &Pattern(right_val))
)
}
}
});
}
#[doc(hidden)]
pub mod private {
#[cfg(feature = "alloc")]
use alloc::string::String;
pub trait CompareAsStrByDefault: AsRef<str> {}
impl CompareAsStrByDefault for str {}
impl CompareAsStrByDefault for String {}
impl<T: CompareAsStrByDefault + ?Sized> CompareAsStrByDefault for &T {}
pub trait CreateComparison {
type Comparison;
fn create_comparison(self) -> Self::Comparison;
}
impl<'a, T, U> CreateComparison for &'a (T, U) {
type Comparison = crate::Comparison<'a, T, U>;
fn create_comparison(self) -> Self::Comparison {
crate::Comparison::new(&self.0, &self.1)
}
}
impl<'a, T, U> CreateComparison for (&'a T, &'a U)
where
T: CompareAsStrByDefault + ?Sized,
U: CompareAsStrByDefault + ?Sized,
{
type Comparison = crate::StrComparison<'a, T, U>;
fn create_comparison(self) -> Self::Comparison {
crate::StrComparison::new(self.0, self.1)
}
}
}