Make code generator self-contained by integrating runtime support
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
roto-runtime = { path = "../runtime" }
|
||||
|
||||
roto-tonic = { path = "../roto-tonic" }
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
log = "0.4"
|
||||
|
||||
@@ -5,7 +5,7 @@ use roto_codegen::google::protobuf::compiler::plugin::{
|
||||
CodeGeneratorRequest, CodeGeneratorResponseBuilder, code_generator_response::FileBuilder,
|
||||
};
|
||||
use roto_codegen::google::protobuf::descriptor::FileDescriptorSet;
|
||||
// use roto_runtime::ProtoBuilder;
|
||||
// use roto_codegen::runtime::ProtoBuilder;
|
||||
use std::io::{self, Read, Write};
|
||||
|
||||
fn main() {
|
||||
@@ -58,7 +58,7 @@ fn handle_request(
|
||||
// Write length as varint
|
||||
let len = file_data.len() as u64;
|
||||
let mut len_buf = [0u8; 10];
|
||||
let len_size = roto_runtime::write_varint(len, &mut len_buf).map_err(|e| {
|
||||
let len_size = roto_codegen::runtime::write_varint(len, &mut len_buf).map_err(|e| {
|
||||
error!("Failed to write varint length: {:?}", e);
|
||||
e
|
||||
})?;
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::google::protobuf::descriptor::{DescriptorProto, EnumDescriptorProto,
|
||||
use crate::google::protobuf::descriptor::FileDescriptorSet;
|
||||
use crate::generator::types::map_type_to_rust_builder;
|
||||
|
||||
use roto_runtime::ProtoAccessor;
|
||||
use crate::runtime::ProtoAccessor;
|
||||
use crate::generator::utils::{to_pascal_case, to_snake_case};
|
||||
use crate::generator::types::map_type_to_rust_accessor;
|
||||
|
||||
@@ -25,7 +25,7 @@ pub fn write_enum(enum_proto: &EnumDescriptorProto, output: &mut String) {
|
||||
let name = std::str::from_utf8(name_bytes).expect("Enum value name invalid utf8");
|
||||
let (num_bytes, _) = accessor.get_value(2).expect("Enum value number missing");
|
||||
let (num, _) =
|
||||
roto_runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||
crate::runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||
|
||||
let pascal_name = to_pascal_case(name);
|
||||
if num == 0 {
|
||||
@@ -55,7 +55,7 @@ pub fn write_enum(enum_proto: &EnumDescriptorProto, output: &mut String) {
|
||||
let name = std::str::from_utf8(name_bytes).expect("Enum value name invalid utf8");
|
||||
let (num_bytes, _) = accessor.get_value(2).expect("Enum value number missing");
|
||||
let (num, _) =
|
||||
roto_runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||
crate::runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||
|
||||
output.push_str(&format!(
|
||||
" {} => {}::{},\n",
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::google::protobuf::descriptor::{
|
||||
FileDescriptorSet, MessageOptions, MethodDescriptorProto, OneofDescriptorProto,
|
||||
ServiceDescriptorProto,
|
||||
};
|
||||
use roto_runtime::ProtoAccessor;
|
||||
use crate::runtime::ProtoAccessor;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::str;
|
||||
|
||||
@@ -139,7 +139,7 @@ fn write_enum(enum_proto: &EnumDescriptorProto, output: &mut String) {
|
||||
let name = std::str::from_utf8(name_bytes).expect("Enum value name invalid utf8");
|
||||
let (num_bytes, _) = accessor.get_value(2).expect("Enum value number missing");
|
||||
let (num, _) =
|
||||
roto_runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||
crate::runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||
|
||||
let pascal_name = to_pascal_case(name);
|
||||
if num == 0 {
|
||||
@@ -169,7 +169,7 @@ fn write_enum(enum_proto: &EnumDescriptorProto, output: &mut String) {
|
||||
let name = std::str::from_utf8(name_bytes).expect("Enum value name invalid utf8");
|
||||
let (num_bytes, _) = accessor.get_value(2).expect("Enum value number missing");
|
||||
let (num, _) =
|
||||
roto_runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||
crate::runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||
|
||||
output.push_str(&format!(
|
||||
" {} => {}::{},\n",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// @generated by protoc-gen-roto — do not edit
|
||||
#[allow(unused_imports)]
|
||||
|
||||
use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator};
|
||||
use crate::runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator};
|
||||
use std::str;
|
||||
use bytes::{Bytes, BytesMut, Buf, BufMut};
|
||||
use tonic::{Request, Response, Status};
|
||||
@@ -20,7 +20,7 @@ use roto_tonic::{BufferPool, StatusBody};
|
||||
use crate::google::protobuf::descriptor;
|
||||
|
||||
pub struct Version<'a> {
|
||||
accessor: roto_runtime::ProtoAccessor<'a>,
|
||||
accessor: crate::runtime::ProtoAccessor<'a>,
|
||||
major_offset: Option<usize>,
|
||||
minor_offset: Option<usize>,
|
||||
patch_offset: Option<usize>,
|
||||
@@ -28,8 +28,8 @@ pub struct Version<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Version<'a> {
|
||||
pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
|
||||
let accessor = roto_runtime::ProtoAccessor::new(data)?;
|
||||
pub fn new(data: &'a [u8]) -> crate::runtime::Result<Self> {
|
||||
let accessor = crate::runtime::ProtoAccessor::new(data)?;
|
||||
let mut major_offset = None;
|
||||
let mut minor_offset = None;
|
||||
let mut patch_offset = None;
|
||||
@@ -51,62 +51,62 @@ suffix_offset,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn major(&self) -> roto_runtime::Result<i32> {
|
||||
let offset = self.major_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn major(&self) -> crate::runtime::Result<i32> {
|
||||
let offset = self.major_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
crate::runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn major_or_default(&self) -> roto_runtime::Result<i32> {
|
||||
pub fn major_or_default(&self) -> crate::runtime::Result<i32> {
|
||||
self.major().or(Ok(0))
|
||||
}
|
||||
|
||||
pub fn has_major(&self) -> bool { self.major_offset.is_some() }
|
||||
|
||||
pub fn minor(&self) -> roto_runtime::Result<i32> {
|
||||
let offset = self.minor_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn minor(&self) -> crate::runtime::Result<i32> {
|
||||
let offset = self.minor_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
crate::runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn minor_or_default(&self) -> roto_runtime::Result<i32> {
|
||||
pub fn minor_or_default(&self) -> crate::runtime::Result<i32> {
|
||||
self.minor().or(Ok(0))
|
||||
}
|
||||
|
||||
pub fn has_minor(&self) -> bool { self.minor_offset.is_some() }
|
||||
|
||||
pub fn patch(&self) -> roto_runtime::Result<i32> {
|
||||
let offset = self.patch_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn patch(&self) -> crate::runtime::Result<i32> {
|
||||
let offset = self.patch_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
crate::runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn patch_or_default(&self) -> roto_runtime::Result<i32> {
|
||||
pub fn patch_or_default(&self) -> crate::runtime::Result<i32> {
|
||||
self.patch().or(Ok(0))
|
||||
}
|
||||
|
||||
pub fn has_patch(&self) -> bool { self.patch_offset.is_some() }
|
||||
|
||||
pub fn suffix(&self) -> roto_runtime::Result<&'a str> {
|
||||
let offset = self.suffix_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn suffix(&self) -> crate::runtime::Result<&'a str> {
|
||||
let offset = self.suffix_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
str::from_utf8(bytes).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn suffix_or_default(&self) -> roto_runtime::Result<&'a str> {
|
||||
pub fn suffix_or_default(&self) -> crate::runtime::Result<&'a str> {
|
||||
self.suffix().or(Ok(""))
|
||||
}
|
||||
|
||||
pub fn has_suffix(&self) -> bool { self.suffix_offset.is_some() }
|
||||
|
||||
pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
|
||||
pub fn raw_fields(&self) -> crate::runtime::RawFieldIterator<'a> {
|
||||
self.accessor.raw_fields()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct VersionBuilder<'b> {
|
||||
builder: roto_runtime::ProtoBuilder<'b>,
|
||||
builder: crate::runtime::ProtoBuilder<'b>,
|
||||
major_written: bool,
|
||||
minor_written: bool,
|
||||
patch_written: bool,
|
||||
@@ -116,7 +116,7 @@ pub struct VersionBuilder<'b> {
|
||||
impl<'b> VersionBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> VersionBuilder<'_> {
|
||||
VersionBuilder {
|
||||
builder: roto_runtime::ProtoBuilder::new(buf),
|
||||
builder: crate::runtime::ProtoBuilder::new(buf),
|
||||
major_written: false,
|
||||
minor_written: false,
|
||||
patch_written: false,
|
||||
@@ -124,31 +124,31 @@ impl<'b> VersionBuilder<'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn major(mut self, value: i32) -> roto_runtime::Result<Self> {
|
||||
pub fn major(mut self, value: i32) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_int32(1, value)?;
|
||||
self.major_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn minor(mut self, value: i32) -> roto_runtime::Result<Self> {
|
||||
pub fn minor(mut self, value: i32) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_int32(2, value)?;
|
||||
self.minor_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn patch(mut self, value: i32) -> roto_runtime::Result<Self> {
|
||||
pub fn patch(mut self, value: i32) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_int32(3, value)?;
|
||||
self.patch_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn suffix(mut self, value: &str) -> roto_runtime::Result<Self> {
|
||||
pub fn suffix(mut self, value: &str) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_string(4, value)?;
|
||||
self.suffix_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &Version<'_>) -> roto_runtime::Result<Self> {
|
||||
pub fn with(mut self, msg: &Version<'_>) -> crate::runtime::Result<Self> {
|
||||
for item in msg.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
@@ -165,7 +165,7 @@ impl<'b> VersionBuilder<'b> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
|
||||
pub fn finish(self) -> crate::runtime::Result<&'b mut [u8]> {
|
||||
self.builder.finish()
|
||||
}
|
||||
}
|
||||
@@ -174,15 +174,15 @@ pub struct OwnedVersion {
|
||||
pub data: bytes::Bytes,
|
||||
}
|
||||
|
||||
impl roto_runtime::RotoOwned for OwnedVersion {
|
||||
impl crate::runtime::RotoOwned for OwnedVersion {
|
||||
type Reader<'a> = Version<'a>;
|
||||
fn reader(&self) -> Version<'_> {
|
||||
Version::new(&self.data).expect("failed to create reader")
|
||||
}
|
||||
}
|
||||
|
||||
impl roto_runtime::RotoMessage for OwnedVersion {
|
||||
fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
|
||||
impl crate::runtime::RotoMessage for OwnedVersion {
|
||||
fn decode(buf: bytes::Bytes) -> crate::runtime::Result<Self> {
|
||||
Ok(OwnedVersion { data: buf })
|
||||
}
|
||||
|
||||
@@ -192,7 +192,7 @@ impl roto_runtime::RotoMessage for OwnedVersion {
|
||||
}
|
||||
|
||||
pub struct CodeGeneratorRequest<'a> {
|
||||
accessor: roto_runtime::ProtoAccessor<'a>,
|
||||
accessor: crate::runtime::ProtoAccessor<'a>,
|
||||
file_to_generate_start: Option<usize>,
|
||||
file_to_generate_end: Option<usize>,
|
||||
parameter_offset: Option<usize>,
|
||||
@@ -204,8 +204,8 @@ pub struct CodeGeneratorRequest<'a> {
|
||||
}
|
||||
|
||||
impl<'a> CodeGeneratorRequest<'a> {
|
||||
pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
|
||||
let accessor = roto_runtime::ProtoAccessor::new(data)?;
|
||||
pub fn new(data: &'a [u8]) -> crate::runtime::Result<Self> {
|
||||
let accessor = crate::runtime::ProtoAccessor::new(data)?;
|
||||
let mut file_to_generate_start = None;
|
||||
let mut file_to_generate_end = None;
|
||||
let mut parameter_offset = None;
|
||||
@@ -242,59 +242,59 @@ compiler_version_offset,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn file_to_generate(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
|
||||
pub fn file_to_generate(&self) -> crate::runtime::RepeatedFieldIterator<'a> {
|
||||
match (self.file_to_generate_start, self.file_to_generate_end) {
|
||||
(Some(start), Some(end)) => self.accessor.iter_repeated_range(1, start, end),
|
||||
_ => self.accessor.iter_repeated(1),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parameter(&self) -> roto_runtime::Result<&'a str> {
|
||||
let offset = self.parameter_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn parameter(&self) -> crate::runtime::Result<&'a str> {
|
||||
let offset = self.parameter_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
str::from_utf8(bytes).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn parameter_or_default(&self) -> roto_runtime::Result<&'a str> {
|
||||
pub fn parameter_or_default(&self) -> crate::runtime::Result<&'a str> {
|
||||
self.parameter().or(Ok(""))
|
||||
}
|
||||
|
||||
pub fn has_parameter(&self) -> bool { self.parameter_offset.is_some() }
|
||||
|
||||
pub fn proto_file(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
|
||||
pub fn proto_file(&self) -> crate::runtime::RepeatedFieldIterator<'a> {
|
||||
match (self.proto_file_start, self.proto_file_end) {
|
||||
(Some(start), Some(end)) => self.accessor.iter_repeated_range(15, start, end),
|
||||
_ => self.accessor.iter_repeated(15),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn source_file_descriptors(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
|
||||
pub fn source_file_descriptors(&self) -> crate::runtime::RepeatedFieldIterator<'a> {
|
||||
match (self.source_file_descriptors_start, self.source_file_descriptors_end) {
|
||||
(Some(start), Some(end)) => self.accessor.iter_repeated_range(17, start, end),
|
||||
_ => self.accessor.iter_repeated(17),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compiler_version(&self) -> roto_runtime::Result<&'a [u8]> {
|
||||
let offset = self.compiler_version_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn compiler_version(&self) -> crate::runtime::Result<&'a [u8]> {
|
||||
let offset = self.compiler_version_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
pub fn compiler_version_or_default(&self) -> roto_runtime::Result<&'a [u8]> {
|
||||
pub fn compiler_version_or_default(&self) -> crate::runtime::Result<&'a [u8]> {
|
||||
self.compiler_version().or(Ok(&[]))
|
||||
}
|
||||
|
||||
pub fn has_compiler_version(&self) -> bool { self.compiler_version_offset.is_some() }
|
||||
|
||||
pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
|
||||
pub fn raw_fields(&self) -> crate::runtime::RawFieldIterator<'a> {
|
||||
self.accessor.raw_fields()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct CodeGeneratorRequestBuilder<'b> {
|
||||
builder: roto_runtime::ProtoBuilder<'b>,
|
||||
builder: crate::runtime::ProtoBuilder<'b>,
|
||||
file_to_generate_written: bool,
|
||||
parameter_written: bool,
|
||||
proto_file_written: bool,
|
||||
@@ -305,7 +305,7 @@ pub struct CodeGeneratorRequestBuilder<'b> {
|
||||
impl<'b> CodeGeneratorRequestBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> CodeGeneratorRequestBuilder<'_> {
|
||||
CodeGeneratorRequestBuilder {
|
||||
builder: roto_runtime::ProtoBuilder::new(buf),
|
||||
builder: crate::runtime::ProtoBuilder::new(buf),
|
||||
file_to_generate_written: false,
|
||||
parameter_written: false,
|
||||
proto_file_written: false,
|
||||
@@ -314,37 +314,37 @@ impl<'b> CodeGeneratorRequestBuilder<'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_to_generate(mut self, value: &str) -> roto_runtime::Result<Self> {
|
||||
pub fn file_to_generate(mut self, value: &str) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_string(1, value)?;
|
||||
self.file_to_generate_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn parameter(mut self, value: &str) -> roto_runtime::Result<Self> {
|
||||
pub fn parameter(mut self, value: &str) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_string(2, value)?;
|
||||
self.parameter_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn proto_file(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
|
||||
pub fn proto_file(mut self, value: &[u8]) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_bytes(15, value)?;
|
||||
self.proto_file_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn source_file_descriptors(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
|
||||
pub fn source_file_descriptors(mut self, value: &[u8]) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_bytes(17, value)?;
|
||||
self.source_file_descriptors_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn compiler_version(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
|
||||
pub fn compiler_version(mut self, value: &[u8]) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_bytes(3, value)?;
|
||||
self.compiler_version_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &CodeGeneratorRequest<'_>) -> roto_runtime::Result<Self> {
|
||||
pub fn with(mut self, msg: &CodeGeneratorRequest<'_>) -> crate::runtime::Result<Self> {
|
||||
for item in msg.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
@@ -362,7 +362,7 @@ impl<'b> CodeGeneratorRequestBuilder<'b> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
|
||||
pub fn finish(self) -> crate::runtime::Result<&'b mut [u8]> {
|
||||
self.builder.finish()
|
||||
}
|
||||
}
|
||||
@@ -371,15 +371,15 @@ pub struct OwnedCodeGeneratorRequest {
|
||||
pub data: bytes::Bytes,
|
||||
}
|
||||
|
||||
impl roto_runtime::RotoOwned for OwnedCodeGeneratorRequest {
|
||||
impl crate::runtime::RotoOwned for OwnedCodeGeneratorRequest {
|
||||
type Reader<'a> = CodeGeneratorRequest<'a>;
|
||||
fn reader(&self) -> CodeGeneratorRequest<'_> {
|
||||
CodeGeneratorRequest::new(&self.data).expect("failed to create reader")
|
||||
}
|
||||
}
|
||||
|
||||
impl roto_runtime::RotoMessage for OwnedCodeGeneratorRequest {
|
||||
fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
|
||||
impl crate::runtime::RotoMessage for OwnedCodeGeneratorRequest {
|
||||
fn decode(buf: bytes::Bytes) -> crate::runtime::Result<Self> {
|
||||
Ok(OwnedCodeGeneratorRequest { data: buf })
|
||||
}
|
||||
|
||||
@@ -389,7 +389,7 @@ impl roto_runtime::RotoMessage for OwnedCodeGeneratorRequest {
|
||||
}
|
||||
|
||||
pub struct CodeGeneratorResponse<'a> {
|
||||
accessor: roto_runtime::ProtoAccessor<'a>,
|
||||
accessor: crate::runtime::ProtoAccessor<'a>,
|
||||
error_offset: Option<usize>,
|
||||
supported_features_offset: Option<usize>,
|
||||
minimum_edition_offset: Option<usize>,
|
||||
@@ -399,8 +399,8 @@ pub struct CodeGeneratorResponse<'a> {
|
||||
}
|
||||
|
||||
impl<'a> CodeGeneratorResponse<'a> {
|
||||
pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
|
||||
let accessor = roto_runtime::ProtoAccessor::new(data)?;
|
||||
pub fn new(data: &'a [u8]) -> crate::runtime::Result<Self> {
|
||||
let accessor = crate::runtime::ProtoAccessor::new(data)?;
|
||||
let mut error_offset = None;
|
||||
let mut supported_features_offset = None;
|
||||
let mut minimum_edition_offset = None;
|
||||
@@ -429,69 +429,69 @@ file_start, file_end,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn error(&self) -> roto_runtime::Result<&'a str> {
|
||||
let offset = self.error_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn error(&self) -> crate::runtime::Result<&'a str> {
|
||||
let offset = self.error_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
str::from_utf8(bytes).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn error_or_default(&self) -> roto_runtime::Result<&'a str> {
|
||||
pub fn error_or_default(&self) -> crate::runtime::Result<&'a str> {
|
||||
self.error().or(Ok(""))
|
||||
}
|
||||
|
||||
pub fn has_error(&self) -> bool { self.error_offset.is_some() }
|
||||
|
||||
pub fn supported_features(&self) -> roto_runtime::Result<u32> {
|
||||
let offset = self.supported_features_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn supported_features(&self) -> crate::runtime::Result<u32> {
|
||||
let offset = self.supported_features_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
roto_runtime::read_varint(bytes).map(|(v, _)| v as u32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
crate::runtime::read_varint(bytes).map(|(v, _)| v as u32).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn supported_features_or_default(&self) -> roto_runtime::Result<u32> {
|
||||
pub fn supported_features_or_default(&self) -> crate::runtime::Result<u32> {
|
||||
self.supported_features().or(Ok(0))
|
||||
}
|
||||
|
||||
pub fn has_supported_features(&self) -> bool { self.supported_features_offset.is_some() }
|
||||
|
||||
pub fn minimum_edition(&self) -> roto_runtime::Result<i32> {
|
||||
let offset = self.minimum_edition_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn minimum_edition(&self) -> crate::runtime::Result<i32> {
|
||||
let offset = self.minimum_edition_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
crate::runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn minimum_edition_or_default(&self) -> roto_runtime::Result<i32> {
|
||||
pub fn minimum_edition_or_default(&self) -> crate::runtime::Result<i32> {
|
||||
self.minimum_edition().or(Ok(0))
|
||||
}
|
||||
|
||||
pub fn has_minimum_edition(&self) -> bool { self.minimum_edition_offset.is_some() }
|
||||
|
||||
pub fn maximum_edition(&self) -> roto_runtime::Result<i32> {
|
||||
let offset = self.maximum_edition_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn maximum_edition(&self) -> crate::runtime::Result<i32> {
|
||||
let offset = self.maximum_edition_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
crate::runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn maximum_edition_or_default(&self) -> roto_runtime::Result<i32> {
|
||||
pub fn maximum_edition_or_default(&self) -> crate::runtime::Result<i32> {
|
||||
self.maximum_edition().or(Ok(0))
|
||||
}
|
||||
|
||||
pub fn has_maximum_edition(&self) -> bool { self.maximum_edition_offset.is_some() }
|
||||
|
||||
pub fn file(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
|
||||
pub fn file(&self) -> crate::runtime::RepeatedFieldIterator<'a> {
|
||||
match (self.file_start, self.file_end) {
|
||||
(Some(start), Some(end)) => self.accessor.iter_repeated_range(15, start, end),
|
||||
_ => self.accessor.iter_repeated(15),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
|
||||
pub fn raw_fields(&self) -> crate::runtime::RawFieldIterator<'a> {
|
||||
self.accessor.raw_fields()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct CodeGeneratorResponseBuilder<'b> {
|
||||
builder: roto_runtime::ProtoBuilder<'b>,
|
||||
builder: crate::runtime::ProtoBuilder<'b>,
|
||||
error_written: bool,
|
||||
supported_features_written: bool,
|
||||
minimum_edition_written: bool,
|
||||
@@ -502,7 +502,7 @@ pub struct CodeGeneratorResponseBuilder<'b> {
|
||||
impl<'b> CodeGeneratorResponseBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> CodeGeneratorResponseBuilder<'_> {
|
||||
CodeGeneratorResponseBuilder {
|
||||
builder: roto_runtime::ProtoBuilder::new(buf),
|
||||
builder: crate::runtime::ProtoBuilder::new(buf),
|
||||
error_written: false,
|
||||
supported_features_written: false,
|
||||
minimum_edition_written: false,
|
||||
@@ -511,37 +511,37 @@ impl<'b> CodeGeneratorResponseBuilder<'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error(mut self, value: &str) -> roto_runtime::Result<Self> {
|
||||
pub fn error(mut self, value: &str) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_string(1, value)?;
|
||||
self.error_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn supported_features(mut self, value: u64) -> roto_runtime::Result<Self> {
|
||||
pub fn supported_features(mut self, value: u64) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_varint(2, value)?;
|
||||
self.supported_features_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn minimum_edition(mut self, value: i32) -> roto_runtime::Result<Self> {
|
||||
pub fn minimum_edition(mut self, value: i32) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_int32(3, value)?;
|
||||
self.minimum_edition_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn maximum_edition(mut self, value: i32) -> roto_runtime::Result<Self> {
|
||||
pub fn maximum_edition(mut self, value: i32) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_int32(4, value)?;
|
||||
self.maximum_edition_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn file(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
|
||||
pub fn file(mut self, value: &[u8]) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_bytes(15, value)?;
|
||||
self.file_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &CodeGeneratorResponse<'_>) -> roto_runtime::Result<Self> {
|
||||
pub fn with(mut self, msg: &CodeGeneratorResponse<'_>) -> crate::runtime::Result<Self> {
|
||||
for item in msg.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
@@ -559,7 +559,7 @@ impl<'b> CodeGeneratorResponseBuilder<'b> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
|
||||
pub fn finish(self) -> crate::runtime::Result<&'b mut [u8]> {
|
||||
self.builder.finish()
|
||||
}
|
||||
}
|
||||
@@ -568,15 +568,15 @@ pub struct OwnedCodeGeneratorResponse {
|
||||
pub data: bytes::Bytes,
|
||||
}
|
||||
|
||||
impl roto_runtime::RotoOwned for OwnedCodeGeneratorResponse {
|
||||
impl crate::runtime::RotoOwned for OwnedCodeGeneratorResponse {
|
||||
type Reader<'a> = CodeGeneratorResponse<'a>;
|
||||
fn reader(&self) -> CodeGeneratorResponse<'_> {
|
||||
CodeGeneratorResponse::new(&self.data).expect("failed to create reader")
|
||||
}
|
||||
}
|
||||
|
||||
impl roto_runtime::RotoMessage for OwnedCodeGeneratorResponse {
|
||||
fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
|
||||
impl crate::runtime::RotoMessage for OwnedCodeGeneratorResponse {
|
||||
fn decode(buf: bytes::Bytes) -> crate::runtime::Result<Self> {
|
||||
Ok(OwnedCodeGeneratorResponse { data: buf })
|
||||
}
|
||||
|
||||
@@ -606,7 +606,7 @@ impl Feature {
|
||||
}
|
||||
|
||||
pub struct File<'a> {
|
||||
accessor: roto_runtime::ProtoAccessor<'a>,
|
||||
accessor: crate::runtime::ProtoAccessor<'a>,
|
||||
name_offset: Option<usize>,
|
||||
insertion_point_offset: Option<usize>,
|
||||
content_offset: Option<usize>,
|
||||
@@ -614,8 +614,8 @@ pub struct File<'a> {
|
||||
}
|
||||
|
||||
impl<'a> File<'a> {
|
||||
pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
|
||||
let accessor = roto_runtime::ProtoAccessor::new(data)?;
|
||||
pub fn new(data: &'a [u8]) -> crate::runtime::Result<Self> {
|
||||
let accessor = crate::runtime::ProtoAccessor::new(data)?;
|
||||
let mut name_offset = None;
|
||||
let mut insertion_point_offset = None;
|
||||
let mut content_offset = None;
|
||||
@@ -637,62 +637,62 @@ generated_code_info_offset,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn name(&self) -> roto_runtime::Result<&'a str> {
|
||||
let offset = self.name_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn name(&self) -> crate::runtime::Result<&'a str> {
|
||||
let offset = self.name_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
str::from_utf8(bytes).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn name_or_default(&self) -> roto_runtime::Result<&'a str> {
|
||||
pub fn name_or_default(&self) -> crate::runtime::Result<&'a str> {
|
||||
self.name().or(Ok(""))
|
||||
}
|
||||
|
||||
pub fn has_name(&self) -> bool { self.name_offset.is_some() }
|
||||
|
||||
pub fn insertion_point(&self) -> roto_runtime::Result<&'a str> {
|
||||
let offset = self.insertion_point_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn insertion_point(&self) -> crate::runtime::Result<&'a str> {
|
||||
let offset = self.insertion_point_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
str::from_utf8(bytes).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn insertion_point_or_default(&self) -> roto_runtime::Result<&'a str> {
|
||||
pub fn insertion_point_or_default(&self) -> crate::runtime::Result<&'a str> {
|
||||
self.insertion_point().or(Ok(""))
|
||||
}
|
||||
|
||||
pub fn has_insertion_point(&self) -> bool { self.insertion_point_offset.is_some() }
|
||||
|
||||
pub fn content(&self) -> roto_runtime::Result<&'a str> {
|
||||
let offset = self.content_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn content(&self) -> crate::runtime::Result<&'a str> {
|
||||
let offset = self.content_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
|
||||
str::from_utf8(bytes).map_err(|_| crate::runtime::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn content_or_default(&self) -> roto_runtime::Result<&'a str> {
|
||||
pub fn content_or_default(&self) -> crate::runtime::Result<&'a str> {
|
||||
self.content().or(Ok(""))
|
||||
}
|
||||
|
||||
pub fn has_content(&self) -> bool { self.content_offset.is_some() }
|
||||
|
||||
pub fn generated_code_info(&self) -> roto_runtime::Result<&'a [u8]> {
|
||||
let offset = self.generated_code_info_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
|
||||
pub fn generated_code_info(&self) -> crate::runtime::Result<&'a [u8]> {
|
||||
let offset = self.generated_code_info_offset.ok_or(crate::runtime::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
pub fn generated_code_info_or_default(&self) -> roto_runtime::Result<&'a [u8]> {
|
||||
pub fn generated_code_info_or_default(&self) -> crate::runtime::Result<&'a [u8]> {
|
||||
self.generated_code_info().or(Ok(&[]))
|
||||
}
|
||||
|
||||
pub fn has_generated_code_info(&self) -> bool { self.generated_code_info_offset.is_some() }
|
||||
|
||||
pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
|
||||
pub fn raw_fields(&self) -> crate::runtime::RawFieldIterator<'a> {
|
||||
self.accessor.raw_fields()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct FileBuilder<'b> {
|
||||
builder: roto_runtime::ProtoBuilder<'b>,
|
||||
builder: crate::runtime::ProtoBuilder<'b>,
|
||||
name_written: bool,
|
||||
insertion_point_written: bool,
|
||||
content_written: bool,
|
||||
@@ -702,7 +702,7 @@ pub struct FileBuilder<'b> {
|
||||
impl<'b> FileBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> FileBuilder<'_> {
|
||||
FileBuilder {
|
||||
builder: roto_runtime::ProtoBuilder::new(buf),
|
||||
builder: crate::runtime::ProtoBuilder::new(buf),
|
||||
name_written: false,
|
||||
insertion_point_written: false,
|
||||
content_written: false,
|
||||
@@ -710,31 +710,31 @@ impl<'b> FileBuilder<'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(mut self, value: &str) -> roto_runtime::Result<Self> {
|
||||
pub fn name(mut self, value: &str) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_string(1, value)?;
|
||||
self.name_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn insertion_point(mut self, value: &str) -> roto_runtime::Result<Self> {
|
||||
pub fn insertion_point(mut self, value: &str) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_string(2, value)?;
|
||||
self.insertion_point_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn content(mut self, value: &str) -> roto_runtime::Result<Self> {
|
||||
pub fn content(mut self, value: &str) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_string(15, value)?;
|
||||
self.content_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn generated_code_info(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
|
||||
pub fn generated_code_info(mut self, value: &[u8]) -> crate::runtime::Result<Self> {
|
||||
self.builder.write_bytes(16, value)?;
|
||||
self.generated_code_info_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &File<'_>) -> roto_runtime::Result<Self> {
|
||||
pub fn with(mut self, msg: &File<'_>) -> crate::runtime::Result<Self> {
|
||||
for item in msg.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
@@ -751,7 +751,7 @@ impl<'b> FileBuilder<'b> {
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
|
||||
pub fn finish(self) -> crate::runtime::Result<&'b mut [u8]> {
|
||||
self.builder.finish()
|
||||
}
|
||||
}
|
||||
@@ -760,15 +760,15 @@ pub struct OwnedFile {
|
||||
pub data: bytes::Bytes,
|
||||
}
|
||||
|
||||
impl roto_runtime::RotoOwned for OwnedFile {
|
||||
impl crate::runtime::RotoOwned for OwnedFile {
|
||||
type Reader<'a> = File<'a>;
|
||||
fn reader(&self) -> File<'_> {
|
||||
File::new(&self.data).expect("failed to create reader")
|
||||
}
|
||||
}
|
||||
|
||||
impl roto_runtime::RotoMessage for OwnedFile {
|
||||
fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
|
||||
impl crate::runtime::RotoMessage for OwnedFile {
|
||||
fn decode(buf: bytes::Bytes) -> crate::runtime::Result<Self> {
|
||||
Ok(OwnedFile { data: buf })
|
||||
}
|
||||
|
||||
|
||||
+1124
-1124
File diff suppressed because it is too large
Load Diff
@@ -1,2 +1,4 @@
|
||||
pub mod generator;
|
||||
pub mod google;
|
||||
pub mod runtime;
|
||||
|
||||
|
||||
@@ -0,0 +1,921 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
use core::fmt;
|
||||
use bytes::BufMut;
|
||||
|
||||
pub struct MapFieldIterator<'a> {
|
||||
inner: RepeatedFieldIterator<'a>,
|
||||
}
|
||||
|
||||
impl<'a> MapFieldIterator<'a> {
|
||||
pub fn new(inner: RepeatedFieldIterator<'a>) -> Self {
|
||||
Self { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for MapFieldIterator<'a> {
|
||||
type Item = Result<(&'a [u8], &'a [u8])>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self.inner.next() {
|
||||
Some(Ok((value, _wire_type))) => {
|
||||
let accessor = ProtoAccessor::new(value).ok()?;
|
||||
let (key_bytes, _) = accessor.get_value(1).ok()?;
|
||||
let (val_bytes, _) = accessor.get_value(2).ok()?;
|
||||
Some(Ok((key_bytes, val_bytes)))
|
||||
}
|
||||
Some(Err(e)) => Some(Err(e)),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum RotoError {
|
||||
UnexpectedEndOfBuffer,
|
||||
InvalidVarint,
|
||||
InvalidWireType(u8),
|
||||
BufferOverflow,
|
||||
FieldNotFound,
|
||||
WireFormatViolation,
|
||||
}
|
||||
|
||||
impl fmt::Display for RotoError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
RotoError::UnexpectedEndOfBuffer => write!(f, "Unexpected end of buffer"),
|
||||
RotoError::InvalidVarint => write!(f, "Invalid varint encoding"),
|
||||
RotoError::InvalidWireType(t) => write!(f, "Invalid wire type: {t}"),
|
||||
RotoError::BufferOverflow => write!(f, "Buffer overflow during write"),
|
||||
RotoError::FieldNotFound => write!(f, "Requested field not found in message"),
|
||||
RotoError::WireFormatViolation => write!(f, "Wire format violation"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for RotoError {}
|
||||
|
||||
pub type Result<T> = core::result::Result<T, RotoError>;
|
||||
|
||||
pub trait RotoOwned {
|
||||
type Reader<'a> where Self: 'a;
|
||||
fn reader(&self) -> Self::Reader<'_>;
|
||||
}
|
||||
|
||||
pub trait RotoMessage: Sized {
|
||||
fn decode(buf: bytes::Bytes) -> Result<Self>;
|
||||
fn bytes(&self) -> bytes::Bytes;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum WireType {
|
||||
Varint = 0,
|
||||
Fixed64 = 1,
|
||||
LengthDelimited = 2,
|
||||
StartGroup = 3, // Deprecated
|
||||
EndGroup = 4, // Deprecated
|
||||
Fixed32 = 5,
|
||||
}
|
||||
|
||||
impl WireType {
|
||||
pub fn from_u8(value: u8) -> Result<Self> {
|
||||
match value {
|
||||
0 => Ok(WireType::Varint),
|
||||
1 => Ok(WireType::Fixed64),
|
||||
2 => Ok(WireType::LengthDelimited),
|
||||
3 => Ok(WireType::StartGroup),
|
||||
4 => Ok(WireType::EndGroup),
|
||||
5 => Ok(WireType::Fixed32),
|
||||
_ => Err(RotoError::InvalidWireType(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Tag {
|
||||
pub field_number: u32,
|
||||
pub wire_type: WireType,
|
||||
}
|
||||
|
||||
impl Tag {
|
||||
/// Decodes a tag from the buffer, returning the tag and the number of bytes read.
|
||||
pub fn decode(data: &[u8]) -> Result<(Self, usize)> {
|
||||
let (val, len) = read_varint(data)?;
|
||||
let wire_type_raw = (val & 0x7) as u8;
|
||||
let field_number = (val >> 3) as u32;
|
||||
|
||||
Ok((
|
||||
Tag {
|
||||
field_number,
|
||||
wire_type: WireType::from_u8(wire_type_raw)?,
|
||||
},
|
||||
len,
|
||||
))
|
||||
}
|
||||
|
||||
/// Encodes a tag into the provided buffer.
|
||||
pub fn encode(field_number: u32, wire_type: WireType, buf: &mut [u8]) -> Result<usize> {
|
||||
let val = ((field_number as u64) << 3) | (wire_type as u64);
|
||||
write_varint(val, buf)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads a varint from the start of the buffer.
|
||||
pub fn read_varint(data: &[u8]) -> Result<(u64, usize)> {
|
||||
let mut result = 0u64;
|
||||
let mut shift = 0;
|
||||
let mut bytes_read = 0;
|
||||
|
||||
for &byte in data {
|
||||
bytes_read += 1;
|
||||
if bytes_read > 10 {
|
||||
return Err(RotoError::InvalidVarint);
|
||||
}
|
||||
|
||||
let value = (byte & 0x7F) as u64;
|
||||
if shift >= 64 {
|
||||
return Err(RotoError::InvalidVarint);
|
||||
}
|
||||
result |= value << shift;
|
||||
shift += 7;
|
||||
|
||||
if (byte & 0x80) == 0 {
|
||||
return Ok((result, bytes_read));
|
||||
}
|
||||
}
|
||||
|
||||
Err(RotoError::UnexpectedEndOfBuffer)
|
||||
}
|
||||
|
||||
/// Writes a varint into the buffer.
|
||||
pub fn write_varint(mut value: u64, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut bytes_written = 0;
|
||||
while value >= 0x80 {
|
||||
if bytes_written >= buf.len() {
|
||||
return Err(RotoError::BufferOverflow);
|
||||
}
|
||||
buf[bytes_written] = (value as u8 & 0x7F) | 0x80;
|
||||
value >>= 7;
|
||||
bytes_written += 1;
|
||||
}
|
||||
|
||||
if bytes_written >= buf.len() {
|
||||
return Err(RotoError::BufferOverflow);
|
||||
}
|
||||
buf[bytes_written] = value as u8;
|
||||
bytes_written += 1;
|
||||
Ok(bytes_written)
|
||||
}
|
||||
|
||||
/// Returns the number of bytes that should be skipped for a given wire type and the current data slice.
|
||||
pub fn skip_value(wire_type: WireType, data: &[u8]) -> Result<usize> {
|
||||
match wire_type {
|
||||
WireType::Varint => {
|
||||
let (_, len) = read_varint(data)?;
|
||||
Ok(len)
|
||||
}
|
||||
WireType::Fixed64 => {
|
||||
if data.len() < 8 {
|
||||
return Err(RotoError::UnexpectedEndOfBuffer);
|
||||
}
|
||||
Ok(8)
|
||||
}
|
||||
WireType::LengthDelimited => {
|
||||
let (len, varint_len) = read_varint(data)?;
|
||||
let total_len = varint_len + len as usize;
|
||||
if data.len() < total_len {
|
||||
return Err(RotoError::UnexpectedEndOfBuffer);
|
||||
}
|
||||
Ok(total_len)
|
||||
}
|
||||
WireType::Fixed32 => {
|
||||
if data.len() < 4 {
|
||||
return Err(RotoError::UnexpectedEndOfBuffer);
|
||||
}
|
||||
Ok(4)
|
||||
}
|
||||
WireType::StartGroup | WireType::EndGroup => {
|
||||
// These are deprecated and not fully supported in this runtime.
|
||||
Err(RotoError::WireFormatViolation)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ProtoAccessor<'a> {
|
||||
data: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> ProtoAccessor<'a> {
|
||||
pub fn new(data: &'a [u8]) -> Result<Self> {
|
||||
Ok(Self { data })
|
||||
}
|
||||
|
||||
/// Returns an iterator over all fields in the message.
|
||||
pub fn fields(&self) -> FieldIterator<'a> {
|
||||
FieldIterator {
|
||||
data: self.data,
|
||||
cursor: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the value and wire type of the last occurrence of the specified field.
|
||||
pub fn get_value(&self, field_number: u32) -> Result<(&'a [u8], WireType)> {
|
||||
let mut last_value = None;
|
||||
for item in self.fields() {
|
||||
let (_offset, tag, value) = item?;
|
||||
if tag.field_number == field_number {
|
||||
last_value = Some((value, tag.wire_type));
|
||||
}
|
||||
}
|
||||
last_value.ok_or(RotoError::FieldNotFound)
|
||||
}
|
||||
|
||||
/// Returns an iterator that scans the entire buffer for all occurrences of the specified field.
|
||||
pub fn iter_repeated(&self, field_number: u32) -> RepeatedFieldIterator<'a> {
|
||||
RepeatedFieldIterator::new(self.data, field_number)
|
||||
}
|
||||
|
||||
/// Returns the value and wire type of a field at a specific offset.
|
||||
pub fn get_value_at(&self, offset: usize) -> Result<(&'a [u8], WireType)> {
|
||||
if offset >= self.data.len() {
|
||||
return Err(RotoError::UnexpectedEndOfBuffer);
|
||||
}
|
||||
let (tag, tag_len) = Tag::decode(&self.data[offset..])?;
|
||||
let cursor_after_tag = offset + tag_len;
|
||||
if cursor_after_tag > self.data.len() {
|
||||
return Err(RotoError::UnexpectedEndOfBuffer);
|
||||
}
|
||||
let value_len = skip_value(tag.wire_type, &self.data[cursor_after_tag..])?;
|
||||
let (value_offset, actual_value_len) = match tag.wire_type {
|
||||
WireType::LengthDelimited => {
|
||||
let (_, varint_len) = read_varint(&self.data[cursor_after_tag..])?;
|
||||
(cursor_after_tag + varint_len, value_len - varint_len)
|
||||
}
|
||||
_ => (cursor_after_tag, value_len),
|
||||
};
|
||||
Ok((
|
||||
&self.data[value_offset..value_offset + actual_value_len],
|
||||
tag.wire_type,
|
||||
))
|
||||
}
|
||||
|
||||
/// Returns an iterator that scans a specific range of the buffer for all occurrences of the specified field.
|
||||
pub fn iter_repeated_range(
|
||||
&self,
|
||||
field_number: u32,
|
||||
start: usize,
|
||||
end: usize,
|
||||
) -> RepeatedFieldIterator<'a> {
|
||||
RepeatedFieldIterator::new_range(self.data, field_number, start, end)
|
||||
}
|
||||
|
||||
/// Returns an iterator that yields `(field_number, raw_bytes)` for every
|
||||
/// field in the message. `raw_bytes` is the complete on-wire encoding
|
||||
/// (tag + value, including any length prefix), suitable for passing
|
||||
/// directly to `ProtoBuilder::write_raw`.
|
||||
pub fn raw_fields(&self) -> RawFieldIterator<'a> {
|
||||
RawFieldIterator {
|
||||
data: self.data,
|
||||
cursor: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FieldIterator<'a> {
|
||||
data: &'a [u8],
|
||||
cursor: usize,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for FieldIterator<'a> {
|
||||
type Item = Result<(usize, Tag, &'a [u8])>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.cursor >= self.data.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (tag, tag_len) = match Tag::decode(&self.data[self.cursor..]) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
self.cursor = self.data.len();
|
||||
return Some(Err(e));
|
||||
}
|
||||
};
|
||||
|
||||
let cursor_after_tag = self.cursor + tag_len;
|
||||
if cursor_after_tag > self.data.len() {
|
||||
self.cursor = self.data.len();
|
||||
return Some(Err(RotoError::UnexpectedEndOfBuffer));
|
||||
}
|
||||
|
||||
let value_len = match skip_value(tag.wire_type, &self.data[cursor_after_tag..]) {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
self.cursor = self.data.len();
|
||||
return Some(Err(e));
|
||||
}
|
||||
};
|
||||
|
||||
let (value_offset, actual_value_len) = match tag.wire_type {
|
||||
WireType::LengthDelimited => {
|
||||
let (_, varint_len) = match read_varint(&self.data[cursor_after_tag..]) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
self.cursor = self.data.len();
|
||||
return Some(Err(e));
|
||||
}
|
||||
};
|
||||
(cursor_after_tag + varint_len, value_len - varint_len)
|
||||
}
|
||||
_ => (cursor_after_tag, value_len),
|
||||
};
|
||||
|
||||
self.cursor = cursor_after_tag + value_len;
|
||||
|
||||
Some(Ok((
|
||||
self.cursor - tag_len - value_len,
|
||||
tag,
|
||||
&self.data[value_offset..value_offset + actual_value_len],
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RepeatedFieldIterator<'a> {
|
||||
iterator: FieldIterator<'a>,
|
||||
field_number: u32,
|
||||
end_offset: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a> RepeatedFieldIterator<'a> {
|
||||
pub fn new(data: &'a [u8], field_number: u32) -> Self {
|
||||
Self {
|
||||
iterator: FieldIterator { data, cursor: 0 },
|
||||
field_number,
|
||||
end_offset: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_range(data: &'a [u8], field_number: u32, start: usize, end: usize) -> Self {
|
||||
Self {
|
||||
iterator: FieldIterator {
|
||||
data,
|
||||
cursor: start,
|
||||
},
|
||||
field_number,
|
||||
end_offset: Some(end),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for RepeatedFieldIterator<'a> {
|
||||
type Item = Result<(&'a [u8], WireType)>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
while let Some(item) = self.iterator.next() {
|
||||
match item {
|
||||
Ok((offset, tag, value)) if tag.field_number == self.field_number => {
|
||||
if let Some(end) = self.end_offset {
|
||||
if offset > end {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
return Some(Ok((value, tag.wire_type)));
|
||||
}
|
||||
Ok(_) => continue,
|
||||
Err(e) => return Some(Err(e)),
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator that yields `(field_number, raw_bytes)` for every field in a
|
||||
/// protobuf message, where `raw_bytes` is the complete on-wire encoding of the
|
||||
/// field: tag varint + value bytes (including the length prefix for
|
||||
/// length-delimited fields). This is the slice needed by
|
||||
/// `ProtoBuilder::write_raw` to copy a field verbatim.
|
||||
pub struct RawFieldIterator<'a> {
|
||||
data: &'a [u8],
|
||||
cursor: usize,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for RawFieldIterator<'a> {
|
||||
type Item = Result<(u32, &'a [u8])>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.cursor >= self.data.len() {
|
||||
return None;
|
||||
}
|
||||
let field_start = self.cursor;
|
||||
let (tag, tag_len) = match Tag::decode(&self.data[self.cursor..]) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
self.cursor = self.data.len();
|
||||
return Some(Err(e));
|
||||
}
|
||||
};
|
||||
let cursor_after_tag = self.cursor + tag_len;
|
||||
if cursor_after_tag > self.data.len() {
|
||||
self.cursor = self.data.len();
|
||||
return Some(Err(RotoError::UnexpectedEndOfBuffer));
|
||||
}
|
||||
let value_len = match skip_value(tag.wire_type, &self.data[cursor_after_tag..]) {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
self.cursor = self.data.len();
|
||||
return Some(Err(e));
|
||||
}
|
||||
};
|
||||
self.cursor = cursor_after_tag + value_len;
|
||||
Some(Ok((tag.field_number, &self.data[field_start..self.cursor])))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[cfg(feature = "alloc")]
|
||||
use alloc::{vec, vec::{Vec}};
|
||||
|
||||
#[test]
|
||||
fn test_varint_read_write() {
|
||||
let mut buf = [0u8; 10];
|
||||
let val = 300u64;
|
||||
let len = write_varint(val, &mut buf).unwrap();
|
||||
assert_eq!(len, 2);
|
||||
assert_eq!(&buf[..2], &[0xAC, 0x02]);
|
||||
|
||||
let (read_val, read_len) = read_varint(&buf[..2]).unwrap();
|
||||
assert_eq!(read_val, val);
|
||||
assert_eq!(read_len, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tag_decode() {
|
||||
// Field 1, WireType Varint: (1 << 3) | 0 = 8
|
||||
let data = [8u8];
|
||||
let (tag, len) = Tag::decode(&data).unwrap();
|
||||
assert_eq!(tag.field_number, 1);
|
||||
assert_eq!(tag.wire_type, WireType::Varint);
|
||||
assert_eq!(len, 1);
|
||||
|
||||
// Field 15, WireType LengthDelimited: (15 << 3) | 2 = 120 | 2 = 122
|
||||
let data2 = [122u8];
|
||||
let (tag2, len2) = Tag::decode(&data2).unwrap();
|
||||
assert_eq!(tag2.field_number, 15);
|
||||
assert_eq!(tag2.wire_type, WireType::LengthDelimited);
|
||||
assert_eq!(len2, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skip_value() {
|
||||
// Varint: 300 (2 bytes)
|
||||
let data_varint = [0xAC, 0x02];
|
||||
assert_eq!(skip_value(WireType::Varint, &data_varint).unwrap(), 2);
|
||||
|
||||
// Fixed32: 4 bytes
|
||||
let data_fixed32 = [0u8; 4];
|
||||
assert_eq!(skip_value(WireType::Fixed32, &data_fixed32).unwrap(), 4);
|
||||
|
||||
// Length delimited: len=3, data=[1,2,3] (1 byte varint for length + 3 bytes)
|
||||
let data_len = [3, 1, 2, 3];
|
||||
assert_eq!(skip_value(WireType::LengthDelimited, &data_len).unwrap(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_accessor_basic() {
|
||||
// Field 1 (Varint): 150
|
||||
// Tag: (1 << 3) | 0 = 8. Value: 150 = [150, 1]
|
||||
// Field 2 (LengthDelimited): "hi"
|
||||
// Tag: (2 << 3) | 2 = 18. Length: 2. Value: [104, 105]
|
||||
let data = [8, 150, 1, 18, 2, 104, 105];
|
||||
let acc = ProtoAccessor::new(&data).unwrap();
|
||||
|
||||
let (val1, type1) = acc.get_value(1).unwrap();
|
||||
assert_eq!(type1, WireType::Varint);
|
||||
assert_eq!(val1, &[150, 1]);
|
||||
|
||||
let (val2, type2) = acc.get_value(2).unwrap();
|
||||
assert_eq!(type2, WireType::LengthDelimited);
|
||||
assert_eq!(val2, &[104, 105]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_accessor_repeated() {
|
||||
// Field 1: 10, Field 1: 20, Field 1: 30
|
||||
// Tags: 8, 8, 8. Values: 10, 20, 30
|
||||
let data = [8, 10, 8, 20, 8, 30];
|
||||
let acc = ProtoAccessor::new(&data).unwrap();
|
||||
|
||||
// Last value should be 30
|
||||
let (val, _) = acc.get_value(1).unwrap();
|
||||
assert_eq!(val, &[30]);
|
||||
|
||||
// Iteration should find all three
|
||||
let results: Vec<_> = acc.iter_repeated(1).collect();
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results[0].as_ref().unwrap().0, &[10]);
|
||||
assert_eq!(results[1].as_ref().unwrap().0, &[20]);
|
||||
assert_eq!(results[2].as_ref().unwrap().0, &[30]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_builder_basic() {
|
||||
let mut buf = [0u8; 1024];
|
||||
let mut builder = ProtoBuilder::new(&mut buf);
|
||||
builder.write_string(1, "hello").unwrap();
|
||||
builder.write_int32(2, 42).unwrap();
|
||||
let data = builder.finish().unwrap();
|
||||
|
||||
let acc = ProtoAccessor::new(data).unwrap();
|
||||
let (val1, _) = acc.get_value(1).unwrap();
|
||||
assert_eq!(val1, "hello".as_bytes());
|
||||
let (val2, _) = acc.get_value(2).unwrap();
|
||||
assert_eq!(val2, &[42]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_builder_overflow() {
|
||||
let mut buf = [0u8; 2];
|
||||
let mut builder = ProtoBuilder::new(&mut buf);
|
||||
let result = builder.write_string(1, "too long");
|
||||
assert_eq!(result, Err(RotoError::BufferOverflow));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_raw_field_iterator_yields_correct_bytes() {
|
||||
// Build: field 1 = string "hi", field 2 = int32 42
|
||||
let mut buf = [0u8; 64];
|
||||
let mut builder = ProtoBuilder::new(&mut buf);
|
||||
builder.write_string(1, "hi").unwrap();
|
||||
builder.write_int32(2, 42).unwrap();
|
||||
let data = builder.finish().unwrap().to_vec();
|
||||
|
||||
let acc = ProtoAccessor::new(&data).unwrap();
|
||||
let raw: Vec<_> = acc.raw_fields().collect();
|
||||
assert_eq!(raw.len(), 2);
|
||||
|
||||
// Field 1: tag = (1 << 3) | 2 = 0x0A, len varint = 0x02, "hi" = [0x68, 0x69]
|
||||
let (fn1, bytes1) = raw[0].as_ref().unwrap();
|
||||
assert_eq!(*fn1, 1);
|
||||
assert_eq!(*bytes1, [0x0A, 0x02, b'h', b'i']);
|
||||
|
||||
// Field 2: tag = (2 << 3) | 0 = 0x10, varint 42 = 0x2A
|
||||
let (fn2, bytes2) = raw[1].as_ref().unwrap();
|
||||
assert_eq!(*fn2, 2);
|
||||
assert_eq!(*bytes2, [0x10, 0x2A]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_write_raw_copies_field_verbatim() {
|
||||
// Build source: field 1 = string "hello", field 2 = int32 99
|
||||
let mut src_buf = [0u8; 64];
|
||||
let mut src_builder = ProtoBuilder::new(&mut src_buf);
|
||||
src_builder.write_string(1, "hello").unwrap();
|
||||
src_builder.write_int32(2, 99).unwrap();
|
||||
let src_data = src_builder.finish().unwrap().to_vec();
|
||||
|
||||
// Copy every raw field verbatim into a new buffer
|
||||
let src_acc = ProtoAccessor::new(&src_data).unwrap();
|
||||
let mut dst_buf = [0u8; 64];
|
||||
let mut dst_builder = ProtoBuilder::new(&mut dst_buf);
|
||||
for item in src_acc.raw_fields() {
|
||||
let (_, raw_bytes) = item.unwrap();
|
||||
dst_builder.write_raw(raw_bytes).unwrap();
|
||||
}
|
||||
let dst_data = dst_builder.finish().unwrap();
|
||||
|
||||
// The copy must be byte-identical to the source
|
||||
assert_eq!(dst_data, src_data.as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_pattern_copies_unseen_fields() {
|
||||
// Build an existing source message with 3 fields
|
||||
let mut src_buf = [0u8; 128];
|
||||
let mut src_builder = ProtoBuilder::new(&mut src_buf);
|
||||
src_builder.write_string(1, "original").unwrap();
|
||||
src_builder.write_int32(2, 99).unwrap();
|
||||
src_builder.write_varint(3, 1u64).unwrap(); // bool
|
||||
let src_data = src_builder.finish().unwrap().to_vec();
|
||||
let src_acc = ProtoAccessor::new(&src_data).unwrap();
|
||||
|
||||
// Simulate what a generated `with` method does:
|
||||
// field 1 was explicitly written; fields 2 and 3 come from source.
|
||||
let field1_written = true;
|
||||
let field2_written = false;
|
||||
let field3_written = false;
|
||||
|
||||
let mut dst_buf = [0u8; 128];
|
||||
let mut dst_builder = ProtoBuilder::new(&mut dst_buf);
|
||||
dst_builder.write_string(1, "updated").unwrap();
|
||||
|
||||
for item in src_acc.raw_fields() {
|
||||
let (field_number, raw_bytes) = item.unwrap();
|
||||
let is_written = match field_number {
|
||||
1 => field1_written,
|
||||
2 => field2_written,
|
||||
3 => field3_written,
|
||||
_ => false,
|
||||
};
|
||||
if !is_written {
|
||||
dst_builder.write_raw(raw_bytes).unwrap();
|
||||
}
|
||||
}
|
||||
let dst_data = dst_builder.finish().unwrap();
|
||||
let dst_acc = ProtoAccessor::new(dst_data).unwrap();
|
||||
|
||||
// Field 1: overridden value
|
||||
let (val1, _) = dst_acc.get_value(1).unwrap();
|
||||
assert_eq!(val1, b"updated");
|
||||
|
||||
// Field 2: copied from source
|
||||
let (val2, _) = dst_acc.get_value(2).unwrap();
|
||||
let (v2, _) = read_varint(val2).unwrap();
|
||||
assert_eq!(v2 as i32, 99);
|
||||
|
||||
// Field 3: copied from source
|
||||
let (val3, _) = dst_acc.get_value(3).unwrap();
|
||||
let (v3, _) = read_varint(val3).unwrap();
|
||||
assert_eq!(v3, 1u64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_protoc_binary_compatibility() {
|
||||
let data = include_bytes!("../data/test_data.pb");
|
||||
let acc = ProtoAccessor::new(data).unwrap();
|
||||
|
||||
// 1. Varints (Integers, Booleans, Enums)
|
||||
let (val_i32, type_i32) = acc.get_value(3).expect("i32_val not found");
|
||||
assert_eq!(type_i32, WireType::Varint);
|
||||
let (v, _) = read_varint(val_i32).unwrap();
|
||||
assert_eq!(v, 42);
|
||||
|
||||
let (val_b, type_b) = acc.get_value(13).expect("b_val not found");
|
||||
assert_eq!(type_b, WireType::Varint);
|
||||
let (v_b, _) = read_varint(val_b).unwrap();
|
||||
assert_eq!(v_b, 1); // true
|
||||
|
||||
let (val_status, type_status) = acc.get_value(16).expect("status not found");
|
||||
assert_eq!(type_status, WireType::Varint);
|
||||
let (v_s, _) = read_varint(val_status).unwrap();
|
||||
assert_eq!(v_s, 1); // ACTIVE
|
||||
|
||||
// 2. Length Delimited (Strings, Bytes)
|
||||
let (val_s, type_s) = acc.get_value(14).expect("s_val not found");
|
||||
assert_eq!(type_s, WireType::LengthDelimited);
|
||||
assert_eq!(val_s, "Hello Roto!".as_bytes());
|
||||
|
||||
// 3. Fixed Width (Floats)
|
||||
let (val_f, type_f) = acc.get_value(2).expect("f_val not found");
|
||||
assert_eq!(type_f, WireType::Fixed32);
|
||||
let f_val = f32::from_le_bytes(val_f.try_into().expect("Expected 4 bytes for f32"));
|
||||
assert!((f_val - 2.71828).abs() < 1e-5);
|
||||
|
||||
// 4. Repeated Fields
|
||||
// Note: primitive repeated fields are packed in proto3, so we iterate over the blob
|
||||
let mut i32_vals = Vec::new();
|
||||
for item in acc.iter_repeated(17) {
|
||||
let (blob, _) = item.expect("Failed to decode repeated i32");
|
||||
let mut cursor = 0;
|
||||
while cursor < blob.len() {
|
||||
let (v, len) = read_varint(&blob[cursor..]).unwrap();
|
||||
i32_vals.push(v);
|
||||
cursor += len;
|
||||
}
|
||||
}
|
||||
assert_eq!(i32_vals, vec![1, 2, 3, 4, 5]);
|
||||
|
||||
let repeated_strings: Vec<_> = acc
|
||||
.iter_repeated(18)
|
||||
.map(|r| {
|
||||
let (val, _) = r.expect("Failed to decode repeated string");
|
||||
core::str::from_utf8(val).expect("Invalid utf8")
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(repeated_strings, vec!["one", "two", "three"]);
|
||||
|
||||
let repeated_nested: Vec<_> = acc
|
||||
.iter_repeated(19)
|
||||
.map(|r| {
|
||||
let (val, _) = r.expect("Failed to decode repeated nested");
|
||||
let nested_acc = ProtoAccessor::new(val).unwrap();
|
||||
let (id_val, _) = nested_acc.get_value(1).expect("Nested id not found");
|
||||
let (id, _) = read_varint(id_val).unwrap();
|
||||
id
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(repeated_nested, vec![101, 102]);
|
||||
|
||||
// 5. Single Nested Message
|
||||
let (val_nested, type_nested) = acc.get_value(20).expect("single_nested not found");
|
||||
assert_eq!(type_nested, WireType::LengthDelimited);
|
||||
let nested_acc = ProtoAccessor::new(val_nested).unwrap();
|
||||
let (val_id, _) = nested_acc.get_value(1).expect("Nested id not found");
|
||||
let (id, _) = read_varint(val_id).unwrap();
|
||||
assert_eq!(id, 200);
|
||||
|
||||
// Validate that fields appear in the expected relative order
|
||||
let field_numbers: Vec<u32> = acc
|
||||
.fields()
|
||||
.map(|r| r.expect("Failed to decode field").1.field_number)
|
||||
.collect();
|
||||
|
||||
let essential_fields = [1, 2, 3, 14, 16, 20];
|
||||
let mut last_field = 0;
|
||||
let mut found_count = 0;
|
||||
for &f in &field_numbers {
|
||||
if essential_fields.contains(&f) {
|
||||
assert!(
|
||||
f >= last_field,
|
||||
"Fields appeared out of order: {} came after {}",
|
||||
f,
|
||||
last_field
|
||||
);
|
||||
last_field = f;
|
||||
found_count += 1;
|
||||
}
|
||||
}
|
||||
assert_eq!(found_count, essential_fields.len());
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ProtoBuilder<'a> {
|
||||
buf: &'a mut [u8],
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl<'a> ProtoBuilder<'a> {
|
||||
pub fn new(buf: &'a mut [u8]) -> Self {
|
||||
Self { buf, pos: 0 }
|
||||
}
|
||||
|
||||
fn write_tag(&mut self, field_number: u32, wire_type: WireType) -> Result<()> {
|
||||
let mut temp = [0u8; 10];
|
||||
let len = Tag::encode(field_number, wire_type, &mut temp)?;
|
||||
self.append_bytes(&temp[..len])
|
||||
}
|
||||
|
||||
fn append_bytes(&mut self, bytes: &[u8]) -> Result<()> {
|
||||
if self.pos + bytes.len() > self.buf.len() {
|
||||
return Err(RotoError::BufferOverflow);
|
||||
}
|
||||
self.buf[self.pos..self.pos + bytes.len()].copy_from_slice(bytes);
|
||||
self.pos += bytes.len();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_varint(&mut self, field_number: u32, value: u64) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::Varint)?;
|
||||
let mut temp = [0u8; 10];
|
||||
let len = write_varint(value, &mut temp)?;
|
||||
self.append_bytes(&temp[..len])
|
||||
}
|
||||
|
||||
pub fn write_int32(&mut self, field_number: u32, value: i32) -> Result<()> {
|
||||
self.write_varint(field_number, value as u64)
|
||||
}
|
||||
|
||||
pub fn write_string(&mut self, field_number: u32, value: &str) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::LengthDelimited)?;
|
||||
let bytes = value.as_bytes();
|
||||
let mut len_buf = [0u8; 10];
|
||||
let len_len = write_varint(bytes.len() as u64, &mut len_buf)?;
|
||||
self.append_bytes(&len_buf[..len_len])?;
|
||||
self.append_bytes(bytes)
|
||||
}
|
||||
|
||||
pub fn write_fixed32(&mut self, field_number: u32, value: u32) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::Fixed32)?;
|
||||
self.append_bytes(&value.to_le_bytes())
|
||||
}
|
||||
|
||||
pub fn write_fixed64(&mut self, field_number: u32, value: u64) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::Fixed64)?;
|
||||
self.append_bytes(&value.to_le_bytes())
|
||||
}
|
||||
|
||||
pub fn write_bytes(&mut self, field_number: u32, value: &[u8]) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::LengthDelimited)?;
|
||||
let mut len_buf = [0u8; 10];
|
||||
let len_len = write_varint(value.len() as u64, &mut len_buf)?;
|
||||
self.append_bytes(&len_buf[..len_len])?;
|
||||
self.append_bytes(value)
|
||||
}
|
||||
|
||||
/// Appends a pre-encoded field (tag + value bytes) verbatim into the
|
||||
/// buffer. Use this together with `ProtoAccessor::raw_fields` to copy
|
||||
/// fields from an existing message into a builder without re-encoding them.
|
||||
pub fn write_raw(&mut self, raw_bytes: &[u8]) -> Result<()> {
|
||||
self.append_bytes(raw_bytes)
|
||||
}
|
||||
|
||||
pub fn write_map_entry(
|
||||
&mut self,
|
||||
field_number: u32,
|
||||
key_encoded: &[u8],
|
||||
value_encoded: &[u8],
|
||||
) -> Result<()> {
|
||||
let entry_len = key_encoded.len() + value_encoded.len();
|
||||
self.write_tag(field_number, WireType::LengthDelimited)?;
|
||||
|
||||
let mut len_buf = [0u8; 10];
|
||||
let len_len = write_varint(entry_len as u64, &mut len_buf)?;
|
||||
self.append_bytes(&len_buf[..len_len])?;
|
||||
|
||||
self.append_bytes(key_encoded)?;
|
||||
self.append_bytes(value_encoded)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn finish(self) -> Result<&'a mut [u8]> {
|
||||
Ok(&mut self.buf[..self.pos])
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BufMutBuilder<'a, B: BufMut> {
|
||||
buf: &'a mut B,
|
||||
}
|
||||
|
||||
impl<'a, B: BufMut> BufMutBuilder<'a, B> {
|
||||
pub fn new(buf: &'a mut B) -> Self {
|
||||
Self { buf }
|
||||
}
|
||||
|
||||
fn write_tag(&mut self, field_number: u32, wire_type: WireType) -> Result<()> {
|
||||
let mut temp = [0u8; 10];
|
||||
let len = Tag::encode(field_number, wire_type, &mut temp)?;
|
||||
self.buf.put_slice(&temp[..len]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_varint(&mut self, field_number: u32, value: u64) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::Varint)?;
|
||||
let mut temp = [0u8; 10];
|
||||
let len = write_varint(value, &mut temp)?;
|
||||
self.buf.put_slice(&temp[..len]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_int32(&mut self, field_number: u32, value: i32) -> Result<()> {
|
||||
self.write_varint(field_number, value as u64)
|
||||
}
|
||||
|
||||
pub fn write_string(&mut self, field_number: u32, value: &str) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::LengthDelimited)?;
|
||||
let bytes = value.as_bytes();
|
||||
let mut len_buf = [0u8; 10];
|
||||
let len_len = write_varint(bytes.len() as u64, &mut len_buf)?;
|
||||
self.buf.put_slice(&len_buf[..len_len]);
|
||||
self.buf.put_slice(bytes);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_fixed32(&mut self, field_number: u32, value: u32) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::Fixed32)?;
|
||||
self.buf.put_slice(&value.to_le_bytes());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_fixed64(&mut self, field_number: u32, value: u64) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::Fixed64)?;
|
||||
self.buf.put_slice(&value.to_le_bytes());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_bytes(&mut self, field_number: u32, value: &[u8]) -> Result<()> {
|
||||
self.write_tag(field_number, WireType::LengthDelimited)?;
|
||||
let mut len_buf = [0u8; 10];
|
||||
let len_len = write_varint(value.len() as u64, &mut len_buf)?;
|
||||
self.buf.put_slice(&len_buf[..len_len]);
|
||||
self.buf.put_slice(value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_raw(&mut self, raw_bytes: &[u8]) -> Result<()> {
|
||||
self.buf.put_slice(raw_bytes);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_map_entry(
|
||||
&mut self,
|
||||
field_number: u32,
|
||||
key_encoded: &[u8],
|
||||
value_encoded: &[u8],
|
||||
) -> Result<()> {
|
||||
let entry_len = key_encoded.len() + value_encoded.len();
|
||||
self.write_tag(field_number, WireType::LengthDelimited)?;
|
||||
|
||||
let mut len_buf = [0u8; 10];
|
||||
let len_len = write_varint(entry_len as u64, &mut len_buf)?;
|
||||
self.buf.put_slice(&len_buf[..len_len]);
|
||||
|
||||
self.buf.put_slice(key_encoded);
|
||||
self.buf.put_slice(value_encoded);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user