Skip to content

Commit

Permalink
refactor: update return type of Config::load() to LoadResult
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Nov 19, 2023
1 parent 1a00912 commit ca0611d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
14 changes: 9 additions & 5 deletions source/cli/Config/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#include "Config.h"

bool Config::load()
LoadReuslt Config::load()
{
if (!std::filesystem::exists(_target_path)) {
return LoadReuslt::FileNotFound;
}

auto config_opt = json::open(_target_path);
if (!config_opt) {
std::cerr << "Failed to open config file: " << _target_path << std::endl;
return false;
return LoadReuslt::FailedToOpen;
}

auto& config = *config_opt;
if (!config.is_object()) {
std::cerr << "Json is not an object: " << config << std::endl;
return false;
return LoadReuslt::InvalidJson;
}

if (!parse(config)) {
std::cerr << "Failed to parse control: " << config << std::endl;
return false;
return LoadReuslt::ParseError;
}

return true;
return LoadReuslt::Success;
}

bool Config::save(const json::value& root)
Expand Down
11 changes: 10 additions & 1 deletion source/cli/Config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@

#include "../meojson/json.hpp"

enum class LoadReuslt
{
Success = 0,
FileNotFound = 1,
FailedToOpen = 2,
InvalidJson = 3,
ParseError = 4
};

class Config
{
public:
virtual ~Config() = default;

public:
virtual bool parse(const json::value& config_json) = 0;
virtual bool load();
virtual LoadReuslt load();
virtual json::value to_json() = 0;
virtual bool save(const json::value& root);
bool dump();
Expand Down
14 changes: 9 additions & 5 deletions source/cli/Config/TasksConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,30 @@ bool TasksConfig::parse(const json::value& config_opt)
return true;
}

bool TasksConfig::load()
LoadReuslt TasksConfig::load()
{
if (!std::filesystem::exists(_target_path)) {
return LoadReuslt::FileNotFound;
}

auto config_opt = json::open(_target_path);
if (!config_opt) {
std::cerr << "Failed to open config file: " << _target_path << std::endl;
return false;
return LoadReuslt::FailedToOpen;
}

auto& config = *config_opt;
if (!config.is_array()) {
std::cerr << "Json is not an array: " << config << std::endl;
return false;
return LoadReuslt::InvalidJson;
}

if (!parse(config)) {
std::cerr << "Failed to parse control: " << config << std::endl;
return false;
return LoadReuslt::ParseError;
}

return true;
return LoadReuslt::Success;
}

json::value TasksConfig::to_json()
Expand Down
2 changes: 1 addition & 1 deletion source/cli/Config/TasksConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TasksConfig : public Config

public:
bool parse(const json::value& config_opt) override;
bool load() override;
LoadReuslt load() override;
json::value to_json() override;

public:
Expand Down

0 comments on commit ca0611d

Please sign in to comment.