summaryrefslogtreecommitdiff
path: root/include/boost_formatters.hpp
blob: 0503d59d798f694009034d06d470e0959724ccdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once

#include <boost/system/error_code.hpp>
#include <boost/url/grammar.hpp>
#include <boost/url/url_view_base.hpp>

#include <format>

// NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp)
template <std::derived_from<boost::system::error_code> ErrorCodeType>
struct std::formatter<ErrorCodeType>
{
    constexpr auto parse(std::format_parse_context& ctx)
    {
        return ctx.begin();
    }

    auto format(const ErrorCodeType& ec, auto& ctx) const
    {
        return std::format_to(ctx.out(), "{}", ec.what());
    }
};

template <std::derived_from<boost::urls::grammar::string_view_base> StringView>
struct std::formatter<StringView>
{
    constexpr auto parse(std::format_parse_context& ctx)
    {
        return ctx.begin();
    }
    auto format(const StringView& msg, auto& ctx) const
    {
        return std::format_to(ctx.out(), "{}",
                              std::string_view(msg.data(), msg.size()));
    }
};

template <std::derived_from<boost::urls::url_view_base> UrlBase>
struct std::formatter<UrlBase>
{
    constexpr auto parse(std::format_parse_context& ctx)
    {
        return ctx.begin();
    }
    auto format(const UrlBase& msg, auto& ctx) const
    {
        return std::format_to(ctx.out(), "{}", std::string_view(msg.buffer()));
    }
};

template <std::derived_from<boost::core::string_view> StringView>
struct std::formatter<StringView>
{
    constexpr auto parse(std::format_parse_context& ctx)
    {
        return ctx.begin();
    }
    auto format(const StringView& msg, auto& ctx) const
    {
        return std::format_to(ctx.out(), "{}", std::string_view(msg));
    }
};
// NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp)