#include "rustduck-sys/native/bridge/report.h"
#include "rustduck-sys/native/bridge/test_hooks.h"
#include "rustduck-sys/native/tspy.h"
#include "rustduck-sys/src/bridge.rs.h"
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <type_traits>
struct RustDuckReportState final {
RustDuckReportState(void *report, bool owned, bool asynchronous) noexcept
: report(report), owned(owned), asynchronous(asynchronous) {}
void terminate() noexcept {
if (asynchronous && !terminated) {
tspyTerminateAsyncReport(report);
terminated = true;
}
}
~RustDuckReportState() noexcept {
terminate();
if (owned) {
tspyDeleteReport(report);
}
}
void *report;
bool owned;
bool asynchronous;
bool terminated = false;
};
namespace {
std::unique_ptr<RustDuckReport> make_report(void *report, bool owned,
bool asynchronous) {
if (report == nullptr) {
throw std::runtime_error("native report construction failed");
}
try {
return std::make_unique<RustDuckReport>(report, owned, asynchronous);
} catch (...) {
if (asynchronous) {
tspyTerminateAsyncReport(report);
}
if (owned) {
tspyDeleteReport(report);
}
throw;
}
}
void *report_forward_callback(int severity, const tspy_uchar *message,
std::size_t size) noexcept {
try {
const bool message_valid = message != nullptr || size == 0;
const auto *bytes = reinterpret_cast<const std::uint8_t *>(message);
const rust::Slice<const std::uint8_t> message_slice(
bytes, message_valid ? size : 0);
rustduck_report_forward_callback(severity, message_slice, message_valid);
} catch (...) {
}
return nullptr;
}
static_assert(std::is_convertible_v<decltype(&report_forward_callback),
tspy_log_callback>);
}
RustDuckReport::RustDuckReport(void *report, bool owned, bool asynchronous)
: state_(
std::make_shared<RustDuckReportState>(report, owned, asynchronous)) {}
void *RustDuckReport::raw() const noexcept { return state_->report; }
std::shared_ptr<void> RustDuckReport::retain() const noexcept { return state_; }
void RustDuckReport::terminate() noexcept { state_->terminate(); }
RustDuckReport::~RustDuckReport() noexcept = default;
std::unique_ptr<RustDuckReport> rustduck_report_null() {
rustduck_test_maybe_fail(RustDuckFailurePoint::ReportConstruct);
return make_report(tspyNullReport(), false, false);
}
std::unique_ptr<RustDuckReport> rustduck_report_stderr() {
rustduck_test_maybe_fail(RustDuckFailurePoint::ReportConstruct);
return make_report(tspyStdErrReport(), false, false);
}
std::unique_ptr<RustDuckReport>
rustduck_report_asynchronous(int severity, bool synchronous_log, bool timed_log,
std::size_t log_message_count) {
rustduck_test_maybe_fail(RustDuckFailurePoint::ReportConstruct);
return make_report(tspyNewAsyncReport(severity, synchronous_log, timed_log,
log_message_count),
true, true);
}
std::unique_ptr<RustDuckReport>
rustduck_report_asynchronous_with_forwarder(int severity, bool synchronous_log,
std::size_t log_message_count) {
rustduck_test_maybe_fail(RustDuckFailurePoint::ReportConstruct);
return make_report(tspyNewPyAsyncReport(report_forward_callback, severity,
synchronous_log, log_message_count),
true, true);
}
void rustduck_report_set_max_severity(RustDuckReport &report, int severity) {
tspySetMaxSeverity(report.raw(), severity);
}
void rustduck_report_log(const RustDuckReport &report, int severity,
rust::Slice<const std::uint8_t> message) {
tspyLogReport(report.raw(), severity, rustduck::utf16_data(message),
rustduck::utf16_size(message));
}
void rustduck_report_terminate(RustDuckReport &report) { report.terminate(); }
#ifdef RUSTDUCK_NATIVE_TEST_HOOKS
void rustduck_test_throw_standard() { throw std::runtime_error("test"); }
void rustduck_test_throw_unknown() { throw 1; }
#endif