From 34ffa4de405d053ef4b53d58e1437f2e82d152b1 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 13 Jul 2026 12:18:08 +0200 Subject: kstd: make error_code formattable --- libs/kstd/kstd/format.hpp | 1 + libs/kstd/kstd/system_error.hpp | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) (limited to 'libs') diff --git a/libs/kstd/kstd/format.hpp b/libs/kstd/kstd/format.hpp index 5ed798db..5d7f4ae6 100644 --- a/libs/kstd/kstd/format.hpp +++ b/libs/kstd/kstd/format.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/libs/kstd/kstd/system_error.hpp b/libs/kstd/kstd/system_error.hpp index f971703b..08725609 100644 --- a/libs/kstd/kstd/system_error.hpp +++ b/libs/kstd/kstd/system_error.hpp @@ -1,6 +1,8 @@ #ifndef KSTD_SYSTEM_ERROR_HPP #define KSTD_SYSTEM_ERROR_HPP +#include + #include #include #include @@ -472,6 +474,29 @@ namespace kstd return {static_cast(value), generic_category()}; } + template<> + struct formatter + { + constexpr auto parse(format_parse_context & context) -> format_parse_context::iterator + { + auto it = context.begin(); + if (it != context.end() && *it != '}') + { + bits::format::error("Invalid specifier for string_view."); + } + return it; + } + + auto format(error_code const & error, format_context & context) const -> void + { + context.push(error.category().name()); + context.push(':'); + formatter{}.format(error.value(), context); + context.push(':'); + context.push(error.message()); + } + }; + } // namespace kstd #endif -- cgit v1.2.3