From a40585f41942a3656f3208bca057dd2a763969a2 Mon Sep 17 00:00:00 2001 From: sami Date: Wed, 9 Sep 2026 19:11:21 +0400 Subject: [PATCH] core: clang-format pass against the landed root .clang-format Pure formatting, no behaviour change. PKG landed .clang-format (Google base, 4-space indent, 100 cols); this brings util/ and the test harness into conformance so `clang-format --dry-run -Werror` is clean. Build and all six test binaries unchanged and green. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01HPPSGhiArbvQgwC2DNiURS --- core/include/vdm/util/error.hpp | 25 ++++---- core/include/vdm/util/event_bus.hpp | 4 +- core/include/vdm/util/log.hpp | 16 +++-- core/include/vdm/util/result.hpp | 41 ++++++++---- core/include/vdm/util/thread_pool.hpp | 6 +- core/src/util/error.cpp | 90 ++++++++++++++++++--------- core/src/util/log.cpp | 15 +++-- core/tests/support/vtest.hpp | 65 ++++++++++--------- core/tests/support/vtest_main.cpp | 4 +- core/tests/util/log_test.cpp | 4 +- 10 files changed, 155 insertions(+), 115 deletions(-) diff --git a/core/include/vdm/util/error.hpp b/core/include/vdm/util/error.hpp index 98efde5..febdcfe 100644 --- a/core/include/vdm/util/error.hpp +++ b/core/include/vdm/util/error.hpp @@ -49,12 +49,12 @@ enum class Error : std::uint16_t { // --- local I/O --- disk_full = 500, io_error, - path_rejected, // outside allowed roots or not writable - permission_denied, // EACCES on the destination + path_rejected, // outside allowed roots or not writable + permission_denied, // EACCES on the destination // --- resume metadata --- - meta_corrupt = 600, // bad magic / failed CRC - meta_version_unsupported, // written by a newer engine + meta_corrupt = 600, // bad magic / failed CRC + meta_version_unsupported, // written by a newer engine // --- probe --- probe_failed = 700, @@ -80,20 +80,17 @@ enum class Error : std::uint16_t { // std::string here is fine. struct ErrorInfo { Error code = Error::internal; - std::string context; // human-readable, for logs and event.notify bodies - int http_status = 0; // 0 when not HTTP-derived - bool retryable = false; // snapshot of is_retryable(code) at construction, may be - // overridden by the caller (e.g. probe_failed) - Error cause = Error::ok; // underlying error when `code` is a wrapper - // (max_retries_exhausted) + std::string context; // human-readable, for logs and event.notify bodies + int http_status = 0; // 0 when not HTTP-derived + bool retryable = false; // snapshot of is_retryable(code) at construction, may be + // overridden by the caller (e.g. probe_failed) + Error cause = Error::ok; // underlying error when `code` is a wrapper + // (max_retries_exhausted) ErrorInfo() = default; explicit ErrorInfo(Error c, std::string ctx = {}, int status = 0) - : code(c), - context(std::move(ctx)), - http_status(status), - retryable(is_retryable(c)) {} + : code(c), context(std::move(ctx)), http_status(status), retryable(is_retryable(c)) {} [[nodiscard]] std::string_view name() const noexcept { return error_name(code); } diff --git a/core/include/vdm/util/event_bus.hpp b/core/include/vdm/util/event_bus.hpp index b3d0c48..61e8dff 100644 --- a/core/include/vdm/util/event_bus.hpp +++ b/core/include/vdm/util/event_bus.hpp @@ -61,9 +61,7 @@ class EventBus { auto &slot = channels_[std::type_index(typeid(E))]; slot.push_back(Entry{ tok, - [h = std::move(handler)](const void *ev) { - h(*static_cast(ev)); - }, + [h = std::move(handler)](const void *ev) { h(*static_cast(ev)); }, }); return tok; } diff --git a/core/include/vdm/util/log.hpp b/core/include/vdm/util/log.hpp index 0a44bb6..c75aa60 100644 --- a/core/include/vdm/util/log.hpp +++ b/core/include/vdm/util/log.hpp @@ -45,8 +45,7 @@ class LogSink { class CallbackSink final : public LogSink { public: using Fn = std::function; - explicit CallbackSink(Fn fn, LogLevel min = LogLevel::trace) - : fn_(std::move(fn)), min_(min) {} + explicit CallbackSink(Fn fn, LogLevel min = LogLevel::trace) : fn_(std::move(fn)), min_(min) {} void write(const LogRecord &r) override { fn_(r); } [[nodiscard]] bool enabled(LogLevel l) const override { return l >= min_; } @@ -64,21 +63,20 @@ void log_emit(LogLevel, std::string_view category, std::string message); namespace detail { [[nodiscard]] bool log_wants(LogLevel); // sink installed && sink.enabled(level) -} +} // namespace detail } // namespace vdm -#define VDM_LOG(level, category, ...) \ +#define VDM_LOG(level, category, ...) \ do { \ - if (::vdm::detail::log_wants(level)) \ - ::vdm::log_emit((level), (category), \ - std::format(__VA_ARGS__)); \ + if (::vdm::detail::log_wants(level)) \ + ::vdm::log_emit((level), (category), std::format(__VA_ARGS__)); \ } while (0) #define VDM_LOG_TRACE(cat, ...) VDM_LOG(::vdm::LogLevel::trace, cat, __VA_ARGS__) #define VDM_LOG_DEBUG(cat, ...) VDM_LOG(::vdm::LogLevel::debug, cat, __VA_ARGS__) -#define VDM_LOG_INFO(cat, ...) VDM_LOG(::vdm::LogLevel::info, cat, __VA_ARGS__) -#define VDM_LOG_WARN(cat, ...) VDM_LOG(::vdm::LogLevel::warn, cat, __VA_ARGS__) +#define VDM_LOG_INFO(cat, ...) VDM_LOG(::vdm::LogLevel::info, cat, __VA_ARGS__) +#define VDM_LOG_WARN(cat, ...) VDM_LOG(::vdm::LogLevel::warn, cat, __VA_ARGS__) #define VDM_LOG_ERROR(cat, ...) VDM_LOG(::vdm::LogLevel::error, cat, __VA_ARGS__) #endif // VDM_UTIL_LOG_HPP diff --git a/core/include/vdm/util/result.hpp b/core/include/vdm/util/result.hpp index 2087bda..d3995c2 100644 --- a/core/include/vdm/util/result.hpp +++ b/core/include/vdm/util/result.hpp @@ -37,7 +37,9 @@ class [[nodiscard]] Result { !std::is_same_v, Result>>> Result(U &&value) : exp_(std::in_place, std::forward(value)) {} - Result() requires std::is_default_constructible_v : exp_(std::in_place) {} + Result() + requires std::is_default_constructible_v + : exp_(std::in_place) {} // Failure construction: implicit from an ErrorInfo/Err. Result(ErrorInfo error) : exp_(std::unexpected(std::move(error))) {} @@ -71,18 +73,30 @@ class [[nodiscard]] Result { // Monadic forwarding — see std::expected. `and_then` chains Result-returning // callables; `transform` maps the value; `transform_error` rewrites the failure. template - auto and_then(F &&f) & { return exp_.and_then(std::forward(f)); } + auto and_then(F &&f) & { + return exp_.and_then(std::forward(f)); + } template - auto and_then(F &&f) const & { return exp_.and_then(std::forward(f)); } + auto and_then(F &&f) const & { + return exp_.and_then(std::forward(f)); + } template - auto and_then(F &&f) && { return std::move(exp_).and_then(std::forward(f)); } + auto and_then(F &&f) && { + return std::move(exp_).and_then(std::forward(f)); + } template - auto transform(F &&f) & { return exp_.transform(std::forward(f)); } + auto transform(F &&f) & { + return exp_.transform(std::forward(f)); + } template - auto transform(F &&f) const & { return exp_.transform(std::forward(f)); } + auto transform(F &&f) const & { + return exp_.transform(std::forward(f)); + } template - auto transform(F &&f) && { return std::move(exp_).transform(std::forward(f)); } + auto transform(F &&f) && { + return std::move(exp_).transform(std::forward(f)); + } template auto transform_error(F &&f) const & { @@ -116,7 +130,9 @@ class [[nodiscard]] Result { }; // Explicit success sentinel for Result returns that reads better than `return {}`. -inline Result ok() { return {}; } +inline Result ok() { + return {}; +} } // namespace vdm @@ -135,11 +151,10 @@ inline Result ok() { return {}; } // VDM_TRY_ASSIGN(decl, expr): bind `decl` to the value of a successful Result, else // return its error. Usage: VDM_TRY_ASSIGN(auto n, read_some()); -#define VDM_TRY_ASSIGN(decl, expr) \ - auto VDM_DETAIL_CAT(_vdm_tmp_, __LINE__) = (expr); \ - if (!VDM_DETAIL_CAT(_vdm_tmp_, __LINE__).has_value()) \ - return ::vdm::ErrorInfo( \ - std::move(VDM_DETAIL_CAT(_vdm_tmp_, __LINE__)).error()); \ +#define VDM_TRY_ASSIGN(decl, expr) \ + auto VDM_DETAIL_CAT(_vdm_tmp_, __LINE__) = (expr); \ + if (!VDM_DETAIL_CAT(_vdm_tmp_, __LINE__).has_value()) \ + return ::vdm::ErrorInfo(std::move(VDM_DETAIL_CAT(_vdm_tmp_, __LINE__)).error()); \ decl = *std::move(VDM_DETAIL_CAT(_vdm_tmp_, __LINE__)) #endif // VDM_UTIL_RESULT_HPP diff --git a/core/include/vdm/util/thread_pool.hpp b/core/include/vdm/util/thread_pool.hpp index 948b86f..f507a47 100644 --- a/core/include/vdm/util/thread_pool.hpp +++ b/core/include/vdm/util/thread_pool.hpp @@ -46,13 +46,11 @@ class ThreadPool { // Enqueue `fn(args...)`; returns a future for its result. Throws std::runtime_error // if the pool is already shutting down. template - auto submit(F &&fn, Args &&...args) - -> std::future> { + auto submit(F &&fn, Args &&...args) -> std::future> { using R = std::invoke_result_t; auto task = std::make_shared>( - [f = std::forward(fn), - ... a = std::forward(args)]() mutable -> R { + [f = std::forward(fn), ... a = std::forward(args)]() mutable -> R { return std::invoke(std::move(f), std::move(a)...); }); std::future fut = task->get_future(); diff --git a/core/src/util/error.cpp b/core/src/util/error.cpp index e0afb72..0c2482e 100644 --- a/core/src/util/error.cpp +++ b/core/src/util/error.cpp @@ -6,34 +6,62 @@ namespace vdm { std::string_view error_name(Error e) noexcept { switch (e) { - case Error::ok: return "ok"; - case Error::canceled: return "canceled"; - case Error::resolve_failed: return "resolve_failed"; - case Error::connect_failed: return "connect_failed"; - case Error::tls_failed: return "tls_failed"; - case Error::connection_reset: return "connection_reset"; - case Error::timeout: return "timeout"; - case Error::too_many_redirects: return "too_many_redirects"; - case Error::http_client_error: return "http_client_error"; - case Error::http_server_error: return "http_server_error"; - case Error::auth_required: return "auth_required"; - case Error::forbidden: return "forbidden"; - case Error::not_found: return "not_found"; - case Error::range_not_satisfiable: return "range_not_satisfiable"; - case Error::gone: return "gone"; - case Error::server_file_changed: return "server_file_changed"; - case Error::content_length_mismatch: return "content_length_mismatch"; - case Error::checksum_mismatch: return "checksum_mismatch"; - case Error::disk_full: return "disk_full"; - case Error::io_error: return "io_error"; - case Error::path_rejected: return "path_rejected"; - case Error::permission_denied: return "permission_denied"; - case Error::meta_corrupt: return "meta_corrupt"; - case Error::meta_version_unsupported: return "meta_version_unsupported"; - case Error::probe_failed: return "probe_failed"; - case Error::unsupported_url_scheme: return "unsupported_url_scheme"; - case Error::max_retries_exhausted: return "max_retries_exhausted"; - case Error::internal: return "internal"; + case Error::ok: + return "ok"; + case Error::canceled: + return "canceled"; + case Error::resolve_failed: + return "resolve_failed"; + case Error::connect_failed: + return "connect_failed"; + case Error::tls_failed: + return "tls_failed"; + case Error::connection_reset: + return "connection_reset"; + case Error::timeout: + return "timeout"; + case Error::too_many_redirects: + return "too_many_redirects"; + case Error::http_client_error: + return "http_client_error"; + case Error::http_server_error: + return "http_server_error"; + case Error::auth_required: + return "auth_required"; + case Error::forbidden: + return "forbidden"; + case Error::not_found: + return "not_found"; + case Error::range_not_satisfiable: + return "range_not_satisfiable"; + case Error::gone: + return "gone"; + case Error::server_file_changed: + return "server_file_changed"; + case Error::content_length_mismatch: + return "content_length_mismatch"; + case Error::checksum_mismatch: + return "checksum_mismatch"; + case Error::disk_full: + return "disk_full"; + case Error::io_error: + return "io_error"; + case Error::path_rejected: + return "path_rejected"; + case Error::permission_denied: + return "permission_denied"; + case Error::meta_corrupt: + return "meta_corrupt"; + case Error::meta_version_unsupported: + return "meta_version_unsupported"; + case Error::probe_failed: + return "probe_failed"; + case Error::unsupported_url_scheme: + return "unsupported_url_scheme"; + case Error::max_retries_exhausted: + return "max_retries_exhausted"; + case Error::internal: + return "internal"; } return "unknown"; } @@ -48,7 +76,7 @@ bool is_retryable(Error e) noexcept { case Error::connection_reset: case Error::timeout: case Error::http_server_error: - case Error::range_not_satisfiable: // stale metadata → re-probe then retry + case Error::range_not_satisfiable: // stale metadata → re-probe then retry case Error::content_length_mismatch: return true; @@ -58,11 +86,11 @@ bool is_retryable(Error e) noexcept { case Error::tls_failed: case Error::too_many_redirects: case Error::http_client_error: - case Error::auth_required: // resolved by credentials, not a retry + case Error::auth_required: // resolved by credentials, not a retry case Error::forbidden: case Error::not_found: case Error::gone: - case Error::server_file_changed: // needs a user decision + case Error::server_file_changed: // needs a user decision case Error::checksum_mismatch: case Error::disk_full: case Error::io_error: diff --git a/core/src/util/log.cpp b/core/src/util/log.cpp index 59e51d9..c0b6d27 100644 --- a/core/src/util/log.cpp +++ b/core/src/util/log.cpp @@ -17,11 +17,16 @@ std::shared_ptr g_sink; std::string_view log_level_name(LogLevel l) noexcept { switch (l) { - case LogLevel::trace: return "trace"; - case LogLevel::debug: return "debug"; - case LogLevel::info: return "info"; - case LogLevel::warn: return "warn"; - case LogLevel::error: return "error"; + case LogLevel::trace: + return "trace"; + case LogLevel::debug: + return "debug"; + case LogLevel::info: + return "info"; + case LogLevel::warn: + return "warn"; + case LogLevel::error: + return "error"; } return "?"; } diff --git a/core/tests/support/vtest.hpp b/core/tests/support/vtest.hpp index 0c8e9bf..0aea90f 100644 --- a/core/tests/support/vtest.hpp +++ b/core/tests/support/vtest.hpp @@ -52,11 +52,11 @@ struct Registrar { Registrar(const char *name, void (*fn)()) { registry().push_back({name, fn}); } }; -inline void report(const char *file, int line, std::string_view expr, - std::string_view detail, bool fatal) { +inline void report(const char *file, int line, std::string_view expr, std::string_view detail, + bool fatal) { stats().failures++; - std::fprintf(stderr, " FAIL %s:%d %.*s", file, line, - static_cast(expr.size()), expr.data()); + std::fprintf(stderr, " FAIL %s:%d %.*s", file, line, static_cast(expr.size()), + expr.data()); if (!detail.empty()) std::fprintf(stderr, " [%.*s]", static_cast(detail.size()), detail.data()); std::fprintf(stderr, "\n"); @@ -110,46 +110,45 @@ inline int run_all() { } // namespace vt -#define VT_TEST(NAME) \ - static void NAME##_impl(); \ - static ::vt::Registrar NAME##_reg(#NAME, &NAME##_impl); \ +#define VT_TEST(NAME) \ + static void NAME##_impl(); \ + static ::vt::Registrar NAME##_reg(#NAME, &NAME##_impl); \ static void NAME##_impl() -#define VT_CHECK(COND) \ - do { \ - ::vt::stats().checks++; \ - if (!(COND)) \ - ::vt::report(__FILE__, __LINE__, #COND, {}, /*fatal=*/false); \ +#define VT_CHECK(COND) \ + do { \ + ::vt::stats().checks++; \ + if (!(COND)) \ + ::vt::report(__FILE__, __LINE__, #COND, {}, /*fatal=*/false); \ } while (0) -#define VT_REQUIRE(COND) \ - do { \ - ::vt::stats().checks++; \ - if (!(COND)) \ - ::vt::report(__FILE__, __LINE__, #COND, {}, /*fatal=*/true); \ +#define VT_REQUIRE(COND) \ + do { \ + ::vt::stats().checks++; \ + if (!(COND)) \ + ::vt::report(__FILE__, __LINE__, #COND, {}, /*fatal=*/true); \ } while (0) -#define VT_CHECK_EQ(A, B) \ +#define VT_CHECK_EQ(A, B) \ do { \ - ::vt::stats().checks++; \ - auto &&_a = (A); \ - auto &&_b = (B); \ - if (!(_a == _b)) \ - ::vt::report(__FILE__, __LINE__, #A " == " #B, \ - ::vt::show(_a) + " vs " + ::vt::show(_b), false); \ + ::vt::stats().checks++; \ + auto &&_a = (A); \ + auto &&_b = (B); \ + if (!(_a == _b)) \ + ::vt::report(__FILE__, __LINE__, #A " == " #B, \ + ::vt::show(_a) + " vs " + ::vt::show(_b), false); \ } while (0) -#define VT_CHECK_NE(A, B) \ +#define VT_CHECK_NE(A, B) \ do { \ - ::vt::stats().checks++; \ - auto &&_a = (A); \ - auto &&_b = (B); \ - if (!(_a != _b)) \ - ::vt::report(__FILE__, __LINE__, #A " != " #B, \ - ::vt::show(_a) + " vs " + ::vt::show(_b), false); \ + ::vt::stats().checks++; \ + auto &&_a = (A); \ + auto &&_b = (B); \ + if (!(_a != _b)) \ + ::vt::report(__FILE__, __LINE__, #A " != " #B, \ + ::vt::show(_a) + " vs " + ::vt::show(_b), false); \ } while (0) -#define VT_FAIL(MSG) \ - ::vt::report(__FILE__, __LINE__, "VT_FAIL", (MSG), /*fatal=*/false) +#define VT_FAIL(MSG) ::vt::report(__FILE__, __LINE__, "VT_FAIL", (MSG), /*fatal=*/false) #endif // VDM_TESTS_VTEST_HPP diff --git a/core/tests/support/vtest_main.cpp b/core/tests/support/vtest_main.cpp index c26b763..e80cce7 100644 --- a/core/tests/support/vtest_main.cpp +++ b/core/tests/support/vtest_main.cpp @@ -1,4 +1,6 @@ // vtest_main.cpp — shared entry point for every CORE test binary. #include "vtest.hpp" -int main() { return ::vt::run_all(); } +int main() { + return ::vt::run_all(); +} diff --git a/core/tests/util/log_test.cpp b/core/tests/util/log_test.cpp index c62e4e9..765ac3a 100644 --- a/core/tests/util/log_test.cpp +++ b/core/tests/util/log_test.cpp @@ -48,8 +48,8 @@ VT_TEST(log_forwards_to_sink_with_format) { VT_TEST(log_level_filter_skips_below_min) { SinkGuard g; auto count = std::make_shared(0); - vdm::set_log_sink(std::make_shared( - [count](const LogRecord &) { ++*count; }, LogLevel::warn)); + vdm::set_log_sink( + std::make_shared([count](const LogRecord &) { ++*count; }, LogLevel::warn)); VDM_LOG_DEBUG("x", "no"); VDM_LOG_INFO("x", "no");