blob: b01c7682a26cb8183851ba73a42ebafd6fcd5c53 (
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
#pragma once
#include <cassert>
#include <optional>
#include <string>
#include <utility>
#include <variant>
namespace Progmasoft::Catch3::Runner
{
/// A failure with a message written for the person who has to act on it.
class Error final
{
public:
explicit Error(std::string message)
: message_{std::move(message)}
{
}
[[nodiscard]] const std::string &
Message() const noexcept
{
return message_;
}
/// The same failure, with what was being done in front of it.
[[nodiscard]] Error
Context(const std::string &context) const
{
return Error{context + ": " + message_};
}
private:
std::string message_;
};
/// A value, or the error that prevented it.
template<typename Value>
class [[nodiscard]] Expected final
{
public:
Expected(Value value)
: state_{std::in_place_index<0>, std::move(value)}
{
}
Expected(Error error)
: state_{std::in_place_index<1>, std::move(error)}
{
}
[[nodiscard]] explicit
operator bool() const noexcept
{
return state_.index() == 0;
}
[[nodiscard]] Value &
operator*() &
{
assert(*this);
return std::get<0>(state_);
}
[[nodiscard]] const Value &
operator*() const &
{
assert(*this);
return std::get<0>(state_);
}
[[nodiscard]] Value &&
operator*() &&
{
assert(*this);
return std::get<0>(std::move(state_));
}
[[nodiscard]] Value *
operator->()
{
assert(*this);
return &std::get<0>(state_);
}
[[nodiscard]] const Value *
operator->() const
{
assert(*this);
return &std::get<0>(state_);
}
[[nodiscard]] const Error &
GetError() const
{
assert(!*this);
return std::get<1>(state_);
}
private:
std::variant<Value, Error> state_;
};
/// Success, or the error that happened instead.
class [[nodiscard]] Status final
{
public:
Status() = default;
Status(Error error)
: error_{std::move(error)}
{
}
[[nodiscard]] static Status
Ok()
{
return Status{};
}
[[nodiscard]] explicit
operator bool() const noexcept
{
return !error_.has_value();
}
[[nodiscard]] const Error &
GetError() const
{
assert(error_.has_value());
return *error_;
}
private:
std::optional<Error> error_;
};
} // namespace Progmasoft::Catch3::Runner
|