Crates.io | format_tools |
lib.rs | format_tools |
version | |
source | src |
created_at | 2024-06-08 19:12:14.071431+00 |
updated_at | 2025-04-22 21:27:22.218189+00 |
description | Collection of mechanisms for formatting and serialization into string. |
homepage | https://github.com/Wandalen/wTools/tree/master/module/core/format_tools |
repository | https://github.com/Wandalen/wTools/tree/master/module/core/format_tools |
max_upload_size | |
id | 1265916 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Collection of mechanisms for formatting and serialization into string.
Using the to_string_with_fallback
macro to convert values to strings with a primary and fallback formatting method.
fn main()
{
#[ cfg( feature = "enabled" ) ]
{
// Import necessary traits and the macro from the `format_tools` crate.
use core::fmt;
use format_tools::
{
WithDebug,
WithDisplay,
to_string_with_fallback,
};
// Define a struct that implements both Debug and Display traits.
struct Both;
// Implement the Debug trait for the Both struct.
impl fmt::Debug for Both
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is debug" )
}
}
// Implement the Display trait for the Both struct.
impl fmt::Display for Both
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is display" )
}
}
// Define a struct that implements only the Debug trait.
struct OnlyDebug;
// Implement the Debug trait for the OnlyDebug struct.
impl fmt::Debug for OnlyDebug
{
fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
write!( f, "This is debug" )
}
}
// Example usage: Using Both which implements both Debug and Display.
let src = Both;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is display".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );
// Example usage: Using OnlyDebug which implements only Debug.
let src = OnlyDebug;
// Convert the struct to a string using `to_string_with_fallback` macro.
// The primary formatting method WithDisplay is not available, so the fallback WithDebug is used.
let got = to_string_with_fallback!( WithDisplay, WithDebug, &src );
let exp = "This is debug".to_string();
// Assert that the result matches the expected value.
assert_eq!( got, exp );
}
}
cargo add format_tools
git clone https://github.com/Wandalen/wTools
cd wTools
cd examples/foramt_tools_trivial
cargo run