From e02280f9ee72dea8265fbfd41f0176cca51b4311 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 20 Jun 2024 21:21:42 +0200 Subject: [PATCH] includes/type: Added arbitrarily formattable type --- includes/type/fmt.pat | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 includes/type/fmt.pat diff --git a/includes/type/fmt.pat b/includes/type/fmt.pat new file mode 100644 index 0000000..b33ac57 --- /dev/null +++ b/includes/type/fmt.pat @@ -0,0 +1,42 @@ +#pragma once + +import std.io; + +/*! + Type that allows specifying its format value using a format string. + ## Usage + + The following code reads a u32 from the data and formats it as an upper case hexadecimal value with + a minimum of 8 digits which is prefixed by 0x. + + The format string is the same as passed to `std::format()` and follows the libfmt specification. + + ```rust + type::Formatted hex_formatted_integer @ 0x00; + ``` +*/ + +namespace auto type { + + /** + Arbitrarily formatted type + @tparam T Type to format + @tparam FormatString libfmt format string to format the value + */ + struct Formatted { + T value; + } [[sealed, format("type::impl::format_formatted"), transform("type::impl::transform_formatted")]]; + + namespace impl { + + fn format_formatted(ref auto formatted) { + return std::format(std::format("{{0:{}}}", formatted.FormatString), formatted.value); + }; + + fn transform_formatted(ref auto formatted) { + return formatted.value; + }; + + } + +} \ No newline at end of file