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()
{
#[ 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 );
}
}
§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::filter;
pub use super::super::md_math;
pub use super::super::output_format;
pub use super::super::print;
pub use super::super::string;
pub use super::super::table;
pub use super::super::output_format;
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.
- ref_
or_ display_ or_ debug_ multiline - 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.
- ref_
or_ display_ or_ debug_ multiline_ field - Macro to create a field using reference, display, or debug formatting.
- ref_
or_ display_ or_ debug_ multiline_ 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 interpreting data as a table.
- Optional
Cow - Universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type.
- With
Debug - Marker type for using Debug formatting.
- With
Debug Multiline - Marker type for using Debug multiline 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.
Enums§
- Line
Type - Represents a line type in a table, either a header or a regular row.
Traits§
- Cells
- A trait for iterating over all cells of a row.
- Fields
- A trait for iterating over fields convertible to a specified type within an entity.
- Filter
Col - Filter columns of a table to print it only partially.
- Filter
Row - Filter columns of a table to print it only partially.
- Into
AsTable - Trait for converting data references into
AsTable
references. - Iterator
Trait - A trait for iterators that implement
_IteratorTrait
andClone
. - Table
Formatter - Trait for defining table formatting logic.
- Table
Header - Trait returning headers of a table if any.
- Table
Output Format - Trait for converting table extracts into string representations.
- Table
Rows - Trait for iterating over rows in a table.
- Table
With Fields - Marker trait to tag structures for which table trait deducing should be done from trait Fields, which is reflection.
- 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.
- _Iterator
Trait - A trait for iterators that are also
ExactSizeIterator
.