From 291fb96f14ff447e456784c345a38105e5ca87a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= Date: Tue, 13 Aug 2024 10:44:06 +0200 Subject: [PATCH] toml11-devel: use new `source_location` API Since toml11-devel-4.0.0 the `source_location` API was changed. https://toruniina.github.io/toml11/docs/reference/source_location/ It also simplifies formatting of `syntax_error`s. --- dnf5/CMakeLists.txt | 5 ++ dnf5/cmdline_aliases.cpp | 117 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 2 deletions(-) diff --git a/dnf5/CMakeLists.txt b/dnf5/CMakeLists.txt index a73706721..6ef8c67c8 100644 --- a/dnf5/CMakeLists.txt +++ b/dnf5/CMakeLists.txt @@ -61,6 +61,11 @@ else() message (STATUS "Using fallback bash completion dir ${BASH_COMPLETION_COMPLETIONSDIR}") endif() +find_package(toml11 REQUIRED) +if (toml11_VERSION VERSION_LESS 4.0.0) + add_definitions(-DTOML11_COMPAT) +endif() + install(FILES bash-completion/dnf5 DESTINATION ${BASH_COMPLETION_COMPLETIONSDIR}) install(FILES "README.plugins" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/dnf5/plugins" RENAME "README") install(DIRECTORY "config/usr/" DESTINATION "${CMAKE_INSTALL_PREFIX}" PATTERN ".gitkeep" EXCLUDE) diff --git a/dnf5/cmdline_aliases.cpp b/dnf5/cmdline_aliases.cpp index bc608c5e8..ca1abcbb4 100644 --- a/dnf5/cmdline_aliases.cpp +++ b/dnf5/cmdline_aliases.cpp @@ -19,6 +19,8 @@ along with libdnf. If not, see . #include "cmdline_aliases.hpp" +#include "utils/string.hpp" + #include #include @@ -59,8 +61,13 @@ bool attach_named_args( *attached_arg_id_path, e.what(), path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger.error("{}", msg); std::cerr << msg << std::endl; return false; @@ -75,8 +82,13 @@ bool attach_named_args( key, alias_id_path, path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT } } if (!attached_arg_id_path) { @@ -97,8 +109,7 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file auto logger = context.get_base().get_logger(); try { - const auto arg_parser_elements = - toml::parse(config_file_path); + const auto arg_parser_elements = toml::parse(config_file_path); try { const auto version = toml::find(arg_parser_elements, "version"); @@ -118,8 +129,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file auto msg = fmt::format( "Bad value type of attribute \"version\" in file \"{}\" on line {}: {}", config_file_path.native(), +#ifdef TOML11_COMPAT loc.line(), loc.line_str()); +#else + loc.first_line_number(), + libdnf5::utils::string::join(loc.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT std::cerr << msg << std::endl; return; } catch (const std::out_of_range & e) { @@ -139,8 +155,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file "Unknown key \"{}\" in file \"{}\" on line {}: {}", element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT continue; } auto element_id_pos = element_id_path.rfind('.'); @@ -154,8 +175,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file auto msg = fmt::format( "Empty or bad element id path in file \"{}\" on line {}: {}", config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -176,8 +202,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file element_parent_id_path, e.what(), config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -202,8 +233,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file type, element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -233,8 +269,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file "Named argument \"{}\" already registered. Requested in file \"{}\" on line {}: {}", element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; break; @@ -250,8 +291,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file "Command \"{}\" already registered. Requested in file \"{}\" on line {}: {}", element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; break; @@ -280,8 +326,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file key, element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT } } @@ -333,8 +384,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file "long in file \"{}\" on line {}: {}", element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -351,8 +407,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file source_id_path, e.what(), config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -368,8 +429,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file group_id, e.what(), config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -383,8 +449,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file key, element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT } } @@ -445,8 +516,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file "long in file \"{}\" on line {}: {}", element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -471,8 +547,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file group_id, e.what(), config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -488,8 +569,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file key, element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT } } @@ -560,8 +646,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file attached_command_id_path, e.what(), config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -579,8 +670,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file group_id, e.what(), config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT logger->error("{}", msg); std::cerr << msg << std::endl; continue; @@ -596,8 +692,13 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file key, element_id_path, config_file_path.native(), +#ifdef TOML11_COMPAT location.line(), location.line_str()); +#else + location.first_line_number(), + libdnf5::utils::string::join(location.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT } } @@ -637,16 +738,28 @@ void load_aliases_from_toml_file(Context & context, const fs::path & config_file auto msg = fmt::format( "Bad value type in file \"{}\" on line {}: {}", config_file_path.native(), +#ifdef TOML11_COMPAT loc.line(), loc.line_str()); +#else + loc.first_line_number(), + libdnf5::utils::string::join(loc.lines(), "\n")); +#endif // #ifdef TOML11_COMPAT std::cerr << msg << std::endl; } } } catch (const toml::syntax_error & e) { logger->error("{}", e.what()); + +#ifdef TOML11_COMPAT auto loc = e.location(); auto msg = fmt::format("Syntax error in file \"{}\" on line {}", config_file_path.native(), loc.line()); std::cerr << msg << std::endl; +#else + for (const auto & err : e.errors()) { + std::cerr << err; + } +#endif // #ifdef TOML11_COMPAT } }