Module :: format_tools

Collection of mechanisms for formatting and serialization into string.
Basic use-case
Using the to_string_with_fallback
macro to convert values to strings with a primary and fallback formatting method.
fn main()
{
use core::fmt;
use format_tools::
{
WithDebug,
WithDisplay,
to_string_with_fallback,
};
struct Both;
impl fmt::Debug for Both
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is debug" )
}
}
impl fmt::Display for Both
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is display" )
}
}
struct OnlyDebug;
impl fmt::Debug for OnlyDebug
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is debug" )
}
}
let src = Both;
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is display".to_string();
assert_eq!( got, exp );
let src = OnlyDebug;
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is debug".to_string();
assert_eq!( got, exp );
}
To add to your project
cargo add format_tools
Try out from the repository
git clone https://github.com/Wandalen/wTools
cd wTools
cd examples/foramt_tools_trivial
cargo run