|
|
|
@ -3,9 +3,7 @@
|
|
|
|
|
// system includes
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <expected>
|
|
|
|
|
|
|
|
|
|
// 3rdparty lib includes
|
|
|
|
|
#include <fmt/core.h>
|
|
|
|
|
#include <format>
|
|
|
|
|
|
|
|
|
|
// local includes
|
|
|
|
|
#include "configwrapper.h"
|
|
|
|
@ -16,7 +14,7 @@ template<int MAX_LENGTH>
|
|
|
|
|
ConfigConstraintReturnType StringMaxSize(const std::string &str)
|
|
|
|
|
{
|
|
|
|
|
if (str.size() > MAX_LENGTH)
|
|
|
|
|
return std::unexpected(fmt::format("String length {} exceeds maximum {}", str.size(), MAX_LENGTH));
|
|
|
|
|
return std::unexpected(std::format("String length {} exceeds maximum {}", str.size(), MAX_LENGTH));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -24,7 +22,7 @@ template<int MIN_LENGTH, int MAX_LENGTH>
|
|
|
|
|
ConfigConstraintReturnType StringMinMaxSize(const std::string &str)
|
|
|
|
|
{
|
|
|
|
|
if (str.size() < MIN_LENGTH || str.size() > MAX_LENGTH)
|
|
|
|
|
return std::unexpected(fmt::format("String length {} exceeds range {} to {}", str.size(), MIN_LENGTH, MAX_LENGTH));
|
|
|
|
|
return std::unexpected(std::format("String length {} exceeds range {} to {}", str.size(), MIN_LENGTH, MAX_LENGTH));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -49,7 +47,7 @@ ConfigConstraintReturnType StringOr(const std::string &str)
|
|
|
|
|
const auto result1 = callback1(str);
|
|
|
|
|
if (result1)
|
|
|
|
|
return {};
|
|
|
|
|
return std::unexpected(fmt::format("None of the following 2 constraints succeded: {} | {}", result0.error(), result1.error()));
|
|
|
|
|
return std::unexpected(std::format("None of the following 2 constraints succeded: {} | {}", result0.error(), result1.error()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<ConfigWrapper<std::string>::ConstraintCallback callback0, ConfigWrapper<std::string>::ConstraintCallback callback1, ConfigWrapper<std::string>::ConstraintCallback callback2>
|
|
|
|
@ -64,7 +62,7 @@ ConfigConstraintReturnType StringOr(const std::string &str)
|
|
|
|
|
const auto result2 = callback2(str);
|
|
|
|
|
if (result2)
|
|
|
|
|
return {};
|
|
|
|
|
return std::unexpected(fmt::format("None of the following 3 constraints succeded: {} | {} | {}", result0.error(), result1.error(), result2.error()));
|
|
|
|
|
return std::unexpected(std::format("None of the following 3 constraints succeded: {} | {} | {}", result0.error(), result1.error(), result2.error()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<ConfigWrapper<std::string>::ConstraintCallback callback0, ConfigWrapper<std::string>::ConstraintCallback callback1>
|
|
|
|
@ -101,7 +99,7 @@ template<typename T, T MIN_VALUE>
|
|
|
|
|
ConfigConstraintReturnType MinValue(typename ConfigWrapper<T>::value_t val)
|
|
|
|
|
{
|
|
|
|
|
if (val < MIN_VALUE)
|
|
|
|
|
return std::unexpected(fmt::format("Value {} exceeds minimum {}", val, MIN_VALUE));
|
|
|
|
|
return std::unexpected(std::format("Value {} exceeds minimum {}", val, MIN_VALUE));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -109,7 +107,7 @@ template<typename T, T MAX_VALUE>
|
|
|
|
|
ConfigConstraintReturnType MaxValue(typename ConfigWrapper<T>::value_t val)
|
|
|
|
|
{
|
|
|
|
|
if (val > MAX_VALUE)
|
|
|
|
|
return std::unexpected(fmt::format("Value {} exceeds maximum {}", val, MAX_VALUE));
|
|
|
|
|
return std::unexpected(std::format("Value {} exceeds maximum {}", val, MAX_VALUE));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -117,7 +115,7 @@ template<typename T, T MIN_VALUE, T MAX_VALUE>
|
|
|
|
|
ConfigConstraintReturnType MinMaxValue(typename ConfigWrapper<T>::value_t val)
|
|
|
|
|
{
|
|
|
|
|
if (val < MIN_VALUE || val > MAX_VALUE)
|
|
|
|
|
return std::unexpected(fmt::format("Value {} exceeds range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
|
|
|
|
return std::unexpected(std::format("Value {} exceeds range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -125,7 +123,7 @@ template<typename T>
|
|
|
|
|
ConfigConstraintReturnType MinMaxValue(typename ConfigWrapper<T>::value_t val, T MIN_VALUE, T MAX_VALUE)
|
|
|
|
|
{
|
|
|
|
|
if (val < MIN_VALUE || val > MAX_VALUE)
|
|
|
|
|
return std::unexpected(fmt::format("Value {} exceeds range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
|
|
|
|
return std::unexpected(std::format("Value {} exceeds range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -133,7 +131,7 @@ template<typename T, T MIN_VALUE, T MAX_VALUE>
|
|
|
|
|
ConfigConstraintReturnType MinMaxOrZeroValue(typename ConfigWrapper<T>::value_t val)
|
|
|
|
|
{
|
|
|
|
|
if (val != 0 && (val < MIN_VALUE || val > MAX_VALUE))
|
|
|
|
|
return std::unexpected(fmt::format("Value {} exceeds constraint 0 or range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
|
|
|
|
return std::unexpected(std::format("Value {} exceeds constraint 0 or range {} to {}", val, MIN_VALUE, MAX_VALUE));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
} // namespace espconfig
|
|
|
|
|