Create common json_parser_utils functions
Create a json_parser_utils namespace containing common functions for
parsing JSON.
Extract the common functions from the JSON parsing code in the
phosphor-regulators and phosphor-power-sequencer applications.
Both applications have some identical parsing functions.
Create a common ConfigFileParserError class. The phosphor-regulators and
phosphor-power-sequencer applications both have an identical version of
this exception class.
Extract the common test cases from the two applications and put them in
a common location as well.
Summary:
* Common JSON parsing functions in
phosphor-power-sequencer/src/config_file_parser.* and
phosphor-regulators/src/config_file_parser.* moved to
json_parser_utils.*
* Common test cases in
phosphor-power-sequencer/test/config_file_parser_tests.cpp and
phosphor-regulators/test/config_file_parser_tests.cpp moved to
test/json_parser_utils_tests.cpp
* phosphor-power-sequencer/src/config_file_parser_error.hpp and
phosphor-regulators/src/config_file_parser_error.hpp replaced with
config_file_parser_error.hpp
* phosphor-power-sequencer/test/config_file_parser_error_tests.cpp and
phosphor-regulators/test/config_file_parser_error_tests.cpp replaced
with test/config_file_parser_error_tests.cpp
Tested:
* Ran automated test cases.
Change-Id: I35074c5e42d9e89def41ba8e729fe11c54ed8d27
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
diff --git a/config_file_parser_error.hpp b/config_file_parser_error.hpp
new file mode 100644
index 0000000..8b3e16e
--- /dev/null
+++ b/config_file_parser_error.hpp
@@ -0,0 +1,85 @@
+/**
+ * Copyright © 2025 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <exception>
+#include <filesystem>
+#include <string>
+
+namespace phosphor::power::util
+{
+
+/**
+ * @class ConfigFileParserError
+ *
+ * An error that occurred while parsing a JSON configuration file.
+ */
+class ConfigFileParserError : public std::exception
+{
+ public:
+ // Specify which compiler-generated methods we want
+ ConfigFileParserError() = delete;
+ ConfigFileParserError(const ConfigFileParserError&) = default;
+ ConfigFileParserError(ConfigFileParserError&&) = default;
+ ConfigFileParserError& operator=(const ConfigFileParserError&) = default;
+ ConfigFileParserError& operator=(ConfigFileParserError&&) = default;
+ virtual ~ConfigFileParserError() = default;
+
+ /**
+ * Constructor.
+ *
+ * @param pathName Configuration file path name
+ * @param error Error message
+ */
+ explicit ConfigFileParserError(const std::filesystem::path& pathName,
+ const std::string& error) :
+ pathName{pathName},
+ error{"ConfigFileParserError: " + pathName.string() + ": " + error}
+ {}
+
+ /**
+ * Returns the configuration file path name.
+ *
+ * @return path name
+ */
+ const std::filesystem::path& getPathName()
+ {
+ return pathName;
+ }
+
+ /**
+ * Returns the description of this error.
+ *
+ * @return error description
+ */
+ const char* what() const noexcept override
+ {
+ return error.c_str();
+ }
+
+ private:
+ /**
+ * Configuration file path name.
+ */
+ const std::filesystem::path pathName;
+
+ /**
+ * Error message.
+ */
+ const std::string error{};
+};
+
+} // namespace phosphor::power::util