Add Hash, Random, Uuid, Encoding, Bits, Math modules + make Testing deps optional
New header-only modules: - Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals - Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string - Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces - Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode - Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks - Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation) Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added Detail/Color.hpp shim that delegates to Stacktrace::Color when present or provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler, ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE. Fix Repr.hpp reflect_struct to handle template specializations that lack std::meta::identifier_of (now guarded by has_identifier check). 33/33 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
42268c6c35
commit
a09e000f0f
65 files changed
+4959
-138
No files matched your search
@@ -0,0 +1,144 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Configuration/Env.hpp>
|
||||
#include <Void/FileSystem/Path.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Result/Error.hpp>
|
||||
#include <Void/Result/Result.hpp>
|
||||
|
||||
#include <format>
|
||||
#include <initializer_list>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Void::Configuration
|
||||
{
|
||||
|
||||
/// Layered key-value configuration store.
|
||||
///
|
||||
/// Values are plain strings. Layers are applied via overlay(): the last
|
||||
/// overlay wins on key conflicts. Typical usage:
|
||||
///
|
||||
/// auto cfg = Config::from_env({"DB_HOST", "DB_PORT"});
|
||||
/// if (auto r = Config::from_file(".env"); r.has_value())
|
||||
/// cfg.overlay(*r); // file values override env
|
||||
/// cfg.set("DB_PORT", "5433"); // explicit override wins everything
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
|
||||
Config() = default;
|
||||
|
||||
/// Populate from specific named environment variables.
|
||||
/// Keys absent from the environment are silently skipped.
|
||||
[[nodiscard]] static Config
|
||||
from_env(std::initializer_list<std::string_view> keys)
|
||||
{
|
||||
Config cfg;
|
||||
for (auto key : keys) {
|
||||
if (auto r = get_env(key); r.has_value()) {
|
||||
cfg.values_.emplace(std::string{key}, std::move(*r));
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Debug, "Configuration",
|
||||
std::format("from_env: loaded '{}'", key));
|
||||
}
|
||||
}
|
||||
return cfg;
|
||||
}
|
||||
|
||||
/// Parse a KEY=VALUE text file (one pair per line, # for comments).
|
||||
/// Lines without '=' produce a warning log and are skipped.
|
||||
[[nodiscard]] static Void::Result::Result<Config, Void::Result::Error>
|
||||
from_file(const std::filesystem::path& path)
|
||||
{
|
||||
auto text = Void::FileSystem::read_file(path);
|
||||
if (!text.has_value()) {
|
||||
return Void::Result::fail(std::format(
|
||||
"Configuration::Config::from_file: {}", text.error().message()));
|
||||
}
|
||||
Config cfg;
|
||||
std::istringstream ss{*text};
|
||||
std::string line;
|
||||
int lineno = 0;
|
||||
while (std::getline(ss, line)) {
|
||||
++lineno;
|
||||
if (!line.empty() && line.back() == '\r') {
|
||||
line.pop_back();
|
||||
}
|
||||
std::string_view sv{line};
|
||||
while (!sv.empty() && sv.front() == ' ') {
|
||||
sv.remove_prefix(1);
|
||||
}
|
||||
if (sv.empty() || sv.front() == '#') {
|
||||
continue;
|
||||
}
|
||||
auto eq = sv.find('=');
|
||||
if (eq == std::string_view::npos) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "Configuration",
|
||||
std::format("from_file: '{}' line {}: missing '='",
|
||||
path.string(), lineno));
|
||||
continue;
|
||||
}
|
||||
auto key = sv.substr(0, eq);
|
||||
while (!key.empty() && key.back() == ' ') {
|
||||
key.remove_suffix(1);
|
||||
}
|
||||
auto val = sv.substr(eq + 1);
|
||||
cfg.values_.emplace(std::string{key}, std::string{val});
|
||||
}
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Debug, "Configuration",
|
||||
std::format("from_file: loaded {} keys from '{}'",
|
||||
cfg.values_.size(), path.string()));
|
||||
return Void::Result::ok(std::move(cfg));
|
||||
}
|
||||
|
||||
/// Layer `other` on top: other's values win on key conflicts.
|
||||
void overlay(const Config& other)
|
||||
{
|
||||
for (const auto& [k, v] : other.values_) {
|
||||
values_.insert_or_assign(k, v);
|
||||
}
|
||||
}
|
||||
|
||||
/// Explicit per-key override (highest priority).
|
||||
void set(std::string_view key, std::string value)
|
||||
{
|
||||
values_.insert_or_assign(std::string{key}, std::move(value));
|
||||
}
|
||||
|
||||
/// Look up a key. Returns an error if the key is not present.
|
||||
[[nodiscard]] Void::Result::Result<std::string_view, Void::Result::Error>
|
||||
get(std::string_view key) const
|
||||
{
|
||||
auto it = values_.find(std::string{key});
|
||||
if (it == values_.end()) {
|
||||
return Void::Result::fail(std::format(
|
||||
"Configuration::Config::get: key '{}' not found", key));
|
||||
}
|
||||
return Void::Result::ok(std::string_view{it->second});
|
||||
}
|
||||
|
||||
/// Look up a key, returning `fallback` when the key is absent.
|
||||
[[nodiscard]] std::string_view
|
||||
get_or(std::string_view key, std::string_view fallback = {}) const
|
||||
{
|
||||
auto it = values_.find(std::string{key});
|
||||
return it != values_.end() ? std::string_view{it->second} : fallback;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t size() const noexcept
|
||||
{
|
||||
return values_.size();
|
||||
}
|
||||
[[nodiscard]] bool empty() const noexcept { return values_.empty(); }
|
||||
|
||||
private:
|
||||
|
||||
std::unordered_map<std::string, std::string> values_;
|
||||
};
|
||||
|
||||
} // namespace Void::Configuration
|
||||
Reference in new issue
Block a user