#include <cstddef>
#include <exception>
#include <string>
#include <string_view>
#include <utility>
#include "rapidproto/rustgen/frame.hpp"
namespace {
constexpr int kInternalError = -1;
}
extern "C" {
using RapidprotoRustgenSink = void (*)(void* context, const char* data, std::size_t len);
int rapidproto_rustgen_run(const char* frame, std::size_t frame_len, RapidprotoRustgenSink sink,
void* context) noexcept {
int status = kInternalError;
std::string text;
try {
rapidproto::rustgen::FrameResult result =
rapidproto::rustgen::run_frame(std::string_view(frame, frame_len));
status = static_cast<int>(result.status);
text = std::move(result.text);
} catch (const std::exception& e) {
text = e.what();
} catch (...) {
text = "unknown exception";
}
sink(context, text.data(), text.size());
return status;
}
}