31 #include <fmt/format.h>
32 #include <fmt/ranges.h>
37 #include <range/v3/all.hpp>
39 #include "fp/result.hpp"
53 T
from = std::numeric_limits<T>::min();
54 T
to = std::numeric_limits<T>::max();
55 std::optional<T>
step = std::nullopt;
59 if (value < from || value >
to) {
60 return tl::make_unexpected(
OutOfRange(fmt::format(
61 "{}: {} is outside of the range [{}, {}]", name, value,
from,
to)));
65 double const step_value =
static_cast<double>(
step.value());
66 double const ratio =
static_cast<double>(value -
from) / step_value;
67 double const distance = fabs(ratio - round(ratio));
71 fmt::format(
"{}: {} is {} away from the nearest valid step", name,
94 template <
typename Rng,
typename T>
96 std::string
const& name) {
97 if (ranges::contains(valid_values, value)) {
100 return tl::make_unexpected(
101 OutOfRange(fmt::format(
"{} is not in {}", value, valid_values)));