Skip to content

Commit

Permalink
toml11-devel: use new source_location API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kontura committed Aug 13, 2024
1 parent cf06886 commit eced57d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dnf5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
114 changes: 114 additions & 0 deletions dnf5/cmdline_aliases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "cmdline_aliases.hpp"

#include "utils/string.hpp"

#include <libdnf5/common/preserve_order_map.hpp>
#include <toml.hpp>

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -118,8 +130,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) {
Expand All @@ -139,8 +156,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('.');
Expand All @@ -154,8 +176,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;
Expand All @@ -176,8 +203,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;
Expand All @@ -202,8 +234,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;
Expand Down Expand Up @@ -233,8 +270,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;
Expand All @@ -250,8 +292,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;
Expand Down Expand Up @@ -280,8 +327,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
}
}

Expand Down Expand Up @@ -333,8 +385,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;
Expand All @@ -351,8 +408,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;
Expand All @@ -368,8 +430,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;
Expand All @@ -383,8 +450,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
}
}

Expand Down Expand Up @@ -445,8 +517,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;
Expand All @@ -471,8 +548,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;
Expand All @@ -488,8 +570,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
}
}

Expand Down Expand Up @@ -560,8 +647,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;
Expand All @@ -579,8 +671,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;
Expand All @@ -596,8 +693,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
}
}

Expand Down Expand Up @@ -637,16 +739,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
}
}

Expand Down

0 comments on commit eced57d

Please sign in to comment.