//! Format wrappers for Zebra use std::{fmt, ops}; #[cfg(any(test, feature = "proptest-impl"))] use proptest::prelude::*; #[cfg(any(test, feature = "proptest-impl"))] use proptest_derive::Arbitrary; /// Wrapper to override `Debug`, redirecting it to only output the type's name. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))] pub struct TypeNameToDebug(pub T); impl fmt::Debug for TypeNameToDebug { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(std::any::type_name::()) } } impl ops::Deref for TypeNameToDebug { type Target = T; fn deref(&self) -> &Self::Target { &self.0 } } impl ops::DerefMut for TypeNameToDebug { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl From for TypeNameToDebug { fn from(t: T) -> Self { Self(t) } } /// Wrapper to override `Debug`, redirecting it to the `Display` impl. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))] pub struct DisplayToDebug(pub T); impl fmt::Debug for DisplayToDebug { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl ops::Deref for DisplayToDebug { type Target = T; fn deref(&self) -> &Self::Target { &self.0 } } impl ops::DerefMut for DisplayToDebug { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl From for DisplayToDebug { fn from(t: T) -> Self { Self(t) } } /// Wrapper to override `Debug` to display a shorter summary of the type. /// /// For collections and exact size iterators, it only displays the /// collection/iterator type, the item type, and the length. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct SummaryDebug(pub CollectionOrIter) where CollectionOrIter: IntoIterator + Clone, ::IntoIter: ExactSizeIterator; impl fmt::Debug for SummaryDebug where CollectionOrIter: IntoIterator + Clone, ::IntoIter: ExactSizeIterator, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "{}<{}>, len={}", std::any::type_name::(), std::any::type_name::<::Item>(), self.0.clone().into_iter().len() ) } } impl ops::Deref for SummaryDebug where CollectionOrIter: IntoIterator + Clone, ::IntoIter: ExactSizeIterator, { type Target = CollectionOrIter; fn deref(&self) -> &Self::Target { &self.0 } } impl ops::DerefMut for SummaryDebug where CollectionOrIter: IntoIterator + Clone, ::IntoIter: ExactSizeIterator, { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl From for SummaryDebug where CollectionOrIter: IntoIterator + Clone, ::IntoIter: ExactSizeIterator, { fn from(collection: CollectionOrIter) -> Self { Self(collection) } } impl IntoIterator for SummaryDebug where CollectionOrIter: IntoIterator + Clone, ::IntoIter: ExactSizeIterator, { type Item = ::Item; type IntoIter = ::IntoIter; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } #[cfg(any(test, feature = "proptest-impl"))] impl Arbitrary for SummaryDebug where CollectionOrIter: Arbitrary + IntoIterator + Clone + 'static, ::IntoIter: ExactSizeIterator, { type Parameters = ::Parameters; fn arbitrary_with(args: Self::Parameters) -> Self::Strategy { CollectionOrIter::arbitrary_with(args) .prop_map_into() .boxed() } type Strategy = BoxedStrategy; }