pub trait ToDisplayAndBrief {
#[doc(alias = "toString")]
fn to_display(&self) -> String;
#[doc(alias = "toStringBrief")]
#[doc(alias = "to_brief")]
#[inline(always)]
fn to_display_brief(&self) -> String {
self.to_display()
}
#[doc(alias = "toStringLong")]
#[doc(alias = "to_string_long")]
#[doc(alias = "to_display_verbose")]
#[inline(always)]
fn to_display_long(&self) -> String {
self.to_display()
}
}
pub fn to_display_when_has_content(title: &str, content: impl AsRef<str>) -> String {
let s = content.as_ref();
match s.trim().is_empty() {
true => "".into(),
false => format!("\n{title}{s}"),
}
}
#[macro_export]
macro_rules! __impl_to_display {
( // * 应对各种「定制」要求:缺省`brief`的、多出`long`的
@( // * 📌必须得要标识符:没有识别出标识符的,无法被对应捕获(并正确重复)
$( $to_display_name:ident )? ;
$( $to_display_brief_name:ident )? ;
$( $to_display_long_name:ident )? $(;)?
)
$( { $( $generics:tt )* } )?
$ty:ty as $ty_as:ty
$( where $( $where_cond:tt )* )?
) => {
impl< $( $( $generics )* )? > $crate::util::ToDisplayAndBrief for $ty
$( where $( $where_cond )* )?
{
$(
#[inline(always)]
fn to_display(&self) -> String {
<Self as $ty_as>::$to_display_name(self)
}
)?
$(
#[inline(always)]
fn to_display_brief(&self) -> String {
<Self as $ty_as>::$to_display_brief_name(self)
}
)?
$(
#[inline(always)]
fn to_display_long(&self) -> String {
<Self as $ty_as>::$to_display_long_name(self)
}
)?
}
};
( @ $inner1:tt $($inner2:tt)* ) => {
core::compile_error!(
concat!(
"方法自动实现错误:",
"@", stringify!($inner1),
" 来源:",
stringify!($($inner2)*),
)
);
};
( $($inner:tt)* ) => {
$crate::__impl_to_display! {
@( __to_display;
__to_display_brief;
)
$($inner)*
}
};
}
#[macro_export]
macro_rules! __impl_to_display_and_display {
(
@( $( $inner_prefix:tt )* )
$( $inner:tt )*
) => {
$crate::__impl_to_display! { @( $( $inner_prefix )* ) $( $inner )* }
$crate::impl_display_from_to_display! { $( $inner )* }
};
(
$( $inner:tt )*
) => {
$crate::__impl_to_display! { $( $inner )* }
$crate::impl_display_from_to_display! { $( $inner )* }
};
}
#[macro_export]
macro_rules! impl_display_from_to_display {
( // * 🚩【2024-05-08 23:44:54】↓此处需要加泛型约束:应对带泛型情况
$( { $( $generics:tt )* } )?
$ty:ty $(as $ty_as:ty)? $( where $( $where_cond:tt )* )?
) => {
impl< $( $( $generics )* )? > std::fmt::Display for $ty
$( where $( $where_cond )* )?
{
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&$crate::util::ToDisplayAndBrief::to_display(self))
}
}
};
}