From f1cb110e52647823f7cd819254285968fb6f06be Mon Sep 17 00:00:00 2001 From: Gabriel Cruz <8129788+gmelodie@users.noreply.github.com> Date: Tue, 30 Jun 2026 11:47:34 -0300 Subject: [PATCH] fix: stale echo.hpp (#103) --- examples/echo/cpp_bindings/echo.hpp | 34 +++++++++++++---------------- ffi.nimble | 4 +++- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/examples/echo/cpp_bindings/echo.hpp b/examples/echo/cpp_bindings/echo.hpp index c99b799..fa89091 100644 --- a/examples/echo/cpp_bindings/echo.hpp +++ b/examples/echo/cpp_bindings/echo.hpp @@ -145,17 +145,20 @@ inline CborError encode_cbor(CborEncoder& e, const std::optional& v) { // ── decode_cbor overloads ─────────────────────────────────────────────── -inline CborError decode_cbor(CborValue& it, bool& out) { - if (!cbor_value_is_boolean(&it)) return CborErrorImproperValue; - CborError err = cbor_value_get_boolean(&it, &out); +// After reading a leaf value, the parser must advance past it; both steps +// short-circuit on the same CborError, so they always travel together. +inline CborError advance_if_ok(CborValue& it, CborError err) { if (err) return err; return cbor_value_advance(&it); } + +inline CborError decode_cbor(CborValue& it, bool& out) { + if (!cbor_value_is_boolean(&it)) return CborErrorImproperValue; + return advance_if_ok(it, cbor_value_get_boolean(&it, &out)); +} inline CborError decode_cbor(CborValue& it, int64_t& out) { if (!cbor_value_is_integer(&it)) return CborErrorImproperValue; - CborError err = cbor_value_get_int64_checked(&it, &out); - if (err) return err; - return cbor_value_advance(&it); + return advance_if_ok(it, cbor_value_get_int64_checked(&it, &out)); } inline CborError decode_cbor(CborValue& it, int32_t& out) { int64_t tmp = 0; @@ -166,15 +169,11 @@ inline CborError decode_cbor(CborValue& it, int32_t& out) { } inline CborError decode_cbor(CborValue& it, uint64_t& out) { if (!cbor_value_is_unsigned_integer(&it)) return CborErrorImproperValue; - CborError err = cbor_value_get_uint64(&it, &out); - if (err) return err; - return cbor_value_advance(&it); + return advance_if_ok(it, cbor_value_get_uint64(&it, &out)); } inline CborError decode_cbor(CborValue& it, double& out) { if (cbor_value_is_double(&it)) { - CborError err = cbor_value_get_double(&it, &out); - if (err) return err; - return cbor_value_advance(&it); + return advance_if_ok(it, cbor_value_get_double(&it, &out)); } if (cbor_value_is_float(&it)) { float f = 0.0f; @@ -191,9 +190,8 @@ inline CborError decode_cbor(CborValue& it, std::string& out) { CborError err = cbor_value_get_string_length(&it, &len); if (err) return err; out.resize(len); - err = cbor_value_copy_text_string(&it, out.empty() ? nullptr : &out[0], &len, nullptr); - if (err) return err; - return cbor_value_advance(&it); + return advance_if_ok( + it, cbor_value_copy_text_string(&it, out.empty() ? nullptr : &out[0], &len, nullptr)); } template @@ -222,10 +220,8 @@ inline CborError decode_cbor(CborValue& it, std::vector& out) { CborError err = cbor_value_get_string_length(&it, &len); if (err) return err; out.resize(len); - err = cbor_value_copy_byte_string( - &it, out.empty() ? nullptr : out.data(), &len, nullptr); - if (err) return err; - return cbor_value_advance(&it); + return advance_if_ok( + it, cbor_value_copy_byte_string(&it, out.empty() ? nullptr : out.data(), &len, nullptr)); } template diff --git a/ffi.nimble b/ffi.nimble index d7002e9..da5f40f 100644 --- a/ffi.nimble +++ b/ffi.nimble @@ -201,8 +201,10 @@ task check_bindings_rust, "Verify checked-in Rust bindings match Nim source": task check_bindings_cpp, "Verify checked-in C++ bindings match Nim source": exec "nimble genbindings_cpp" + exec "nimble genbindings_cpp_echo" exec "git diff --exit-code --" & " examples/timer/cpp_bindings/my_timer.hpp" & - " examples/timer/cpp_bindings/CMakeLists.txt" + " examples/timer/cpp_bindings/CMakeLists.txt" & + " examples/echo/cpp_bindings/echo.hpp" & " examples/echo/cpp_bindings/CMakeLists.txt" task check_bindings, "Verify all checked-in example bindings match Nim source": exec "nimble check_bindings_rust"