22#include "util/Assert.hpp"
23#include "util/OverloadSet.hpp"
24#include "util/config/ConfigConstraints.hpp"
25#include "util/config/Error.hpp"
26#include "util/config/Types.hpp"
28#include <fmt/format.h>
39namespace util::config {
66 defaultValue(Value value, std::optional<std::string_view> description = std::nullopt)
68 auto const err = checkTypeConsistency(type_, value);
70 ASSERT(
false,
"{}", err->error);
71 description_ = description;
83 [[nodiscard]] std::optional<Error>
84 setValue(Value value, std::optional<std::string_view> key = std::nullopt)
86 auto err = checkTypeConsistency(type_, value);
87 if (err.has_value()) {
88 err->error = fmt::format(
"{} {}", key.value_or(
"Unknown_key"), err->error);
92 if (cons_.has_value()) {
93 auto constraintCheck = cons_->get().checkConstraint(value);
94 if (constraintCheck.has_value()) {
95 constraintCheck->error = fmt::format(
"{} {}", key.value_or(
"Unknown_key"), constraintCheck->error);
96 return constraintCheck;
117 cons_ = std::reference_wrapper<Constraint const>(cons);
118 ASSERT(cons_.has_value(),
"Constraint must be defined");
120 if (value_.has_value()) {
121 auto const& temp = cons_.value().get();
122 auto const& result = temp.checkConstraint(value_.value());
123 if (result.has_value()) {
128 [&
type](
bool tmp) {
type = fmt::format(
"bool {}", tmp); },
129 [&
type](std::string
const& tmp) {
type = fmt::format(
"string {}", tmp); },
130 [&
type](
double tmp) {
type = fmt::format(
"double {}", tmp); },
131 [&
type](int64_t tmp) {
type = fmt::format(
"int {}", tmp); },
135 ASSERT(
false,
"Value {} ConfigValue does not satisfy the set Constraint",
type);
146 [[nodiscard]]
constexpr std::optional<std::reference_wrapper<Constraint const>>
157 [[nodiscard]]
constexpr ConfigType
192 return value_.has_value();
200 [[nodiscard]] Value
const&
203 ASSERT(value_.has_value(),
"getValue() is called when there is no value set");
204 return value_.value();
217 stream <<
"- **Required**: " << (val.
isOptional() ?
"False" :
"True") <<
"\n";
218 stream <<
"- **Type**: " << val.
type() <<
"\n";
219 if (val.description_.has_value()) {
220 stream <<
"- **Default value**: " << *val.description_ <<
"\n";
222 stream <<
"- **Default value**: `" << *val.value_ <<
"`\n";
224 stream <<
"- **Default value**: None\n";
226 stream <<
"- **Constraints**: ";
231 stream <<
"None" <<
"\n";
243 static std::optional<Error>
244 checkTypeConsistency(ConfigType
type, Value value)
246 if (
type == ConfigType::String && !std::holds_alternative<std::string>(value)) {
247 return Error{
"value does not match type string"};
249 if (
type == ConfigType::Boolean && !std::holds_alternative<bool>(value)) {
250 return Error{
"value does not match type boolean"};
252 if (
type == ConfigType::Double && (!std::holds_alternative<double>(value))) {
253 if (std::holds_alternative<int64_t>(value))
255 return Error{
"value does not match type double"};
257 if (
type == ConfigType::Integer && !std::holds_alternative<int64_t>(value)) {
258 return Error{
"value does not match type integer"};
264 bool optional_{
false};
265 std::optional<Value> value_;
266 std::optional<std::reference_wrapper<Constraint const>> cons_;
267 std::optional<std::string_view> description_;
Represents the config values for Json/Yaml config.
Definition ConfigValue.hpp:47
ConfigValue & defaultValue(Value value, std::optional< std::string_view > description=std::nullopt)
Sets the default value for the config.
Definition ConfigValue.hpp:66
friend std::ostream & operator<<(std::ostream &stream, ConfigValue val)
Prints all the info of this config value to the output stream.
Definition ConfigValue.hpp:215
constexpr ConfigType type() const
Gets the config type.
Definition ConfigValue.hpp:158
constexpr std::optional< std::reference_wrapper< Constraint const > > getConstraint() const
Retrieves the constraint associated with this ConfigValue, if any.
Definition ConfigValue.hpp:147
constexpr ConfigValue & optional()
Sets the config value as optional, meaning the user doesn't have to provide the value in their config...
Definition ConfigValue.hpp:169
constexpr ConfigValue(ConfigType type)
Constructor initializing with the config type.
Definition ConfigValue.hpp:54
std::optional< Error > setValue(Value value, std::optional< std::string_view > key=std::nullopt)
Sets the value current ConfigValue given by the User's defined value.
Definition ConfigValue.hpp:84
bool constexpr isOptional() const
Checks if configValue is optional.
Definition ConfigValue.hpp:180
Value const & getValue() const
Get the value of config.
Definition ConfigValue.hpp:201
bool constexpr hasValue() const
Check if value is optional.
Definition ConfigValue.hpp:190
constexpr ConfigValue & withConstraint(Constraint const &cons)
Assigns a constraint to the ConfigValue.
Definition ConfigValue.hpp:115
An interface to enforce constraints on certain values within ClioConfigDefinition.
Definition ConfigConstraints.hpp:90
Overload set for lambdas.
Definition OverloadSet.hpp:30
Displays the different errors when parsing user config.
Definition Error.hpp:31