Expand description
§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()
{
// 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 );
}
§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
Re-exports§
pub use super::super::to_string;
pub use super::super::to_string_with_fallback;
Modules§
- dependency
- Namespace with dependencies.
- exposed
- Exposed namespace of the module.
- format
- Collection of mechanisms for formatting and serialization into string.
- orphan
- Orphan namespace of the module.
- own
- Own namespace of the module.
- prelude
- Prelude to use essentials:
use my_module::prelude::*
. - ref_
or_ debug - Converting representations to a reference on a string slice, but if not possible, to a debug string.
- ref_
or_ display_ or_ debug - Converting representations to a reference on a string slice, but if not possible, to a display string, and if that is also not possible, then to a debug string.
Macros§
- _field
- Macro to create a field with optional fallbacks.
- _field_
with_ key - Macro to create a field with a key and formatted value.
- ref_
or_ debug_ field - Macro to create a field using reference or debug formatting.
- ref_
or_ debug_ field_ with_ key - Macro to create a field with key using reference or debug formatting.
- ref_
or_ display_ or_ debug_ field - Macro to create a field using reference, display, or debug formatting.
- ref_
or_ display_ or_ debug_ field_ with_ key - Macro to create a field with key using reference, display, or debug formatting.
- to_
string_ with_ fallback - Macro to convert a value to a string using a specified formatting method with a fallback.
Structs§
- AsTable
- Transparent wrapper for table-like structures.
- Context
- Struct for formatting tables.
- MaybeAs
- Universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type.
- Styles
- Struct to hold options to print data as table.
- With
Debug - Marker type for using Debug formatting.
- With
Display - Marker type for using Display formatting.
- WithRef
- Marker type for returning reference representing instance instead of allocating new string.
- With
Well - Marker type for usign Well formatting.
Traits§
- Cells
- A trait for iterating over all cells of a row.
- Fields
- A trait for iterating over all fields convertible into a specified type within an entity.
- Iterator
Trait - A trait for iterators that implement
_IteratorTrait
andClone
. - Table
Formatter - A trait for formatting tables.
- Table
Header - Trait returning headers of a table if any.
- Table
Rows - A trait for iterating over all rows of a table.
- Table
Size - A trait for iterating over all rows of a table.
- Table
ToString - A trait for converting tables to a string representation.
- ToString
With - Trait to convert a type to a string using a specified formatting method.
- ToString
With Fallback - Trait to convert a type to a string with a fallback formatting.