Skip to content

Commit

Permalink
fix: write default settings file, if none is present
Browse files Browse the repository at this point in the history
  • Loading branch information
Totto16 committed Oct 31, 2024
1 parent dd58ba8 commit 88387f8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
38 changes: 33 additions & 5 deletions src/libs/core/helper/parse_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// better json error messages, see https://json.nlohmann.me/api/macros/json_diagnostics/
#define JSON_DIAGNOSTICS 1 // NOLINT(cppcoreguidelines-macro-usage)
#endif

#include <nlohmann/json.hpp>

#include <core/helper/types.hpp>

#include "./expected.hpp"
#include "./windows.hpp"

Expand Down Expand Up @@ -49,6 +50,12 @@ NLOHMANN_JSON_NAMESPACE_END

namespace json {

enum class ParseError : u8 {
OpenError,
ReadError,
FormatError,
};

template<typename T>
[[nodiscard]] helper::expected<T, std::string> try_parse_json(const std::string& content) noexcept {

Expand All @@ -68,22 +75,43 @@ namespace json {
}

template<typename T>
[[nodiscard]] helper::expected<T, std::string> try_parse_json_file(const std::filesystem::path& file) noexcept {
[[nodiscard]] helper::expected<T, std::pair<std::string, ParseError>> try_parse_json_file(
const std::filesystem::path& file
) noexcept {

if (not std::filesystem::exists(file)) {
return helper::unexpected<std::string>{ fmt::format("File '{}' doesn't exist", file.string()) };
return helper::unexpected<std::pair<std::string, ParseError>>{ std::make_pair<std::string, ParseError>(
fmt::format("File '{}' doesn't exist", file.string()), ParseError::OpenError
) };
}

std::ifstream file_stream{ file };

if (not file_stream.is_open()) {
return helper::unexpected<std::string>{ fmt::format("File '{}' couldn't be opened!", file.string()) };
return helper::unexpected<std::pair<std::string, ParseError>>{ std::make_pair<std::string, ParseError>(
fmt::format("File '{}' couldn't be opened!", file.string()), ParseError::OpenError
) };
}

std::stringstream result;
result << file_stream.rdbuf();

return try_parse_json<T>(result.str());
file_stream.close();

if (file_stream.fail()) {
return helper::unexpected<std::pair<std::string, ParseError>>{ std::make_pair<std::string, ParseError>(
fmt::format("Couldn't read from file '{}'", file.string()), ParseError::ReadError
) };
}

auto parse_result = try_parse_json<T>(result.str());
if (not parse_result.has_value()) {
return helper::unexpected<std::pair<std::string, ParseError>>{
std::make_pair<std::string, ParseError>(std::move(parse_result.error()), ParseError::FormatError)
};
}

return parse_result.value();
}

template<typename T>
Expand Down
3 changes: 2 additions & 1 deletion src/lobby/credentials/secret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ namespace {
auto result = json::try_parse_json_file<nlohmann::json>(file);

if (not result.has_value()) {
return helper::unexpected<std::string>{ result.error() };
auto [error, _] = result.error();
return helper::unexpected<std::string>{ error };
}

return result.value();
Expand Down
9 changes: 7 additions & 2 deletions src/manager/settings_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ SettingsManager::SettingsManager() {
if (result.has_value()) {
m_settings = result.value();
} else {
spdlog::error("unable to load settings from \"{}\": {}", settings_filename, result.error());
auto [error, error_type] = result.error();

spdlog::error("unable to load settings from \"{}\": {}", settings_filename, error);
spdlog::warn("applying default settings");

m_settings = {
Expand All @@ -28,7 +30,10 @@ SettingsManager::SettingsManager() {
.api_url = std::nullopt }
};

//TODO(Totto): save the file, if it doesn't exist, if it has an error, just leave it there
//save the default file, only if it doesn't exist, if it has an error, just leave it there
if (error_type == json::ParseError::OpenError) {
this->save();
}
}
}

Expand Down

0 comments on commit 88387f8

Please sign in to comment.