From ca0611d5b6c5f173f390a1075c7439736244ffcc Mon Sep 17 00:00:00 2001 From: dongwlin Date: Sun, 19 Nov 2023 11:41:41 +0800 Subject: [PATCH] refactor: update return type of Config::load() to LoadResult --- source/cli/Config/Config.cpp | 14 +++++++++----- source/cli/Config/Config.h | 11 ++++++++++- source/cli/Config/TasksConfig.cpp | 14 +++++++++----- source/cli/Config/TasksConfig.h | 2 +- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/source/cli/Config/Config.cpp b/source/cli/Config/Config.cpp index 2a32bd6..5e3a5f7 100644 --- a/source/cli/Config/Config.cpp +++ b/source/cli/Config/Config.cpp @@ -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) diff --git a/source/cli/Config/Config.h b/source/cli/Config/Config.h index 4db5001..2dce222 100644 --- a/source/cli/Config/Config.h +++ b/source/cli/Config/Config.h @@ -6,6 +6,15 @@ #include "../meojson/json.hpp" +enum class LoadReuslt +{ + Success = 0, + FileNotFound = 1, + FailedToOpen = 2, + InvalidJson = 3, + ParseError = 4 +}; + class Config { public: @@ -13,7 +22,7 @@ class Config 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(); diff --git a/source/cli/Config/TasksConfig.cpp b/source/cli/Config/TasksConfig.cpp index 857ced8..81e7e33 100644 --- a/source/cli/Config/TasksConfig.cpp +++ b/source/cli/Config/TasksConfig.cpp @@ -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() diff --git a/source/cli/Config/TasksConfig.h b/source/cli/Config/TasksConfig.h index 25b72aa..b61dece 100644 --- a/source/cli/Config/TasksConfig.h +++ b/source/cli/Config/TasksConfig.h @@ -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: