use core::fmt;
use super::PositionedChar;
pub trait DisplayHuman {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
#[cfg_attr(not(tarpaulin), inline(always))]
fn display(&self) -> HumanDisplay<'_, Self> {
HumanDisplay(self)
}
}
impl<T: DisplayHuman + ?Sized> DisplayHuman for &T {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(*self).fmt(f)
}
}
impl DisplayHuman for () {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, _: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Ok(())
}
}
impl DisplayHuman for u8 {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if self.is_ascii() {
write!(f, "{}", *self as char)
} else {
fmt::Display::fmt(self, f)
}
}
}
macro_rules! impl_display_human_for_primitive {
($($ty:ty),+) => {
$(
impl DisplayHuman for $ty {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt::Display::fmt(self, f)
}
}
)*
};
}
impl_display_human_for_primitive!(
u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64, char, str
);
impl<T: DisplayHuman> DisplayHuman for PositionedChar<T> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.char_ref().fmt(f)
}
}
impl DisplayHuman for [u8] {
#[cfg(not(feature = "bstr"))]
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match core::str::from_utf8(self) {
Ok(s) => s.fmt(f),
Err(_) => core::fmt::Debug::fmt(self, f),
}
}
#[cfg(feature = "bstr")]
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
bstr::BStr::new(self).fmt(f)
}
}
impl DisplayHuman for [char] {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for c in self {
c.fmt(f)?;
}
Ok(())
}
}
impl<const N: usize> DisplayHuman for [u8; N] {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.as_slice().fmt(f)
}
}
impl<const N: usize> DisplayHuman for [char; N] {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.as_slice().fmt(f)
}
}
#[cfg(feature = "bytes")]
impl DisplayHuman for bytes::Bytes {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.as_ref().fmt(f)
}
}
#[cfg(feature = "bstr")]
impl DisplayHuman for bstr::BStr {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[cfg(feature = "hipstr")]
const _: () = {
use hipstr::{HipByt, HipStr};
impl DisplayHuman for HipStr<'_> {
#[cfg_attr(test, inline)]
#[cfg_attr(not(test), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl DisplayHuman for HipByt<'_> {
#[cfg_attr(test, inline)]
#[cfg_attr(not(test), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
DisplayHuman::fmt(self.as_ref(), f)
}
}
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct HumanDisplay<'a, T: ?Sized>(&'a T);
impl<T: DisplayHuman + ?Sized> core::fmt::Display for HumanDisplay<'_, T> {
#[cfg_attr(not(tarpaulin), inline(always))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}