Skip to content

Commit

Permalink
Rename some functions and mark noexcept
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdewar committed Aug 11, 2023
1 parent 7931f38 commit 9bcb97f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
24 changes: 14 additions & 10 deletions src/HealthGPS.Console/configuration_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,37 @@ nlohmann::json get(const json &j, const std::string &key) {

void rebase_valid_path(std::filesystem::path &path, const std::filesystem::path &base_dir) {
if (path.is_relative()) {
path = std::filesystem::weakly_canonical(base_dir / path);
try {
path = std::filesystem::weakly_canonical(base_dir / path);
} catch (const std::filesystem::filesystem_error &e) {
throw ConfigurationError{fmt::format("OS error while reading {}", path)};
}
}

if (!std::filesystem::exists(path)) {
throw ConfigurationError{fmt::format("Path does not exist: {}", path.string())};
}
}

bool get_valid_path_to(const json &j, const std::string &key, const std::filesystem::path &base_dir,
std::filesystem::path &out) {
if (!get_to(j, key, out)) {
bool rebase_valid_path_to(const json &j, const std::string &key, std::filesystem::path &path,
const std::filesystem::path &base_dir) noexcept {
if (!get_to(j, key, path)) {
return false;
}

try {
rebase_valid_path(out, base_dir);
rebase_valid_path(path, base_dir);
} catch (const ConfigurationError &) {
fmt::print(fg(fmt::color::red), "Could not find file {}", out.string());
fmt::print(fg(fmt::color::red), "Could not find file {}", path.string());
return false;
}

return true;
}

void get_valid_path_to(const json &j, const std::string &key, const std::filesystem::path &base_dir,
std::filesystem::path &out, bool &success) {
if (!get_valid_path_to(j, key, base_dir, out)) {
void rebase_valid_path_to(const json &j, const std::string &key, std::filesystem::path &path,
const std::filesystem::path &base_dir, bool &success) noexcept {
if (!rebase_valid_path_to(j, key, path, base_dir)) {
success = false;
}
}
Expand All @@ -56,7 +60,7 @@ poco::FileInfo get_file_info(const json &j, const std::filesystem::path &base_di

bool success = true;
poco::FileInfo info;
get_valid_path_to(dataset, "name", base_dir, info.name, success);
rebase_valid_path_to(dataset, "name", info.name, base_dir, success);
get_to(dataset, "format", info.format, success);
get_to(dataset, "delimiter", info.delimiter, success);
get_to(dataset, "columns", info.columns, success);
Expand Down
19 changes: 10 additions & 9 deletions src/HealthGPS.Console/configuration_parsing_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ nlohmann::json get(const nlohmann::json &j, const std::string &key);
/// @param key Key to value
/// @param out Output object
/// @return True if value was retrieved successfully, false otherwise
template <class T> bool get_to(const nlohmann::json &j, const std::string &key, T &out) {
template <class T> bool get_to(const nlohmann::json &j, const std::string &key, T &out) noexcept {
try {
out = j.at(key).get<T>();
return true;
Expand All @@ -43,7 +43,7 @@ template <class T> bool get_to(const nlohmann::json &j, const std::string &key,
/// @param success Success flag, set to false in case of failure
/// @return True if value was retrieved successfully, false otherwise
template <class T>
bool get_to(const nlohmann::json &j, const std::string &key, T &out, bool &success) {
bool get_to(const nlohmann::json &j, const std::string &key, T &out, bool &success) noexcept {
const bool ret = get_to(j, key, out);
if (!ret) {
success = false;
Expand All @@ -61,20 +61,21 @@ void rebase_valid_path(std::filesystem::path &path, const std::filesystem::path
/// @param j JSON object
/// @param key Key to value
/// @param base_dir Base directory for relative path
/// @param out Output variable
/// @param path Output variable
/// @return True if value was retrieved successfully and is valid path, false otherwise
bool get_valid_path_to(const nlohmann::json &j, const std::string &key,
const std::filesystem::path &base_dir, std::filesystem::path &out);
bool rebase_valid_path_to(const nlohmann::json &j, const std::string &key,
std::filesystem::path &path,
const std::filesystem::path &base_dir) noexcept;

/// @brief Get a valid path from a JSON object
/// @param j JSON object
/// @param key Key to value
/// @param base_dir Base directory for relative path
/// @param out Output variable
/// @param path Output variable
/// @param success Success flag, set to false in case of failure
void get_valid_path_to(const nlohmann::json &j, const std::string &key,
const std::filesystem::path &base_dir, std::filesystem::path &out,
bool &success);
void rebase_valid_path_to(const nlohmann::json &j, const std::string &key,
std::filesystem::path &out, const std::filesystem::path &base_dir,
bool &success) noexcept;

/// @brief Load FileInfo from JSON
/// @param j Input JSON
Expand Down

0 comments on commit 9bcb97f

Please sign in to comment.