| 18 | }; |
| 19 | |
| 20 | std::optional<ryml::Tree> ParseYAMLFromString(ryml::csubstr yaml, ryml::csubstr file_name, Error* error) |
| 21 | { |
| 22 | RapidYAMLContext context; |
| 23 | context.error = error; |
| 24 | |
| 25 | ryml::Callbacks callbacks; |
| 26 | |
| 27 | #if RYML_VERSION_MAJOR > 0 || RYML_VERSION_MINOR >= 11 |
| 28 | callbacks.set_user_data(static_cast<void*>(&context)); |
| 29 | |
| 30 | callbacks.set_error_basic([](ryml::csubstr msg, const ryml::ErrorDataBasic& errdata, void* user_data) { |
| 31 | std::string description; |
| 32 | auto callback = [&description](ryml::csubstr string) { |
| 33 | description.append(string.str, string.len); |
| 34 | }; |
| 35 | ryml::err_basic_format(std::move(callback), msg, errdata); |
| 36 | |
| 37 | // We might have already returned, so don't try to recover. |
| 38 | pxFailRel(description.c_str()); |
| 39 | std::abort(); |
| 40 | }); |
| 41 | |
| 42 | callbacks.set_error_parse([](ryml::csubstr msg, const ryml::ErrorDataParse& errdata, void* user_data) { |
| 43 | RapidYAMLContext* context = static_cast<RapidYAMLContext*>(user_data); |
| 44 | |
| 45 | std::string description; |
| 46 | auto callback = [&description](ryml::csubstr string) { |
| 47 | description.append(string.str, string.len); |
| 48 | }; |
| 49 | ryml::err_parse_format(std::move(callback), msg, errdata); |
| 50 | |
| 51 | Error::SetString(context->error, std::move(description)); |
| 52 | std::longjmp(context->env, 1); |
| 53 | }); |
| 54 | |
| 55 | callbacks.set_error_visit([](ryml::csubstr msg, const ryml::ErrorDataVisit& errdata, void* user_data) { |
| 56 | std::string description; |
| 57 | auto callback = [&description](ryml::csubstr string) { |
| 58 | description.append(string.str, string.len); |
| 59 | }; |
| 60 | ryml::err_visit_format(std::move(callback), msg, errdata); |
| 61 | |
| 62 | // We've probably already returned, so don't try to recover. |
| 63 | pxFailRel(description.c_str()); |
| 64 | std::abort(); |
| 65 | }); |
| 66 | #else |
| 67 | callbacks.m_user_data = static_cast<void*>(&context); |
| 68 | callbacks.m_error = [](const char* msg, size_t msg_len, ryml::Location location, void* user_data) { |
| 69 | RapidYAMLContext* context = static_cast<RapidYAMLContext*>(user_data); |
| 70 | |
| 71 | Error::SetString(context->error, std::string(msg, msg_len)); |
| 72 | std::longjmp(context->env, 1); |
| 73 | }; |
| 74 | #endif |
| 75 | |
| 76 | ryml::EventHandlerTree event_handler(callbacks); |
| 77 | ryml::Parser parser(&event_handler); |
no test coverage detected