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