Crate format_tools

Source
Expand description

§Module :: format_tools

experimental rust-status docs.rs Open in Gitpod discord

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.
OptionalCow
Universal wrapper with transparent option of copy on write reference emphasizing a specific aspect of identity of its internal type.
WithDebug
Marker type for using Debug formatting.
WithDebugMultiline
Marker type for using Debug multiline formatting.
WithDisplay
Marker type for using Display formatting.
WithRef
Marker type for returning reference representing instance instead of allocating new string.
WithWell
Marker type for usign Well formatting.

Enums§

LineType
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.
FilterCol
Filter columns of a table to print it only partially.
FilterRow
Filter columns of a table to print it only partially.
IntoAsTable
Trait for converting data references into AsTable references.
IteratorTrait
A trait for iterators that implement _IteratorTrait and Clone.
TableFormatter
Trait for defining table formatting logic.
TableHeader
Trait returning headers of a table if any.
TableOutputFormat
Trait for converting table extracts into string representations.
TableRows
Trait for iterating over rows in a table.
TableWithFields
Marker trait to tag structures for which table trait deducing should be done from trait Fields, which is reflection.
ToStringWith
Trait to convert a type to a string using a specified formatting method.
ToStringWithFallback
Trait to convert a type to a string with a fallback formatting.
_IteratorTrait
A trait for iterators that are also ExactSizeIterator.