From b96531c9cac03083d779bb0159df8afbc0d93fc0 Mon Sep 17 00:00:00 2001 From: Sakapoi <97370653+Sakapoi@users.noreply.github.com> Date: Tue, 3 Sep 2024 08:11:58 +0300 Subject: [PATCH] format_tools : fixed tests (#1448) --- module/core/format_tools/Readme.md | 98 ++++++++++--------- .../examples/format_tools_trivial.rs | 96 +++++++++--------- 2 files changed, 101 insertions(+), 93 deletions(-) diff --git a/module/core/format_tools/Readme.md b/module/core/format_tools/Readme.md index 19543ef6f4..6fe6e41b5f 100644 --- a/module/core/format_tools/Readme.md +++ b/module/core/format_tools/Readme.md @@ -15,66 +15,70 @@ Using the `to_string_with_fallback` macro to convert values to strings with a pr ```rust fn main() { - // Import necessary traits and the macro from the `format_tools` crate. - use core::fmt; - use format_tools:: + #[ cfg( feature = "enabled" ) ] { - WithDebug, - WithDisplay, - to_string_with_fallback, - }; - // Define a struct that implements both Debug and Display traits. - struct Both; + // Import necessary traits and the macro from the `format_tools` crate. + use core::fmt; + use format_tools:: + { + WithDebug, + WithDisplay, + to_string_with_fallback, + }; - // Implement the Debug trait for the Both struct. - impl fmt::Debug for Both - { - fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result + // 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 { - write!( f, "This is debug" ) + 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 + // Implement the Display trait for the Both struct. + impl fmt::Display for Both { - write!( f, "This is display" ) + 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; + // 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 + // Implement the Debug trait for the OnlyDebug struct. + impl fmt::Debug for OnlyDebug { - write!( f, "This is debug" ) + 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 ); + // 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 ); + + } } ``` diff --git a/module/core/format_tools/examples/format_tools_trivial.rs b/module/core/format_tools/examples/format_tools_trivial.rs index 6683ab3f4a..bd7c7208c9 100644 --- a/module/core/format_tools/examples/format_tools_trivial.rs +++ b/module/core/format_tools/examples/format_tools_trivial.rs @@ -5,64 +5,68 @@ fn main() { - // Import necessary traits and the macro from the `format_tools` crate. - use core::fmt; - use format_tools:: + #[ cfg( feature = "enabled" ) ] { - WithDebug, - WithDisplay, - to_string_with_fallback, - }; - // Define a struct that implements both Debug and Display traits. - struct Both; + // Import necessary traits and the macro from the `format_tools` crate. + use core::fmt; + use format_tools:: + { + WithDebug, + WithDisplay, + to_string_with_fallback, + }; - // Implement the Debug trait for the Both struct. - impl fmt::Debug for Both - { - fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result + // 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 { - write!( f, "This is debug" ) + 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 + // Implement the Display trait for the Both struct. + impl fmt::Display for Both { - write!( f, "This is display" ) + 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; + // 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 + // Implement the Debug trait for the OnlyDebug struct. + impl fmt::Debug for OnlyDebug { - write!( f, "This is debug" ) + 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 ); + // 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 ); + + } } \ No newline at end of file