Include generated code
This commit is contained in:
@@ -0,0 +1 @@
|
||||
pub mod protobuf;
|
||||
@@ -0,0 +1 @@
|
||||
pub mod plugin;
|
||||
@@ -0,0 +1,457 @@
|
||||
use crate::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator};
|
||||
use std::str;
|
||||
|
||||
use crate::google::protobuf::descriptor;
|
||||
|
||||
pub struct Version<'a> {
|
||||
accessor: crate::ProtoAccessor<'a>,
|
||||
major_offset: Option<usize>,
|
||||
minor_offset: Option<usize>,
|
||||
patch_offset: Option<usize>,
|
||||
suffix_offset: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a> Version<'a> {
|
||||
pub fn new(data: &'a [u8]) -> crate::Result<Self> {
|
||||
let accessor = crate::ProtoAccessor::new(data)?;
|
||||
let mut major_offset = None;
|
||||
let mut minor_offset = None;
|
||||
let mut patch_offset = None;
|
||||
let mut suffix_offset = None;
|
||||
for item in accessor.fields() {
|
||||
let (offset, tag, _) = item?;
|
||||
if tag.field_number == 1 { major_offset = Some(offset); }
|
||||
if tag.field_number == 2 { minor_offset = Some(offset); }
|
||||
if tag.field_number == 3 { patch_offset = Some(offset); }
|
||||
if tag.field_number == 4 { suffix_offset = Some(offset); }
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
accessor,
|
||||
major_offset,
|
||||
minor_offset,
|
||||
patch_offset,
|
||||
suffix_offset,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn major(&self) -> crate::Result<i32> {
|
||||
let offset = self.major_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn minor(&self) -> crate::Result<i32> {
|
||||
let offset = self.minor_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn patch(&self) -> crate::Result<i32> {
|
||||
let offset = self.patch_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn suffix(&self) -> crate::Result<&'a str> {
|
||||
let offset = self.suffix_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct VersionBuilder<'b> {
|
||||
builder: crate::ProtoBuilder<'b>,
|
||||
}
|
||||
|
||||
impl<'b> VersionBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> VersionBuilder<'_> {
|
||||
VersionBuilder {
|
||||
builder: crate::ProtoBuilder::new(buf),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn major(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(1, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn minor(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(2, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn patch(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(3, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn suffix(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(4, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn finish(self) -> crate::Result<&'b mut [u8]> {
|
||||
self.builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CodeGeneratorRequest<'a> {
|
||||
accessor: crate::ProtoAccessor<'a>,
|
||||
file_to_generate_start: Option<usize>,
|
||||
file_to_generate_end: Option<usize>,
|
||||
parameter_offset: Option<usize>,
|
||||
proto_file_start: Option<usize>,
|
||||
proto_file_end: Option<usize>,
|
||||
source_file_descriptors_start: Option<usize>,
|
||||
source_file_descriptors_end: Option<usize>,
|
||||
compiler_version_offset: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a> CodeGeneratorRequest<'a> {
|
||||
pub fn new(data: &'a [u8]) -> crate::Result<Self> {
|
||||
let accessor = crate::ProtoAccessor::new(data)?;
|
||||
let mut file_to_generate_start = None;
|
||||
let mut file_to_generate_end = None;
|
||||
let mut parameter_offset = None;
|
||||
let mut proto_file_start = None;
|
||||
let mut proto_file_end = None;
|
||||
let mut source_file_descriptors_start = None;
|
||||
let mut source_file_descriptors_end = None;
|
||||
let mut compiler_version_offset = None;
|
||||
for item in accessor.fields() {
|
||||
let (offset, tag, _) = item?;
|
||||
if tag.field_number == 1 {
|
||||
if file_to_generate_start.is_none() { file_to_generate_start = Some(offset); }
|
||||
file_to_generate_end = Some(offset);
|
||||
}
|
||||
if tag.field_number == 2 { parameter_offset = Some(offset); }
|
||||
if tag.field_number == 15 {
|
||||
if proto_file_start.is_none() { proto_file_start = Some(offset); }
|
||||
proto_file_end = Some(offset);
|
||||
}
|
||||
if tag.field_number == 17 {
|
||||
if source_file_descriptors_start.is_none() { source_file_descriptors_start = Some(offset); }
|
||||
source_file_descriptors_end = Some(offset);
|
||||
}
|
||||
if tag.field_number == 3 { compiler_version_offset = Some(offset); }
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
accessor,
|
||||
file_to_generate_start, file_to_generate_end,
|
||||
parameter_offset,
|
||||
proto_file_start, proto_file_end,
|
||||
source_file_descriptors_start, source_file_descriptors_end,
|
||||
compiler_version_offset,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn file_to_generate(&self) -> crate::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) -> crate::Result<&'a str> {
|
||||
let offset = self.parameter_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn proto_file(&self) -> crate::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) -> crate::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) -> crate::Result<&'a [u8]> {
|
||||
let offset = self.compiler_version_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct CodeGeneratorRequestBuilder<'b> {
|
||||
builder: crate::ProtoBuilder<'b>,
|
||||
}
|
||||
|
||||
impl<'b> CodeGeneratorRequestBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> CodeGeneratorRequestBuilder<'_> {
|
||||
CodeGeneratorRequestBuilder {
|
||||
builder: crate::ProtoBuilder::new(buf),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_to_generate(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(1, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn parameter(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(2, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn proto_file(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(15, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn source_file_descriptors(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(17, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn compiler_version(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(3, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn finish(self) -> crate::Result<&'b mut [u8]> {
|
||||
self.builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CodeGeneratorResponse<'a> {
|
||||
accessor: crate::ProtoAccessor<'a>,
|
||||
error_offset: Option<usize>,
|
||||
supported_features_offset: Option<usize>,
|
||||
minimum_edition_offset: Option<usize>,
|
||||
maximum_edition_offset: Option<usize>,
|
||||
file_start: Option<usize>,
|
||||
file_end: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a> CodeGeneratorResponse<'a> {
|
||||
pub fn new(data: &'a [u8]) -> crate::Result<Self> {
|
||||
let accessor = crate::ProtoAccessor::new(data)?;
|
||||
let mut error_offset = None;
|
||||
let mut supported_features_offset = None;
|
||||
let mut minimum_edition_offset = None;
|
||||
let mut maximum_edition_offset = None;
|
||||
let mut file_start = None;
|
||||
let mut file_end = None;
|
||||
for item in accessor.fields() {
|
||||
let (offset, tag, _) = item?;
|
||||
if tag.field_number == 1 { error_offset = Some(offset); }
|
||||
if tag.field_number == 2 { supported_features_offset = Some(offset); }
|
||||
if tag.field_number == 3 { minimum_edition_offset = Some(offset); }
|
||||
if tag.field_number == 4 { maximum_edition_offset = Some(offset); }
|
||||
if tag.field_number == 15 {
|
||||
if file_start.is_none() { file_start = Some(offset); }
|
||||
file_end = Some(offset);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
accessor,
|
||||
error_offset,
|
||||
supported_features_offset,
|
||||
minimum_edition_offset,
|
||||
maximum_edition_offset,
|
||||
file_start, file_end,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn error(&self) -> crate::Result<&'a str> {
|
||||
let offset = self.error_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn supported_features(&self) -> crate::Result<u32> {
|
||||
let offset = self.supported_features_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
crate::read_varint(bytes).map(|(v, _)| v as u32).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn minimum_edition(&self) -> crate::Result<i32> {
|
||||
let offset = self.minimum_edition_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn maximum_edition(&self) -> crate::Result<i32> {
|
||||
let offset = self.maximum_edition_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn file(&self) -> crate::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 struct CodeGeneratorResponseBuilder<'b> {
|
||||
builder: crate::ProtoBuilder<'b>,
|
||||
}
|
||||
|
||||
impl<'b> CodeGeneratorResponseBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> CodeGeneratorResponseBuilder<'_> {
|
||||
CodeGeneratorResponseBuilder {
|
||||
builder: crate::ProtoBuilder::new(buf),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(1, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn supported_features(mut self, value: u64) -> crate::Result<Self> {
|
||||
self.builder.write_varint(2, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn minimum_edition(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(3, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn maximum_edition(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(4, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn file(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(15, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn finish(self) -> crate::Result<&'b mut [u8]> {
|
||||
self.builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub mod code_generator_response {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(i32)]
|
||||
pub enum Feature {
|
||||
FEATURENONE = 0,
|
||||
FEATUREPROTO3OPTIONAL = 1,
|
||||
FEATURESUPPORTSEDITIONS = 2,
|
||||
}
|
||||
|
||||
impl Feature {
|
||||
pub fn from_i32(value: i32) -> Self {
|
||||
match value {
|
||||
0 => Feature::FEATURENONE,
|
||||
1 => Feature::FEATUREPROTO3OPTIONAL,
|
||||
2 => Feature::FEATURESUPPORTSEDITIONS,
|
||||
_ => Feature::FEATURENONE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct File<'a> {
|
||||
accessor: crate::ProtoAccessor<'a>,
|
||||
name_offset: Option<usize>,
|
||||
insertion_point_offset: Option<usize>,
|
||||
content_offset: Option<usize>,
|
||||
generated_code_info_offset: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a> File<'a> {
|
||||
pub fn new(data: &'a [u8]) -> crate::Result<Self> {
|
||||
let accessor = crate::ProtoAccessor::new(data)?;
|
||||
let mut name_offset = None;
|
||||
let mut insertion_point_offset = None;
|
||||
let mut content_offset = None;
|
||||
let mut generated_code_info_offset = None;
|
||||
for item in accessor.fields() {
|
||||
let (offset, tag, _) = item?;
|
||||
if tag.field_number == 1 { name_offset = Some(offset); }
|
||||
if tag.field_number == 2 { insertion_point_offset = Some(offset); }
|
||||
if tag.field_number == 15 { content_offset = Some(offset); }
|
||||
if tag.field_number == 16 { generated_code_info_offset = Some(offset); }
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
accessor,
|
||||
name_offset,
|
||||
insertion_point_offset,
|
||||
content_offset,
|
||||
generated_code_info_offset,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn name(&self) -> crate::Result<&'a str> {
|
||||
let offset = self.name_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn insertion_point(&self) -> crate::Result<&'a str> {
|
||||
let offset = self.insertion_point_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn content(&self) -> crate::Result<&'a str> {
|
||||
let offset = self.content_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
str::from_utf8(bytes).map_err(|_| crate::RotoError::WireFormatViolation)
|
||||
}
|
||||
|
||||
pub fn generated_code_info(&self) -> crate::Result<&'a [u8]> {
|
||||
let offset = self.generated_code_info_offset.ok_or(crate::RotoError::FieldNotFound)?;
|
||||
let (bytes, _) = self.accessor.get_value_at(offset)?;
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct FileBuilder<'b> {
|
||||
builder: crate::ProtoBuilder<'b>,
|
||||
}
|
||||
|
||||
impl<'b> FileBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> FileBuilder<'_> {
|
||||
FileBuilder {
|
||||
builder: crate::ProtoBuilder::new(buf),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(1, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn insertion_point(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(2, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn content(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(15, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn generated_code_info(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(16, value)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn finish(self) -> crate::Result<&'b mut [u8]> {
|
||||
self.builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,2 @@
|
||||
pub mod compiler;
|
||||
pub mod descriptor;
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
pub mod proto_gen;
|
||||
pub mod generator;
|
||||
// pub mod google;
|
||||
pub mod google;
|
||||
// Uncomment this to check if the code compiles
|
||||
// #[path = "../proto/google/protobuf/descriptor.rs"]
|
||||
// pub mod descriptor;
|
||||
|
||||
Reference in New Issue
Block a user