mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
- Replace vendored single-header toml11 (includes/toml.hpp) with the toml++ library (includes/toml++) - Rewrite Config to use toml::parse_file and expose typed get/getArray/getPath/contains accessors, storing parse errors instead of logging them - Update bundled license to toml++
209 lines
5.1 KiB
C++
209 lines
5.1 KiB
C++
#pragma once
|
|
|
|
#include <filesystem>
|
|
#include <optional>
|
|
#include <toml++/toml.h>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <cstddef>
|
|
|
|
namespace JuiceAgent::Config {
|
|
|
|
class Config {
|
|
private:
|
|
toml::table _config;
|
|
std::optional<toml::parse_error> _last_error;
|
|
|
|
std::filesystem::path _runtime_dir;
|
|
std::filesystem::path _config_path;
|
|
|
|
bool _valid = false;
|
|
|
|
public:
|
|
explicit Config(const std::filesystem::path& runtime_dir = {}) {
|
|
// Check if the runtime directory is empty
|
|
if (runtime_dir.empty()) {
|
|
// Use the default runtime directory
|
|
_runtime_dir = std::filesystem::current_path();
|
|
} else {
|
|
_runtime_dir = runtime_dir;
|
|
}
|
|
|
|
_config_path = _runtime_dir / "config.toml";
|
|
|
|
// Load the config file
|
|
try {
|
|
_config = toml::parse_file(_config_path.string());
|
|
_valid = true;
|
|
} catch (const toml::parse_error& e) {
|
|
// TODO: Log the error
|
|
_last_error = e;
|
|
}
|
|
}
|
|
|
|
|
|
template<typename T>
|
|
T get(const std::string& path) const {
|
|
const auto* node = _config.at_path(path).node();
|
|
|
|
if (!node) {
|
|
// TODO: Log the error
|
|
throw std::runtime_error(
|
|
"Configuration key not found: " + path
|
|
);
|
|
}
|
|
|
|
auto value = node->value<T>();
|
|
|
|
if (!value) {
|
|
// TODO: Log the error
|
|
throw std::runtime_error(
|
|
"Configuration type mismatch: " + path
|
|
);
|
|
}
|
|
|
|
return *value;
|
|
}
|
|
|
|
template<typename T>
|
|
T get(const std::string& path, T defaultValue) const {
|
|
const auto* node = _config.at_path(path).node();
|
|
|
|
if (!node) {
|
|
// Configuration key is missing; use the default value.
|
|
// TODO: Log the warning
|
|
return defaultValue;
|
|
}
|
|
|
|
auto value = node->value<T>();
|
|
|
|
if (!value) {
|
|
// Configuration type mismatch; use the default value.
|
|
// TODO: Log the warning
|
|
return defaultValue;
|
|
}
|
|
|
|
return *value;
|
|
}
|
|
|
|
std::string getPath(const std::string& key) const {
|
|
const std::filesystem::path value(get<std::string>(key));
|
|
|
|
if (value.is_absolute()) {
|
|
return value.lexically_normal().string();
|
|
}
|
|
|
|
const std::string normalized = value.generic_string();
|
|
|
|
if (normalized == "." || normalized.starts_with("./")) {
|
|
return (_runtime_dir / value).lexically_normal().string();
|
|
}
|
|
|
|
return value.lexically_normal().string();
|
|
}
|
|
|
|
template<typename T>
|
|
std::vector<T> getArray(const std::string& path) const {
|
|
const auto* node = _config.at_path(path).node();
|
|
|
|
if (!node) {
|
|
throw std::runtime_error(
|
|
"Configuration key not found: " + path
|
|
);
|
|
}
|
|
|
|
const auto* array = node->as_array();
|
|
|
|
if (!array) {
|
|
throw std::runtime_error(
|
|
"Configuration type mismatch: expected array: " + path
|
|
);
|
|
}
|
|
|
|
std::vector<T> result;
|
|
result.reserve(array->size());
|
|
|
|
for (std::size_t i = 0; i < array->size(); ++i) {
|
|
const auto& element = (*array)[i];
|
|
auto value = element.value<T>();
|
|
|
|
if (!value) {
|
|
throw std::runtime_error(
|
|
"Configuration array element type mismatch: "
|
|
+ path + "[" + std::to_string(i) + "]"
|
|
);
|
|
}
|
|
|
|
result.push_back(*value);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
template<typename T>
|
|
std::vector<T> getArray(
|
|
const std::string& path,
|
|
std::vector<T> defaultValue
|
|
) const {
|
|
const auto* node = _config.at_path(path).node();
|
|
|
|
if (!node) {
|
|
return defaultValue;
|
|
}
|
|
|
|
const auto* array = node->as_array();
|
|
|
|
if (!array) {
|
|
throw std::runtime_error(
|
|
"Configuration type mismatch: expected array: " + path
|
|
);
|
|
}
|
|
|
|
std::vector<T> result;
|
|
result.reserve(array->size());
|
|
|
|
for (std::size_t i = 0; i < array->size(); ++i) {
|
|
const auto& element = (*array)[i];
|
|
auto value = element.value<T>();
|
|
|
|
if (!value) {
|
|
throw std::runtime_error(
|
|
"Configuration array element type mismatch: "
|
|
+ path + "[" + std::to_string(i) + "]"
|
|
);
|
|
}
|
|
|
|
result.push_back(*value);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool contains(const std::string& path) const {
|
|
return _config.at_path(path).node() != nullptr;
|
|
}
|
|
|
|
// Getters
|
|
const toml::table& getConfig() const {
|
|
return _config;
|
|
}
|
|
|
|
const std::filesystem::path& getRuntimeDir() const {
|
|
return _runtime_dir;
|
|
}
|
|
|
|
const std::filesystem::path& getConfigPath() const {
|
|
return _config_path;
|
|
}
|
|
|
|
const std::optional<toml::parse_error>& getLastError() const {
|
|
return _last_error;
|
|
}
|
|
|
|
bool isValid() const {
|
|
return _valid;
|
|
}
|
|
};
|
|
|
|
} // namespace JuiceAgent::Config
|