From 601c8daa76f4381af40e56152830575aa76e0c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Mon, 22 Jun 2026 23:28:02 +0200 Subject: [PATCH] Make sure that read does not abort when the default values do not match the validators --- include/rfl/parsing/Parser_default.hpp | 31 ++++++++++++++++---------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index ecab78c7..b9a7f1a7 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -85,7 +85,7 @@ struct Parser { * @param _var The input variable to read from. * @return A Result containing the parsed value or an error. */ - static auto read(const R& _r, const InputVarType& _var) noexcept { + static auto read(const R& _r, const InputVarType& _var) { if constexpr (internal::is_basic_type_v) { return ParserBasicType::read(_r, _var); @@ -644,9 +644,10 @@ struct Parser { (*_definitions)[name] = Parser::to_schema( _definitions, &view); - }else { + } else { (*_definitions)[name] = - Parser::to_schema(_definitions); + Parser::to_schema( + _definitions); } } } @@ -692,16 +693,22 @@ struct Parser { /// so we only use it when the DefaultIfMissing preprocessor is added. static Result read_struct_with_default(const R& _r, const InputVarType& _var) { - auto t = T{}; - auto view = ProcessorsType::template process(to_view(t)); - using ViewType = decltype(view); - const auto err = - Parser::read_view_with_default(_r, _var, - &view); - if (err) [[unlikely]] { - return error(*err); + try { + auto t = T{}; // This might fail, for instance if the default value does + // not satisfy the validator, but in that case we will just + // return the error. + auto view = ProcessorsType::template process(to_view(t)); + using ViewType = decltype(view); + const auto err = + Parser::read_view_with_default( + _r, _var, &view); + if (err) [[unlikely]] { + return error(*err); + } + return t; + } catch (std::exception& e) { + return error(e.what()); } - return t; } };