fix tl expected issues
This commit is contained in:
parent
686decfc50
commit
4caf57c82e
@ -1,19 +1,10 @@
|
||||
#include <ranczo-io/utils/json_helpers.hpp>
|
||||
|
||||
#include <boost/json/value.hpp>
|
||||
#include <boost/json.hpp>
|
||||
|
||||
std::optional<double> ranczo::json::as_number(const boost::json::value &v) {
|
||||
if(v.is_double())
|
||||
return v.as_double();
|
||||
if(v.is_int64())
|
||||
return static_cast< double >(v.as_int64());
|
||||
if(v.is_uint64())
|
||||
return static_cast< double >(v.as_uint64());
|
||||
if(v.is_string()) {
|
||||
const auto & s = v.as_string();
|
||||
double out{};
|
||||
if(auto res = std::from_chars(s.data(), s.data() + s.size(), out); res.ec == std::errc{})
|
||||
return out;
|
||||
}
|
||||
if (auto p = boost::json::try_value_to<double>(v))
|
||||
return *p;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
@ -83,7 +83,7 @@ class SimpleTimer {
|
||||
ASYNC_CHECK(on_tick_());
|
||||
} catch(const std::exception & e) {
|
||||
spdlog::error("on_tick threw exception: {}", e.what());
|
||||
co_return std::unexpected{ boost::system::error_code{} };
|
||||
co_return unexpected{ boost::system::error_code{} };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -59,17 +59,17 @@ awaitable_expected< void > MqttThermometer::listen() noexcept {
|
||||
expected< Thermometer::ThermometerData > MqttThermometer::to_thermometer_data(const boost::json::value & v) {
|
||||
const auto * obj = v.if_object();
|
||||
if(!obj) {
|
||||
return std::unexpected{make_error_code(std::errc::invalid_argument)};
|
||||
return unexpected{make_error_code(std::errc::invalid_argument)};
|
||||
}
|
||||
|
||||
// ---- value (required) ----
|
||||
const auto * pv = obj->if_contains("value");
|
||||
if(!pv) {
|
||||
return std::unexpected{make_error_code(std::errc::invalid_argument)};
|
||||
return unexpected{make_error_code(std::errc::invalid_argument)};
|
||||
}
|
||||
auto val = ranczo::json::as_number(*pv);
|
||||
if(!val) {
|
||||
return std::unexpected{make_error_code(std::errc::invalid_argument)};
|
||||
return unexpected{make_error_code(std::errc::invalid_argument)};
|
||||
}
|
||||
|
||||
// ---- timestamp (optional; default: now()) ----
|
||||
|
||||
Loading…
Reference in New Issue
Block a user