userver: userver/utils/small_string_serialization.hpp Source File
Loading...
Searching...
No Matches
small_string_serialization.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/small_string_serialization.hpp
4/// @brief Parse, serialize, and fmt formatting for SmallString.
5/// @ingroup userver_universal
6
7#include <string>
8
9#include <fmt/core.h>
10
11#include <userver/formats/parse/to.hpp>
12#include <userver/formats/serialize/to.hpp>
13#include <userver/utils/small_string.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace utils {
18
19template <typename Value, std::size_t N>
20Value Serialize(const SmallString<N>& value, formats::serialize::To<Value>) {
21 return typename Value::Builder{std::string_view{value}}.ExtractValue();
22}
23
24template <typename Value, std::size_t N>
25SmallString<N> Parse(const Value& value, formats::parse::To<SmallString<N>>) {
26 return SmallString<N>{value.template As<std::string>()};
27}
28
29} // namespace utils
30
31USERVER_NAMESPACE_END
32
33template <std::size_t N>
34struct fmt::formatter<USERVER_NAMESPACE::utils::SmallString<N>> : public fmt::formatter<std::string_view> {
35 template <typename FormatContext>
36 auto format(const USERVER_NAMESPACE::utils::SmallString<N>& value, FormatContext& ctx) const
37 -> decltype(ctx.out()) {
38 return formatter<std::string_view>::format(std::string_view{value}, ctx);
39 }
40};