File size: 9,271 Bytes
2492322 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | #ifndef MAMBA_API_CONFIGURATION_IMPL_HPP
#define MAMBA_API_CONFIGURATION_IMPL_HPP
#include <optional>
#include <string>
#include <vector>
#include <yaml-cpp/yaml.h>
#include "mamba/core/common_types.hpp"
#include "mamba/core/context.hpp"
#include "mamba/fs/filesystem.hpp"
namespace mamba
{
namespace detail
{
// Because CLI11 supports std::optional for options but not for flags...
/**************
* cli_config *
**************/
template <class T>
struct cli_config
{
using storage_type = std::optional<T>;
storage_type m_storage;
cli_config() = default;
cli_config(const T& value)
: m_storage(value)
{
}
storage_type& storage()
{
return m_storage;
}
bool has_value() const
{
return m_storage.has_value();
}
const T& value() const
{
return m_storage.value();
}
void reset()
{
m_storage.reset();
}
};
/**********************
* Source declaration *
**********************/
template <class T>
struct Source
{
static std::vector<std::string> default_value(const T&)
{
return std::vector<std::string>({ "default" });
};
static void merge(
const std::map<std::string, T>& values,
const std::vector<std::string>& sources,
T& value,
std::vector<std::string>& source
);
static T deserialize(const std::string& value);
static bool is_sequence();
};
template <class T>
struct Source<std::vector<T>>
{
static std::vector<std::string> default_value(const std::vector<T>& init)
{
return std::vector<std::string>(std::max<size_t>(1, init.size()), "default");
};
static void merge(
const std::map<std::string, std::vector<T>>& values,
const std::vector<std::string>& sources,
std::vector<T>& value,
std::vector<std::string>& source
);
static std::vector<T> deserialize(const std::string& value);
static bool is_sequence();
};
/*************************
* Source implementation *
*************************/
template <class T>
void Source<T>::merge(
const std::map<std::string, T>& values,
const std::vector<std::string>& sources,
T& value,
std::vector<std::string>& source
)
{
source = sources;
value = values.at(sources.front());
}
template <class T>
T Source<T>::deserialize(const std::string& value)
{
if (value.empty())
{
return YAML::Node("").as<T>();
}
else
{
return YAML::Load(value).as<T>();
}
}
template <class T>
bool Source<T>::is_sequence()
{
return false;
}
template <class T>
void Source<std::vector<T>>::merge(
const std::map<std::string, std::vector<T>>& values,
const std::vector<std::string>& sources,
std::vector<T>& value,
std::vector<std::string>& source
)
{
value.clear();
source.clear();
for (auto& s : sources)
{
auto& vec = values.at(s);
for (auto& v : vec)
{
auto find_v = std::find(value.begin(), value.end(), v);
if (find_v == value.end())
{
value.push_back(v);
source.push_back(s);
}
}
}
}
template <class T>
std::vector<T> Source<std::vector<T>>::deserialize(const std::string& value)
{
return YAML::Load("[" + value + "]").as<std::vector<T>>();
}
template <class T>
bool Source<std::vector<T>>::is_sequence()
{
return true;
}
}
}
/****************
* YAML parsers *
****************/
namespace YAML
{
template <class T>
struct convert<std::optional<T>>
{
static Node encode(const T& rhs)
{
return Node(rhs.value());
}
static bool decode(const Node& node, std::optional<T>& rhs)
{
if (!node.IsScalar())
{
return false;
}
rhs = std::optional<T>(node.as<T>());
return true;
}
};
template <>
struct convert<mamba::VerificationLevel>
{
static Node encode(const mamba::VerificationLevel& rhs)
{
if (rhs == mamba::VerificationLevel::Disabled)
{
return Node("disabled");
}
else if (rhs == mamba::VerificationLevel::Warn)
{
return Node("warn");
}
else if (rhs == mamba::VerificationLevel::Enabled)
{
return Node("enabled");
}
else
{
return Node();
}
}
static bool decode(const Node& node, mamba::VerificationLevel& rhs)
{
if (!node.IsScalar())
{
return false;
}
auto str = node.as<std::string>();
if (str == "enabled")
{
rhs = mamba::VerificationLevel::Enabled;
}
else if (str == "warn")
{
rhs = mamba::VerificationLevel::Warn;
}
else if (str == "disabled")
{
rhs = mamba::VerificationLevel::Disabled;
}
else
{
throw std::runtime_error(
"Invalid 'VerificationLevel', should be in {'enabled', 'warn', 'disabled'}"
);
}
return true;
}
};
template <>
struct convert<mamba::ChannelPriority>
{
static Node encode(const mamba::ChannelPriority& rhs)
{
if (rhs == mamba::ChannelPriority::Strict)
{
return Node("strict");
}
else if (rhs == mamba::ChannelPriority::Flexible)
{
return Node("flexible");
}
else if (rhs == mamba::ChannelPriority::Disabled)
{
return Node("disabled");
}
else
{
return Node();
}
}
static bool decode(const Node& node, mamba::ChannelPriority& rhs)
{
if (!node.IsScalar())
{
return false;
}
auto str = node.as<std::string>();
if (str == "strict")
{
rhs = mamba::ChannelPriority::Strict;
}
else if ((str == "flexible") || (str == "true"))
{
rhs = mamba::ChannelPriority::Flexible;
}
else if (str == "disabled")
{
rhs = mamba::ChannelPriority::Disabled;
}
else
{
return false;
}
return true;
}
};
template <>
struct convert<mamba::fs::u8path>
{
static Node encode(const mamba::fs::u8path& rhs)
{
return Node(rhs.string());
}
static bool decode(const Node& node, mamba::fs::u8path& rhs)
{
if (!node.IsScalar())
{
return false;
}
rhs = mamba::fs::u8path(node.as<std::string>());
return true;
}
};
template <>
struct convert<mamba::log_level>
{
private:
inline static const std::array<std::string, 7> log_level_names = {
"trace", "debug", "info", "warning", "error", "critical", "off"
};
public:
static Node encode(const mamba::log_level& rhs)
{
return Node(log_level_names[static_cast<size_t>(rhs)]);
}
static bool decode(const Node& node, mamba::log_level& rhs)
{
auto name = node.as<std::string>();
auto it = std::find(log_level_names.begin(), log_level_names.end(), name);
if (it != log_level_names.end())
{
rhs = static_cast<mamba::log_level>(it - log_level_names.begin());
return true;
}
LOG_ERROR << "Invalid log level, should be in {'critical', 'error', 'warning', 'info', 'debug', 'trace', 'off'} but is '"
<< name << "'";
return false;
}
};
}
#endif
|