srs-control 0.2.2
Loading...
Searching...
No Matches
ConfigHandler.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "srs/utils/GlazeCustom.hpp" // IWYU pragma: keep
4#include <filesystem>
5#include <fmt/format.h>
6#include <glaze/core/opts.hpp>
7#include <glaze/core/reflect.hpp>
8#include <glaze/glaze.hpp>
9#include <glaze/json/read.hpp>
10#include <glaze/json/write.hpp>
11#include <glaze/yaml.hpp>
12#include <glaze/yaml/opts.hpp>
13#include <glaze/yaml/read.hpp>
14#include <glaze/yaml/write.hpp>
15#include <optional>
16#include <spdlog/spdlog.h>
17#include <string>
18#include <string_view>
19
20namespace srs::config
21{
22
23 enum class ConfigFileType
24 {
28 };
29
30 inline auto resolve_filetype(std::string_view filename) -> ConfigFileType
31 {
32 using enum ConfigFileType;
33 if (filename.ends_with(".json"))
34 {
35 return json;
36 }
37 else if (filename.ends_with(".yml") or filename.ends_with(".yaml"))
38 {
39
40 return yaml;
41 }
42 return invalid;
43 }
44
45 inline auto output_config_to_json(const auto& config, std::string_view json_filename) -> std::optional<std::string>
46 {
47 auto buffer = std::string{};
48 auto error_code = glz::write_file_json<glz::opts{ .prettify = true }>(config, json_filename, buffer);
49 if (error_code)
50 {
51 return fmt::format("Error occurred during writing the json file {}: {}",
52 json_filename,
53 glz::format_error(error_code, buffer));
54 }
55 return {};
56 }
57
58 inline auto set_config_from_json(auto& config, std::string_view json_filename) -> std::optional<std::string>
59 {
60 auto buffer = std::string{};
61 auto error_code = glz::read_file_json(config, json_filename, buffer);
62 if (error_code)
63 {
64 return fmt::format("Error occurred during reading the json file {}: {}",
65 json_filename,
66 glz::format_error(error_code, buffer));
67 }
68 return {};
69 }
70
71 inline auto output_config_to_yaml(const auto& config, std::string_view filename) -> std::optional<std::string>
72
73 {
74 auto buffer = std::string{};
75 auto error_code = glz::write_file_yaml<glz::yaml::yaml_opts{}>(config, filename, buffer);
76 if (error_code)
77 {
78 return fmt::format(
79 "Error occurred during writing the yaml file {}: {}", filename, glz::format_error(error_code, buffer));
80 }
81 return {};
82 }
83
84 inline auto set_config_from_yaml(auto& app_config, std::string_view filename) -> std::optional<std::string>
85 {
86 auto buffer = std::string{};
87 auto error_code = glz::read_file_yaml(app_config, filename, buffer);
88 if (error_code)
89 {
90 return fmt::format(
91 "Error occurred during reading the json file {}: {}", filename, glz::format_error(error_code, buffer));
92 }
93 return {};
94 }
95
96 inline auto output_config_to_file(const auto& config, std::string_view filename, bool is_overwrite = false)
97 -> std::optional<std::string>
98 {
99 if (not is_overwrite and std::filesystem::exists(filename))
100 {
101 return fmt::format("Cannot dump the default value to the configuration file {:?} as it's already existed!",
102 filename);
103 }
104
105 const auto parent_dir = std::filesystem::absolute(std::filesystem::path{ filename }).parent_path();
106 std::filesystem::create_directories(parent_dir);
107
108 auto file_type = resolve_filetype(filename);
109 using enum ConfigFileType;
110 switch (file_type)
111 {
112 case yaml:
113 return output_config_to_yaml(config, filename);
114 case json:
115 return output_config_to_json(config, filename);
116 case invalid:
117 default:
118 return fmt::format("Filename {:?} must have a suffix of .yml, .yaml or .json", filename);
119 }
120 return {};
121 }
122
123 inline auto set_config_from_file(auto& config, std::string_view filename, bool is_default_generated = true)
124 -> std::optional<std::string>
125 {
126 if (filename.empty())
127 {
128 return std::string{ "Config filename is empty!" };
129 }
130 auto file_type = resolve_filetype(filename);
131 if (not std::filesystem::exists(filename))
132 {
133 if (is_default_generated)
134 {
135 spdlog::warn("configuration file {:?} doesn't exist. Creating a new file with default values",
136 filename);
137 output_config_to_file(config, filename);
138 }
139 else
140 {
141 return fmt::format("configuration file {:?} doesn't exist!", filename);
142 }
143 }
144 else
145 {
146 using enum ConfigFileType;
147 switch (file_type)
148 {
149 case yaml:
150 return set_config_from_yaml(config, filename);
151 break;
152 case json:
153 return set_config_from_json(config, filename);
154 break;
155 case invalid:
156 default:
157 return fmt::format("Filename {:?} must have a suffix of .yml, .yaml or .json", filename);
158 }
159 }
160 return {};
161 }
162} // namespace srs::config
auto output_config_to_yaml(const auto &config, std::string_view filename) -> std::optional< std::string >
auto output_config_to_file(const auto &config, std::string_view filename, bool is_overwrite=false) -> std::optional< std::string >
auto set_config_from_yaml(auto &app_config, std::string_view filename) -> std::optional< std::string >
auto output_config_to_json(const auto &config, std::string_view json_filename) -> std::optional< std::string >
auto set_config_from_json(auto &config, std::string_view json_filename) -> std::optional< std::string >
auto resolve_filetype(std::string_view filename) -> ConfigFileType
auto set_config_from_file(auto &config, std::string_view filename, bool is_default_generated=true) -> std::optional< std::string >