aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/error/error_attribute.h
blob: 24ebf30ee72c03c88e5c84716d930e2cfd6d8a50 (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once

#include "public.h"

#include <library/cpp/yt/misc/guid.h>
#include <library/cpp/yt/misc/tag_invoke_cpo.h>

#include <string>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <class T>
concept CPrimitiveConvertible =
    std::same_as<T, i8> ||
    std::same_as<T, i32> ||
    std::same_as<T, i64> ||
    std::same_as<T, ui8> ||
    std::same_as<T, ui32> ||
    std::same_as<T, ui64> ||
    std::same_as<T, float> ||
    std::same_as<T, double> ||
    std::constructible_from<TStringBuf, const T&> ||
    std::same_as<T, TDuration> ||
    std::same_as<T, TInstant> ||
    std::same_as<T, bool> ||
    std::same_as<T, TGuid>;

////////////////////////////////////////////////////////////////////////////////

namespace NAttributeValueConversionImpl {

struct TTo
    : public TTagInvokeCpoBase<TTo>
{ };

////////////////////////////////////////////////////////////////////////////////

template <class U>
struct TFrom
    : public TTagInvokeCpoBase<TFrom<U>>
{ };

} // namespace NAttributeValueConversionImpl

////////////////////////////////////////////////////////////////////////////////

inline constexpr NAttributeValueConversionImpl::TTo ToErrorAttributeValue = {};
template <class U>
inline constexpr NAttributeValueConversionImpl::TFrom<U> FromErrorAttributeValue = {};

////////////////////////////////////////////////////////////////////////////////

template <class T>
concept CConvertibleToAttributeValue = requires (const T& value) {
    { NYT::ToErrorAttributeValue(value) } -> std::same_as<std::string>;
};

template <class T>
concept CConvertibleFromAttributeValue = requires (TStringBuf value) {
    { NYT::FromErrorAttributeValue<T>(value) } -> std::same_as<T>;
};

////////////////////////////////////////////////////////////////////////////////

struct TErrorAttribute
{
    using TKey = std::string;
    using TValue = std::string;

    template <CConvertibleToAttributeValue T>
    TErrorAttribute(const TKey& key, const T& value)
        : Key(key)
        , Value(NYT::ToErrorAttributeValue(value))
    { }

    TKey Key;
    TValue Value;
};

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT

#define ERROR_ATTRIBUTE_INL_H_
#include "error_attribute-inl.h"
#undef ERROR_ATTRIBUTE_INL_H_