xref: /openbmc/bmcweb/include/boost_formatters.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include <boost/system/error_code.hpp>
6 #include <boost/url/grammar.hpp>
7 #include <boost/url/url_view_base.hpp>
8 
9 #include <format>
10 
11 // NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp)
12 template <std::derived_from<boost::system::error_code> ErrorCodeType>
13 struct std::formatter<ErrorCodeType>
14 {
15     constexpr auto parse(std::format_parse_context& ctx)
16     {
17         return ctx.begin();
18     }
19 
20     auto format(const ErrorCodeType& ec, auto& ctx) const
21     {
22         return std::format_to(ctx.out(), "{}", ec.what());
23     }
24 };
25 
26 template <std::derived_from<boost::urls::grammar::string_view_base> StringView>
27 struct std::formatter<StringView>
28 {
29     constexpr auto parse(std::format_parse_context& ctx)
30     {
31         return ctx.begin();
32     }
33     auto format(const StringView& msg, auto& ctx) const
34     {
35         return std::format_to(ctx.out(), "{}",
36                               std::string_view(msg.data(), msg.size()));
37     }
38 };
39 
40 template <std::derived_from<boost::urls::url_view_base> UrlBase>
41 struct std::formatter<UrlBase>
42 {
43     constexpr auto parse(std::format_parse_context& ctx)
44     {
45         return ctx.begin();
46     }
47     auto format(const UrlBase& msg, auto& ctx) const
48     {
49         return std::format_to(ctx.out(), "{}", std::string_view(msg.buffer()));
50     }
51 };
52 
53 template <std::derived_from<boost::core::string_view> StringView>
54 struct std::formatter<StringView>
55 {
56     constexpr auto parse(std::format_parse_context& ctx)
57     {
58         return ctx.begin();
59     }
60     auto format(const StringView& msg, auto& ctx) const
61     {
62         return std::format_to(ctx.out(), "{}", std::string_view(msg));
63     }
64 };
65 // NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp)
66