#include "rapidproto/rustgen/frame.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
#include "rapidproto/arenagen/modes.hpp"
#include "rapidproto/resolve.hpp"
#include "rapidproto/resolver.hpp"
#include "rapidproto/result.hpp"
#include "rapidproto/rustgen/generator.hpp"
#include "rapidproto/rustgen/names.hpp"
#include "rapidproto/rustgen/pipeline.hpp"
#include "rapidproto/source.hpp"
namespace rapidproto::rustgen {
namespace {
constexpr unsigned kBitsPerByte = 8;
constexpr std::size_t kU32Bytes = 4;
class FrameReader {
public:
explicit FrameReader(std::string_view data) : m_data(data) {}
void expect_magic() {
static constexpr std::string_view kMagic = "RPWF5\n";
if (m_data.substr(m_pos, kMagic.size()) != kMagic) {
fail("input is not a RPWF5 frame");
return;
}
m_pos += kMagic.size();
}
[[nodiscard]] std::uint32_t u32() {
if (!ok() || m_data.size() - m_pos < kU32Bytes) {
fail("truncated frame: expected a 4-byte length");
return 0;
}
std::uint32_t value = 0;
for (std::size_t i = 0; i < kU32Bytes; ++i) {
value |= static_cast<std::uint32_t>(static_cast<unsigned char>(m_data[m_pos + i]))
<< (kBitsPerByte * i);
}
m_pos += kU32Bytes;
return value;
}
[[nodiscard]] std::uint8_t u8() {
if (!ok() || m_data.size() - m_pos < 1) {
fail("truncated frame: expected a byte");
return 0;
}
return static_cast<std::uint8_t>(m_data[m_pos++]);
}
[[nodiscard]] std::string bytes() {
const std::uint32_t len = u32();
if (!ok() || m_data.size() - m_pos < len) {
fail("truncated frame: declared length runs past the input");
return {};
}
std::string out(m_data.substr(m_pos, len));
m_pos += len;
return out;
}
void expect_end() {
if (ok() && m_pos != m_data.size()) {
fail("trailing bytes after the frame");
}
}
[[nodiscard]] std::size_t remaining() const { return m_data.size() - m_pos; }
[[nodiscard]] bool ok() const { return m_error.empty(); }
[[nodiscard]] const std::string& error() const { return m_error; }
private:
void fail(std::string message) {
if (ok()) {
m_error = std::move(message);
}
}
std::string_view m_data;
std::size_t m_pos = 0;
std::string m_error;
};
bool is_module_path(std::string_view path) {
constexpr std::string_view kRoot = "crate";
constexpr std::string_view kSeparator = "::";
constexpr std::string_view kRaw = "r#";
if (path.substr(0, kRoot.size()) != kRoot) {
return false;
}
path.remove_prefix(kRoot.size());
const auto alpha = [](char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
};
const auto digit = [](char c) { return c >= '0' && c <= '9'; };
while (!path.empty()) {
if (path.substr(0, kSeparator.size()) != kSeparator) {
return false;
}
path.remove_prefix(kSeparator.size());
const std::string_view seg = path.substr(0, path.find(kSeparator));
path.remove_prefix(seg.size());
const std::string_view name =
seg.substr(0, kRaw.size()) == kRaw ? seg.substr(kRaw.size()) : seg;
if (name.empty() || name == "_" || is_raw_ineligible(name) || !alpha(name.front()) ||
!std::all_of(name.begin(), name.end(), [&](char c) { return alpha(c) || digit(c); })) {
return false;
}
}
return true;
}
FrameResult failure(FrameStatus status, std::string message) {
return FrameResult{status, std::move(message)};
}
struct Request {
std::vector<std::string> entries;
std::unordered_map<std::string, std::string> sources; std::string modes_text;
bool unknown_all = false;
bool emit_arena = false;
bool emit_stream = false;
bool emit_serde = false;
std::string module_path;
};
std::optional<std::string> read_request(std::string_view frame, Request& request) {
FrameReader reader(frame);
reader.expect_magic();
const std::uint32_t n_entries = reader.u32();
request.entries.reserve(std::min<std::size_t>(n_entries, reader.remaining() / kU32Bytes));
for (std::uint32_t i = 0; i < n_entries && reader.ok(); ++i) {
request.entries.push_back(reader.bytes());
}
const std::uint32_t n_files = reader.u32();
request.sources.reserve(
std::min<std::size_t>(n_files, reader.remaining() / (kU32Bytes + kU32Bytes)));
for (std::uint32_t i = 0; i < n_files && reader.ok(); ++i) {
std::string name = reader.bytes();
std::string body = reader.bytes();
request.sources.emplace(std::move(name), std::move(body));
}
request.modes_text = reader.bytes();
request.unknown_all = reader.u8() != 0;
request.emit_arena = reader.u8() != 0;
request.emit_stream = reader.u8() != 0;
request.emit_serde = reader.u8() != 0;
request.module_path = reader.bytes();
reader.expect_end();
if (!reader.ok()) {
return reader.error();
}
return std::nullopt;
}
std::optional<std::string> check_options(const Request& request) {
if (request.entries.empty()) {
return "no entry files given";
}
if (!request.emit_arena && !request.emit_stream) {
return "no decoder model selected: enable arena and/or stream";
}
if (!is_module_path(request.module_path)) {
return "module path '" + request.module_path +
"' must be `crate` or a path from it of ASCII identifiers, such as `crate::proto`";
}
return std::nullopt;
}
std::optional<std::string> resolve_modes(const Request& request, const ResolvedFileSet& set,
const SymbolTable& symbols, arenagen::FieldModes& modes) {
if (request.modes_text.empty() && !request.unknown_all) {
return std::nullopt;
}
arenagen::FieldModesSpec spec;
spec.unknown_all = request.unknown_all;
if (!request.modes_text.empty()) {
if (auto r = arenagen::parse_modes_file(request.modes_text, "<field-modes>", spec); !r) {
return r.error().message;
}
}
auto resolved = arenagen::resolve_field_modes(spec, set, symbols);
if (!resolved) {
return resolved.error().message;
}
modes = std::move(resolved).value();
return std::nullopt;
}
FrameResult generate(const Request& request) {
ResolverConfig config;
config.source_provider = [&request](const std::string& name) -> std::optional<std::string> {
const auto it = request.sources.find(name);
if (it == request.sources.end()) {
return std::nullopt;
}
return it->second;
};
SourceRegistry registry; auto resolved = resolve(request.entries, config, registry);
if (!resolved) {
return failure(FrameStatus::kResolveError, render_error(resolved.error(), registry));
}
ResolvedFileSet set = std::move(resolved).value();
auto analyzed = analyze(set);
if (!analyzed) {
return failure(FrameStatus::kResolveError, render_error(analyzed.error(), registry));
}
const SymbolTable symbols = std::move(analyzed).value();
arenagen::FieldModes modes;
if (auto error = resolve_modes(request, set, symbols, modes)) {
return failure(FrameStatus::kModesError, std::move(*error));
}
const RustOptions options{request.emit_arena, request.emit_stream, request.module_path,
request.emit_serde};
return FrameResult{FrameStatus::kOk, generate_rust(set, symbols, modes, options)};
}
}
FrameResult run_frame(std::string_view frame) {
Request request;
if (auto error = read_request(frame, request)) {
return failure(FrameStatus::kBadFrame, std::move(*error));
}
if (auto error = check_options(request)) {
return failure(FrameStatus::kBadFrame, std::move(*error));
}
return generate(request);
}
}