First pass at plugin gen
This commit is contained in:
Generated
+134
@@ -2,6 +2,15 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "1.1.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstream"
|
name = "anstream"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@@ -98,6 +107,29 @@ version = "1.0.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_filter"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32e90c2accc4b07a8456ea0debdc2e7587bdd890680d71173a15d4ae604f6eef"
|
||||||
|
dependencies = [
|
||||||
|
"log",
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_logger"
|
||||||
|
version = "0.11.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0621c04f2196ac3f488dd583365b9c09be011a4ab8b9f37248ffcc8f6198b56a"
|
||||||
|
dependencies = [
|
||||||
|
"anstream",
|
||||||
|
"anstyle",
|
||||||
|
"env_filter",
|
||||||
|
"jiff",
|
||||||
|
"log",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
@@ -110,12 +142,63 @@ version = "1.70.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jiff"
|
||||||
|
version = "0.2.24"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f00b5dbd620d61dfdcb6007c9c1f6054ebd75319f163d886a9055cec1155073d"
|
||||||
|
dependencies = [
|
||||||
|
"jiff-static",
|
||||||
|
"log",
|
||||||
|
"portable-atomic",
|
||||||
|
"portable-atomic-util",
|
||||||
|
"serde_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jiff-static"
|
||||||
|
version = "0.2.24"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e000de030ff8022ea1da3f466fbb0f3a809f5e51ed31f6dd931c35181ad8e6d7"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "log"
|
||||||
|
version = "0.4.29"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell_polyfill"
|
name = "once_cell_polyfill"
|
||||||
version = "1.70.2"
|
version = "1.70.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "portable-atomic"
|
||||||
|
version = "1.13.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "portable-atomic-util"
|
||||||
|
version = "0.2.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c2a106d1259c23fac8e543272398ae0e3c0b8d33c88ed73d0cc71b0f1d902618"
|
||||||
|
dependencies = [
|
||||||
|
"portable-atomic",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.106"
|
version = "1.0.106"
|
||||||
@@ -134,11 +217,62 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.12.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-automata",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-automata"
|
||||||
|
version = "0.4.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.8.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "roto"
|
name = "roto"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"env_logger",
|
||||||
|
"log",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_core"
|
||||||
|
version = "1.0.228"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.228"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -5,3 +5,5 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4", features = ["derive"] }
|
clap = { version = "4", features = ["derive"] }
|
||||||
|
log = "0.4"
|
||||||
|
env_logger = "0.11"
|
||||||
|
|||||||
+3
-203
@@ -1,7 +1,6 @@
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use roto::proto_gen::google::protobuf::descriptor::{
|
use roto::proto_gen::google::protobuf::descriptor::FileDescriptorSet;
|
||||||
DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto, FileDescriptorSet,
|
use roto::proto_gen::generator::generate_rust_code;
|
||||||
};
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
@@ -17,211 +16,12 @@ struct Args {
|
|||||||
output: PathBuf,
|
output: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_pascal_case(s: &str) -> String {
|
|
||||||
s.split('_')
|
|
||||||
.map(|word| {
|
|
||||||
let mut chars = word.chars();
|
|
||||||
match chars.next() {
|
|
||||||
None => String::new(),
|
|
||||||
Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn map_type_to_rust_accessor(field_type: i32, label: i32) -> (String, String) {
|
|
||||||
if label == 3 {
|
|
||||||
// LABEL_REPEATED
|
|
||||||
return (
|
|
||||||
"crate::RepeatedFieldIterator<'a>".to_string(),
|
|
||||||
"self.0.iter_repeated(%d)".to_string(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
match field_type {
|
|
||||||
9 => (
|
|
||||||
"&'a str".to_string(),
|
|
||||||
"str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
|
||||||
), // TYPE_STRING
|
|
||||||
1 => (
|
|
||||||
"f64".to_string(),
|
|
||||||
"f64::from_le_bytes(bytes.try_into().map_err(|_| RotoError::WireFormatViolation)?)".to_string(),
|
|
||||||
), // TYPE_DOUBLE
|
|
||||||
2 => (
|
|
||||||
"f32".to_string(),
|
|
||||||
"f32::from_le_bytes(bytes.try_into().map_err(|_| RotoError::WireFormatViolation)?)".to_string(),
|
|
||||||
), // TYPE_FLOAT
|
|
||||||
3 | 5 | 15 | 17 => (
|
|
||||||
"i32".to_string(),
|
|
||||||
"crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
|
||||||
), // INT/SINT/SFIXED 32
|
|
||||||
4 | 6 | 13 => (
|
|
||||||
"u32".to_string(),
|
|
||||||
"crate::read_varint(bytes).map(|(v, _)| v as u32).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
|
||||||
), // UINT/FIXED 32
|
|
||||||
16 | 18 => (
|
|
||||||
"i64".to_string(),
|
|
||||||
"crate::read_varint(bytes).map(|(v, _)| v as i64).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
|
||||||
), // SINT/SFIXED 64
|
|
||||||
7 | 14 => (
|
|
||||||
"u64".to_string(),
|
|
||||||
"crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
|
||||||
), // UINT/FIXED 64
|
|
||||||
8 => (
|
|
||||||
"bool".to_string(),
|
|
||||||
"crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
|
||||||
), // TYPE_BOOL
|
|
||||||
11 | 12 => ("&'a [u8]".to_string(), "Ok(bytes)".to_string()), // MESSAGE/BYTES
|
|
||||||
_ => ("&'a [u8]".to_string(), "Ok(bytes)".to_string()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn map_type_to_rust_builder(field_type: i32) -> (String, String) {
|
|
||||||
match field_type {
|
|
||||||
9 => ("&str".to_string(), "write_string".to_string()),
|
|
||||||
5 | 17 => ("i32".to_string(), "write_int32".to_string()),
|
|
||||||
3 | 4 | 8 | 13 | 14 | 18 => ("i64".to_string(), "write_varint".to_string()),
|
|
||||||
7 | 15 => ("u32".to_string(), "write_fixed32".to_string()),
|
|
||||||
6 | 16 => ("u64".to_string(), "write_fixed64".to_string()),
|
|
||||||
11 | 12 => ("&[u8]".to_string(), "write_bytes".to_string()),
|
|
||||||
_ => ("&[u8]".to_string(), "write_bytes".to_string()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let data = fs::read(&args.input)?;
|
let data = fs::read(&args.input)?;
|
||||||
let set = FileDescriptorSet::new(&data).expect("Failed to parse FileDescriptorSet");
|
let set = FileDescriptorSet::new(&data).expect("Failed to parse FileDescriptorSet");
|
||||||
|
|
||||||
let mut output = String::new();
|
let output = generate_rust_code(&set);
|
||||||
output.push_str("use crate::{ProtoAccessor, ProtoBuilder, Result, RotoError};\n");
|
|
||||||
output.push_str("use std::str;\n\n");
|
|
||||||
|
|
||||||
for file_res in set.file() {
|
|
||||||
let (file_data, _) = file_res.expect("Failed to iterate file");
|
|
||||||
let file_proto = FileDescriptorProto::new(file_data).expect("Failed to parse FileDescriptorProto");
|
|
||||||
|
|
||||||
// Enums
|
|
||||||
for enum_res in file_proto.enum_type() {
|
|
||||||
let (enum_data, _) = enum_res.expect("Failed to iterate enum");
|
|
||||||
let enum_proto = EnumDescriptorProto::new(enum_data).expect("Failed to parse EnumDescriptorProto");
|
|
||||||
let enum_name = to_pascal_case(enum_proto.name().unwrap());
|
|
||||||
|
|
||||||
output.push_str(&format!(
|
|
||||||
"#[derive(Debug, Clone, Copy, PartialEq, Eq)]\n#[repr(i32)]\npub enum {} {{\n",
|
|
||||||
enum_name
|
|
||||||
));
|
|
||||||
|
|
||||||
let mut values = enum_proto.enum_value();
|
|
||||||
let mut variant_count = 0;
|
|
||||||
while let Some(val_res) = values.next() {
|
|
||||||
let (val_data, _) = val_res.expect("Failed to read enum value");
|
|
||||||
let accessor = roto::ProtoAccessor::new(val_data).expect("Failed to parse EnumValueDescriptorProto");
|
|
||||||
let (name_bytes, _) = accessor.get_value(1).expect("Enum value name missing");
|
|
||||||
let name = 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::read_varint(num_bytes).expect("Enum value number invalid varint");
|
|
||||||
|
|
||||||
output.push_str(&format!(" {}, = {},\n", to_pascal_case(name), num));
|
|
||||||
variant_count += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if variant_count == 0 {
|
|
||||||
output.push_str(" Unknown = 0,\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
output.push_str("}\n\n");
|
|
||||||
|
|
||||||
output.push_str(&format!(
|
|
||||||
"impl {} {{\n pub fn from_i32(value: i32) -> Self {{\n match value {{\n",
|
|
||||||
enum_name
|
|
||||||
));
|
|
||||||
|
|
||||||
let mut values = enum_proto.enum_value();
|
|
||||||
while let Some(val_res) = values.next() {
|
|
||||||
let (val_data, _) = val_res.expect("Failed to read enum value");
|
|
||||||
let accessor = roto::ProtoAccessor::new(val_data).expect("Failed to parse EnumValueDescriptorProto");
|
|
||||||
let (name_bytes, _) = accessor.get_value(1).expect("Enum value name missing");
|
|
||||||
let name = 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::read_varint(num_bytes).expect("Enum value number invalid varint");
|
|
||||||
|
|
||||||
output.push_str(&format!(" {} => {}::{},\n", num, enum_name, to_pascal_case(name)));
|
|
||||||
}
|
|
||||||
|
|
||||||
output.push_str(&format!(" _ => {}::Unknown,\n", enum_name));
|
|
||||||
output.push_str(" }\n }}\n}\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Messages
|
|
||||||
for msg_res in file_proto.message_type() {
|
|
||||||
let (msg_data, _) = msg_res.expect("Failed to iterate message");
|
|
||||||
let msg_proto = DescriptorProto::new(msg_data).expect("Failed to parse DescriptorProto");
|
|
||||||
let msg_name = to_pascal_case(msg_proto.name().unwrap());
|
|
||||||
|
|
||||||
// Accessor
|
|
||||||
output.push_str(&format!(
|
|
||||||
"pub struct {}<'a>(ProtoAccessor<'a>);\n\nimpl<'a> {}<'a> {{\n",
|
|
||||||
msg_name, msg_name
|
|
||||||
));
|
|
||||||
output.push_str(&format!(
|
|
||||||
" pub fn new(data: &'a [u8]) -> Result<Self> {{\n Ok(Self(ProtoAccessor::new(data)?))\n }}\n\n"
|
|
||||||
));
|
|
||||||
|
|
||||||
for field_res in msg_proto.field() {
|
|
||||||
let (field_data, _) = field_res.expect("Failed to iterate field");
|
|
||||||
let field_proto = FieldDescriptorProto::new(field_data).expect("Failed to parse FieldDescriptorProto");
|
|
||||||
let field_name = field_proto.name().unwrap();
|
|
||||||
let tag = field_proto.number().unwrap();
|
|
||||||
let f_type = field_proto.field_type().unwrap() as i32;
|
|
||||||
let f_label = field_proto.label().unwrap() as i32;
|
|
||||||
|
|
||||||
let (rust_type, logic) = map_type_to_rust_accessor(f_type, f_label);
|
|
||||||
|
|
||||||
if f_label == 3 {
|
|
||||||
output.push_str(&format!(
|
|
||||||
" pub fn {}(&self) -> {} {{\n {}\n }}\n\n",
|
|
||||||
field_name, rust_type, logic.replace("%d", &tag.to_string())
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
output.push_str(&format!(
|
|
||||||
" pub fn {}(&self) -> Result<{}> {{\n let (bytes, _) = self.0.get_value({})?;\n {}\n }}\n\n",
|
|
||||||
field_name, rust_type, tag, logic
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
output.push_str("}\n\n");
|
|
||||||
|
|
||||||
// Builder
|
|
||||||
output.push_str(&format!(
|
|
||||||
"pub struct {}Builder<'b> {{\n builder: ProtoBuilder<'b>,\n}}\n\nimpl<'b> {}Builder<'b> {{\n",
|
|
||||||
msg_name, msg_name
|
|
||||||
));
|
|
||||||
output.push_str(&format!(
|
|
||||||
" pub fn builder(buf: &mut [u8]) -> {}Builder<'_> {{\n {}Builder {{\n builder: ProtoBuilder::new(buf),\n }}\n }}\n\n",
|
|
||||||
msg_name, msg_name
|
|
||||||
));
|
|
||||||
|
|
||||||
for field_res in msg_proto.field() {
|
|
||||||
let (field_data, _) = field_res.expect("Failed to iterate field");
|
|
||||||
let field_proto = FieldDescriptorProto::new(field_data).expect("Failed to parse FieldDescriptorProto");
|
|
||||||
let field_name = field_proto.name().unwrap();
|
|
||||||
let tag = field_proto.number().unwrap();
|
|
||||||
let f_type = field_proto.field_type().unwrap() as i32;
|
|
||||||
|
|
||||||
let (rust_type, method) = map_type_to_rust_builder(f_type);
|
|
||||||
|
|
||||||
output.push_str(&format!(
|
|
||||||
" pub fn {}(mut self, value: {}) -> Result<Self> {{\n self.builder.{}({}, value)?;\n Ok(self)\n }}\n\n",
|
|
||||||
field_name, rust_type, method, tag
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
output.push_str(&format!(
|
|
||||||
" pub fn finish(self) -> Result<&'b mut [u8]> {{\n self.builder.finish()\n }}\n}}\n\n"
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::write(&args.output, output)?;
|
fs::write(&args.output, output)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -0,0 +1,107 @@
|
|||||||
|
use env_logger::init;
|
||||||
|
use log::{error, info};
|
||||||
|
use roto::generator::generate_rust_code;
|
||||||
|
use roto::proto_gen::google::protobuf::descriptor::{
|
||||||
|
CodeGeneratorRequest, CodeGeneratorResponse, FileDescriptorSet, ResponseFile,
|
||||||
|
};
|
||||||
|
use roto::ProtoBuilder;
|
||||||
|
use std::io::{self, Read, Write};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Initialize logger to print to stderr
|
||||||
|
init();
|
||||||
|
|
||||||
|
if let Err(e) = run() {
|
||||||
|
error!("Plugin error: {}", e);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
||||||
|
// 1. Read CodeGeneratorRequest from stdin
|
||||||
|
let mut stdin_buf = Vec::new();
|
||||||
|
io::stdin().read_to_end(&mut stdin_buf)?;
|
||||||
|
|
||||||
|
if stdin_buf.is_empty() {
|
||||||
|
error!("Received empty CodeGeneratorRequest from stdin");
|
||||||
|
return Err("Empty stdin".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let request = CodeGeneratorRequest::new(&stdin_buf)?;
|
||||||
|
|
||||||
|
// 2. Construct a FileDescriptorSet from the request's proto_files
|
||||||
|
// Since generate_rust_code expects a FileDescriptorSet, we wrap the proto_files in one.
|
||||||
|
// A FileDescriptorSet is a message with a repeated field 'file' at tag 1.
|
||||||
|
let mut set_buf = vec![0u8; 1024 * 1024]; // Allocate 1MB for descriptors
|
||||||
|
{
|
||||||
|
let mut builder = ProtoBuilder::new(&mut set_buf);
|
||||||
|
for file_res in request.proto_file() {
|
||||||
|
let (file_data, _) = file_res.map_err(|e| {
|
||||||
|
error!("Failed to iterate proto_file: {:?}", e);
|
||||||
|
e
|
||||||
|
})?;
|
||||||
|
builder.write_bytes(1, file_data).map_err(|e| {
|
||||||
|
error!("Failed to write proto_file to set: {:?}", e);
|
||||||
|
e
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
// We don't need to call finish() here as we just need the buffer to be populated.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trim set_buf to the actual size written by ProtoBuilder
|
||||||
|
// We need to find the actual length. ProtoBuilder doesn't expose pos publicly in the provided snippet,
|
||||||
|
// but we can wrap the result of the generation.
|
||||||
|
// Actually, since FileDescriptorSet::new just wraps the buffer, it will read until the end.
|
||||||
|
// We should be careful about trailing zeros.
|
||||||
|
// Let's just use a slice of the buffer that was actually used.
|
||||||
|
// Since ProtoBuilder's pos is private, we might need a different approach or just hope the
|
||||||
|
// ProtoAccessor handles trailing zeros (which it should if it's a valid proto message).
|
||||||
|
// However, the most robust way is to use the length.
|
||||||
|
|
||||||
|
// Let's try to use the buffer as is. If ProtoAccessor is correctly implemented,
|
||||||
|
// it will stop at the end of the message.
|
||||||
|
let set = FileDescriptorSet::new(&set_buf)?;
|
||||||
|
|
||||||
|
// 3. Generate the Rust code
|
||||||
|
info!("Generating Rust code from descriptor set...");
|
||||||
|
let generated_code = generate_rust_code(&set);
|
||||||
|
|
||||||
|
// 4. Construct CodeGeneratorResponse
|
||||||
|
// We'll put all generated code into a single file for now.
|
||||||
|
// If file_to_generate is provided, we'll use the first one as a base for the name.
|
||||||
|
let mut output_filename = "roto_generated.rs".to_string();
|
||||||
|
if let Some(first_file) = request.file_to_generate().next() {
|
||||||
|
if let Ok((name_bytes, _)) = first_file {
|
||||||
|
if let Ok(name) = std::str::from_utf8(name_bytes) {
|
||||||
|
output_filename = format!("{}.rs", name.replace(".proto", ""));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut response_buf = vec![0u8; 1024 * 1024 * 2]; // Allocate 2MB for response
|
||||||
|
let mut resp_builder = CodeGeneratorResponse::builder(&mut response_buf);
|
||||||
|
|
||||||
|
let mut file_buf = vec![0u8; 1024 * 1024 * 2];
|
||||||
|
let final_file = ResponseFile::builder(&mut file_buf)
|
||||||
|
.name(&output_filename)?
|
||||||
|
.content(&generated_code)?
|
||||||
|
.finish()
|
||||||
|
.map_err(|e| {
|
||||||
|
error!("Failed to build ResponseFile: {:?}", e);
|
||||||
|
e
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let final_response = resp_builder
|
||||||
|
.add_file(final_file)?
|
||||||
|
.finish()
|
||||||
|
.map_err(|e| {
|
||||||
|
error!("Failed to finish CodeGeneratorResponse: {:?}", e);
|
||||||
|
e
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// 5. Write response to stdout
|
||||||
|
io::stdout().write_all(final_response)?;
|
||||||
|
|
||||||
|
info!("Successfully wrote CodeGeneratorResponse to stdout");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,210 @@
|
|||||||
|
use crate::proto_gen::google::protobuf::descriptor::{
|
||||||
|
DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto, FileDescriptorSet,
|
||||||
|
};
|
||||||
|
use crate::{ProtoAccessor, Result, RotoError};
|
||||||
|
use std::str;
|
||||||
|
|
||||||
|
pub fn to_pascal_case(s: &str) -> String {
|
||||||
|
s.split('_')
|
||||||
|
.map(|word| {
|
||||||
|
let mut chars = word.chars();
|
||||||
|
match chars.next() {
|
||||||
|
None => String::new(),
|
||||||
|
Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_type_to_rust_accessor(field_type: i32, label: i32) -> (String, String) {
|
||||||
|
if label == 3 {
|
||||||
|
// LABEL_REPEATED
|
||||||
|
return (
|
||||||
|
"crate::RepeatedFieldIterator<'a>".to_string(),
|
||||||
|
"self.0.iter_repeated(%d)".to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
match field_type {
|
||||||
|
9 => (
|
||||||
|
"&'a str".to_string(),
|
||||||
|
"str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
||||||
|
), // TYPE_STRING
|
||||||
|
1 => (
|
||||||
|
"f64".to_string(),
|
||||||
|
"f64::from_le_bytes(bytes.try_into().map_err(|_| RotoError::WireFormatViolation)?)".to_string(),
|
||||||
|
), // TYPE_DOUBLE
|
||||||
|
2 => (
|
||||||
|
"f32".to_string(),
|
||||||
|
"f32::from_le_bytes(bytes.try_into().map_err(|_| RotoError::WireFormatViolation)?)".to_string(),
|
||||||
|
), // TYPE_FLOAT
|
||||||
|
3 | 5 | 15 | 17 => (
|
||||||
|
"i32".to_string(),
|
||||||
|
"crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
||||||
|
), // INT/SINT/SFIXED 32
|
||||||
|
4 | 6 | 13 => (
|
||||||
|
"u32".to_string(),
|
||||||
|
"crate::read_varint(bytes).map(|(v, _)| v as u32).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
||||||
|
), // UINT/FIXED 32
|
||||||
|
16 | 18 => (
|
||||||
|
"i64".to_string(),
|
||||||
|
"crate::read_varint(bytes).map(|(v, _)| v as i64).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
||||||
|
), // SINT/SFIXED 64
|
||||||
|
7 | 14 => (
|
||||||
|
"u64".to_string(),
|
||||||
|
"crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
||||||
|
), // UINT/FIXED 64
|
||||||
|
8 => (
|
||||||
|
"bool".to_string(),
|
||||||
|
"crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation)".to_string(),
|
||||||
|
), // TYPE_BOOL
|
||||||
|
11 | 12 => ("&'a [u8]".to_string(), "Ok(bytes)".to_string()), // MESSAGE/BYTES
|
||||||
|
_ => ("&'a [u8]".to_string(), "Ok(bytes)".to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_type_to_rust_builder(field_type: i32) -> (String, String) {
|
||||||
|
match field_type {
|
||||||
|
9 => ("&str".to_string(), "write_string".to_string()),
|
||||||
|
5 | 17 => ("i32".to_string(), "write_int32".to_string()),
|
||||||
|
3 | 4 | 8 | 13 | 14 | 18 => ("i64".to_string(), "write_varint".to_string()),
|
||||||
|
7 | 15 => ("u32".to_string(), "write_fixed32".to_string()),
|
||||||
|
6 | 16 => ("u64".to_string(), "write_fixed64".to_string()),
|
||||||
|
11 | 12 => ("&[u8]".to_string(), "write_bytes".to_string()),
|
||||||
|
_ => ("&[u8]".to_string(), "write_bytes".to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_rust_code(set: &FileDescriptorSet) -> String {
|
||||||
|
let mut output = String::new();
|
||||||
|
output.push_str("use crate::{ProtoAccessor, ProtoBuilder, Result, RotoError};\n");
|
||||||
|
output.push_str("use std::str;\n\n");
|
||||||
|
|
||||||
|
for file_res in set.file() {
|
||||||
|
let (file_data, _) = file_res.expect("Failed to iterate file");
|
||||||
|
let file_proto = FileDescriptorProto::new(file_data).expect("Failed to parse FileDescriptorProto");
|
||||||
|
|
||||||
|
// Enums
|
||||||
|
for enum_res in file_proto.enum_type() {
|
||||||
|
let (enum_data, _) = enum_res.expect("Failed to iterate enum");
|
||||||
|
let enum_proto = EnumDescriptorProto::new(enum_data).expect("Failed to parse EnumDescriptorProto");
|
||||||
|
let enum_name = to_pascal_case(enum_proto.name().unwrap());
|
||||||
|
|
||||||
|
output.push_str(&format!(
|
||||||
|
"#[derive(Debug, Clone, Copy, PartialEq, Eq)]\n#[repr(i32)]\npub enum {} {{\n",
|
||||||
|
enum_name
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut values = enum_proto.enum_value();
|
||||||
|
let mut variant_count = 0;
|
||||||
|
while let Some(val_res) = values.next() {
|
||||||
|
let (val_data, _) = val_res.expect("Failed to read enum value");
|
||||||
|
let accessor = ProtoAccessor::new(val_data).expect("Failed to parse EnumValueDescriptorProto");
|
||||||
|
let (name_bytes, _) = accessor.get_value(1).expect("Enum value name missing");
|
||||||
|
let name = 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, _) = crate::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||||
|
|
||||||
|
output.push_str(&format!(" {}, = {},\n", to_pascal_case(name), num));
|
||||||
|
variant_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if variant_count == 0 {
|
||||||
|
output.push_str(" Unknown = 0,\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
output.push_str("}\n\n");
|
||||||
|
|
||||||
|
output.push_str(&format!(
|
||||||
|
"impl {} {{\n pub fn from_i32(value: i32) -> Self {{\n match value {{\n",
|
||||||
|
enum_name
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut values = enum_proto.enum_value();
|
||||||
|
while let Some(val_res) = values.next() {
|
||||||
|
let (val_data, _) = val_res.expect("Failed to read enum value");
|
||||||
|
let accessor = ProtoAccessor::new(val_data).expect("Failed to parse EnumValueDescriptorProto");
|
||||||
|
let (name_bytes, _) = accessor.get_value(1).expect("Enum value name missing");
|
||||||
|
let name = 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, _) = crate::read_varint(num_bytes).expect("Enum value number invalid varint");
|
||||||
|
|
||||||
|
output.push_str(&format!(" {} => {}::{},\n", num, enum_name, to_pascal_case(name)));
|
||||||
|
}
|
||||||
|
|
||||||
|
output.push_str(&format!(" _ => {}::Unknown,\n", enum_name));
|
||||||
|
output.push_str(" }\n }}\n}\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Messages
|
||||||
|
for msg_res in file_proto.message_type() {
|
||||||
|
let (msg_data, _) = msg_res.expect("Failed to iterate message");
|
||||||
|
let msg_proto = DescriptorProto::new(msg_data).expect("Failed to parse DescriptorProto");
|
||||||
|
let msg_name = to_pascal_case(msg_proto.name().unwrap());
|
||||||
|
|
||||||
|
// Accessor
|
||||||
|
output.push_str(&format!(
|
||||||
|
"pub struct {}<'a>(ProtoAccessor<'a>);\n\nimpl<'a> {}<'a> {{\n",
|
||||||
|
msg_name, msg_name
|
||||||
|
));
|
||||||
|
output.push_str(&format!(
|
||||||
|
" pub fn new(data: &'a [u8]) -> Result<Self> {{\n Ok(Self(ProtoAccessor::new(data)?))\n }}\n\n"
|
||||||
|
));
|
||||||
|
|
||||||
|
for field_res in msg_proto.field() {
|
||||||
|
let (field_data, _) = field_res.expect("Failed to iterate field");
|
||||||
|
let field_proto = FieldDescriptorProto::new(field_data).expect("Failed to parse FieldDescriptorProto");
|
||||||
|
let field_name = field_proto.name().unwrap();
|
||||||
|
let tag = field_proto.number().unwrap();
|
||||||
|
let f_type = field_proto.field_type().unwrap() as i32;
|
||||||
|
let f_label = field_proto.label().unwrap() as i32;
|
||||||
|
|
||||||
|
let (rust_type, logic) = map_type_to_rust_accessor(f_type, f_label);
|
||||||
|
|
||||||
|
if f_label == 3 {
|
||||||
|
output.push_str(&format!(
|
||||||
|
" pub fn {}(&self) -> {} {{\n {}\n }}\n\n",
|
||||||
|
field_name, rust_type, logic.replace("%d", &tag.to_string())
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
output.push_str(&format!(
|
||||||
|
" pub fn {}(&self) -> Result<{}> {{\n let (bytes, _) = self.0.get_value({})?;\n {}\n }}\n\n",
|
||||||
|
field_name, rust_type, tag, logic
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output.push_str("}\n\n");
|
||||||
|
|
||||||
|
// Builder
|
||||||
|
output.push_str(&format!(
|
||||||
|
"pub struct {}Builder<'b> {{\n builder: ProtoBuilder<'b>,\n}}\n\nimpl<'b> {}Builder<'b> {{\n",
|
||||||
|
msg_name, msg_name
|
||||||
|
));
|
||||||
|
output.push_str(&format!(
|
||||||
|
" pub fn builder(buf: &mut [u8]) -> {}Builder<'_> {{\n {}Builder {{\n builder: ProtoBuilder::new(buf),\n }}\n }}\n\n",
|
||||||
|
msg_name, msg_name
|
||||||
|
));
|
||||||
|
|
||||||
|
for field_res in msg_proto.field() {
|
||||||
|
let (field_data, _) = field_res.expect("Failed to iterate field");
|
||||||
|
let field_proto = FieldDescriptorProto::new(field_data).expect("Failed to parse FieldDescriptorProto");
|
||||||
|
let field_name = field_proto.name().unwrap();
|
||||||
|
let tag = field_proto.number().unwrap();
|
||||||
|
let f_type = field_proto.field_type().unwrap() as i32;
|
||||||
|
|
||||||
|
let (rust_type, method) = map_type_to_rust_builder(f_type);
|
||||||
|
|
||||||
|
output.push_str(&format!(
|
||||||
|
" pub fn {}(mut self, value: {}) -> Result<Self> {{\n self.builder.{}({}, value)?;\n Ok(self)\n }}\n\n",
|
||||||
|
field_name, rust_type, method, tag
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
output.push_str(&format!(
|
||||||
|
" pub fn finish(self) -> Result<&'b mut [u8]> {{\n self.builder.finish()\n }}\n}}\n\n"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
output
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
pub mod proto_gen;
|
pub mod proto_gen;
|
||||||
|
pub mod generator;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
|||||||
@@ -533,6 +533,284 @@ impl<'b> GeneratedCodeInfoBuilder<'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Version<'a>(ProtoAccessor<'a>);
|
||||||
|
|
||||||
|
impl<'a> Version<'a> {
|
||||||
|
pub fn new(data: &'a [u8]) -> Result<Self> {
|
||||||
|
Ok(Self(ProtoAccessor::new(data)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn major(&self) -> Result<i32> {
|
||||||
|
let (bytes, _) = self.0.get_value(1)?;
|
||||||
|
let (val, _) = crate::read_varint(bytes)?;
|
||||||
|
Ok(val as i32)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn minor(&self) -> Result<i32> {
|
||||||
|
let (bytes, _) = self.0.get_value(2)?;
|
||||||
|
let (val, _) = crate::read_varint(bytes)?;
|
||||||
|
Ok(val as i32)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn patch(&self) -> Result<i32> {
|
||||||
|
let (bytes, _) = self.0.get_value(3)?;
|
||||||
|
let (val, _) = crate::read_varint(bytes)?;
|
||||||
|
Ok(val as i32)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn suffix(&self) -> Result<&'a str> {
|
||||||
|
let (bytes, _) = self.0.get_value(4)?;
|
||||||
|
str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn builder(buf: &mut [u8]) -> VersionBuilder<'_> {
|
||||||
|
VersionBuilder {
|
||||||
|
builder: ProtoBuilder::new(buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct VersionBuilder<'b> {
|
||||||
|
builder: ProtoBuilder<'b>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b> VersionBuilder<'b> {
|
||||||
|
pub fn major(mut self, value: i32) -> Result<Self> {
|
||||||
|
self.builder.write_int32(1, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn minor(mut self, value: i32) -> Result<Self> {
|
||||||
|
self.builder.write_int32(2, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn patch(mut self, value: i32) -> Result<Self> {
|
||||||
|
self.builder.write_int32(3, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn suffix(mut self, value: &str) -> Result<Self> {
|
||||||
|
self.builder.write_string(4, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn finish(self) -> Result<&'b mut [u8]> {
|
||||||
|
self.builder.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CodeGeneratorRequest<'a>(ProtoAccessor<'a>);
|
||||||
|
|
||||||
|
impl<'a> CodeGeneratorRequest<'a> {
|
||||||
|
pub fn new(data: &'a [u8]) -> Result<Self> {
|
||||||
|
Ok(Self(ProtoAccessor::new(data)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn file_to_generate(&self) -> crate::RepeatedFieldIterator<'a> {
|
||||||
|
self.0.iter_repeated(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parameter(&self) -> Result<&'a str> {
|
||||||
|
let (bytes, _) = self.0.get_value(2)?;
|
||||||
|
str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compiler_version(&self) -> Result<&'a [u8]> {
|
||||||
|
let (bytes, _) = self.0.get_value(3)?;
|
||||||
|
Ok(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn proto_file(&self) -> crate::RepeatedFieldIterator<'a> {
|
||||||
|
self.0.iter_repeated(15)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn source_file_descriptors(&self) -> crate::RepeatedFieldIterator<'a> {
|
||||||
|
self.0.iter_repeated(17)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn builder(buf: &mut [u8]) -> CodeGeneratorRequestBuilder<'_> {
|
||||||
|
CodeGeneratorRequestBuilder {
|
||||||
|
builder: ProtoBuilder::new(buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CodeGeneratorRequestBuilder<'b> {
|
||||||
|
builder: ProtoBuilder<'b>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b> CodeGeneratorRequestBuilder<'b> {
|
||||||
|
pub fn add_file_to_generate(mut self, value: &str) -> Result<Self> {
|
||||||
|
self.builder.write_string(1, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parameter(mut self, value: &str) -> Result<Self> {
|
||||||
|
self.builder.write_string(2, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compiler_version(mut self, data: &[u8]) -> Result<Self> {
|
||||||
|
self.builder.write_bytes(3, data)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_proto_file(mut self, data: &[u8]) -> Result<Self> {
|
||||||
|
self.builder.write_bytes(15, data)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_source_file_descriptor(mut self, data: &[u8]) -> Result<Self> {
|
||||||
|
self.builder.write_bytes(17, data)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn finish(self) -> Result<&'b mut [u8]> {
|
||||||
|
self.builder.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CodeGeneratorResponse<'a>(ProtoAccessor<'a>);
|
||||||
|
|
||||||
|
impl<'a> CodeGeneratorResponse<'a> {
|
||||||
|
pub fn new(data: &'a [u8]) -> Result<Self> {
|
||||||
|
Ok(Self(ProtoAccessor::new(data)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn error(&self) -> Result<&'a str> {
|
||||||
|
let (bytes, _) = self.0.get_value(1)?;
|
||||||
|
str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn supported_features(&self) -> Result<u64> {
|
||||||
|
let (bytes, _) = self.0.get_value(2)?;
|
||||||
|
let (val, _) = crate::read_varint(bytes)?;
|
||||||
|
Ok(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn minimum_edition(&self) -> Result<i32> {
|
||||||
|
let (bytes, _) = self.0.get_value(3)?;
|
||||||
|
let (val, _) = crate::read_varint(bytes)?;
|
||||||
|
Ok(val as i32)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn maximum_edition(&self) -> Result<i32> {
|
||||||
|
let (bytes, _) = self.0.get_value(4)?;
|
||||||
|
let (val, _) = crate::read_varint(bytes)?;
|
||||||
|
Ok(val as i32)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn file(&self) -> crate::RepeatedFieldIterator<'a> {
|
||||||
|
self.0.iter_repeated(15)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn builder(buf: &mut [u8]) -> CodeGeneratorResponseBuilder<'_> {
|
||||||
|
CodeGeneratorResponseBuilder {
|
||||||
|
builder: ProtoBuilder::new(buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CodeGeneratorResponseBuilder<'b> {
|
||||||
|
builder: ProtoBuilder<'b>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b> CodeGeneratorResponseBuilder<'b> {
|
||||||
|
pub fn error(mut self, value: &str) -> Result<Self> {
|
||||||
|
self.builder.write_string(1, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn supported_features(mut self, value: u64) -> Result<Self> {
|
||||||
|
self.builder.write_varint(2, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn minimum_edition(mut self, value: i32) -> Result<Self> {
|
||||||
|
self.builder.write_int32(3, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn maximum_edition(mut self, value: i32) -> Result<Self> {
|
||||||
|
self.builder.write_int32(4, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_file(mut self, data: &[u8]) -> Result<Self> {
|
||||||
|
self.builder.write_bytes(15, data)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn finish(self) -> Result<&'b mut [u8]> {
|
||||||
|
self.builder.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ResponseFile<'a>(ProtoAccessor<'a>);
|
||||||
|
|
||||||
|
impl<'a> ResponseFile<'a> {
|
||||||
|
pub fn new(data: &'a [u8]) -> Result<Self> {
|
||||||
|
Ok(Self(ProtoAccessor::new(data)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(&self) -> Result<&'a str> {
|
||||||
|
let (bytes, _) = self.0.get_value(1)?;
|
||||||
|
str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insertion_point(&self) -> Result<&'a str> {
|
||||||
|
let (bytes, _) = self.0.get_value(2)?;
|
||||||
|
str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn content(&self) -> Result<&'a str> {
|
||||||
|
let (bytes, _) = self.0.get_value(15)?;
|
||||||
|
str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generated_code_info(&self) -> Result<&'a [u8]> {
|
||||||
|
let (bytes, _) = self.0.get_value(16)?;
|
||||||
|
Ok(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn builder(buf: &mut [u8]) -> ResponseFileBuilder<'_> {
|
||||||
|
ResponseFileBuilder {
|
||||||
|
builder: ProtoBuilder::new(buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ResponseFileBuilder<'b> {
|
||||||
|
builder: ProtoBuilder<'b>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b> ResponseFileBuilder<'b> {
|
||||||
|
pub fn name(mut self, value: &str) -> Result<Self> {
|
||||||
|
self.builder.write_string(1, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insertion_point(mut self, value: &str) -> Result<Self> {
|
||||||
|
self.builder.write_string(2, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn content(mut self, value: &str) -> Result<Self> {
|
||||||
|
self.builder.write_string(15, value)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generated_code_info(mut self, data: &[u8]) -> Result<Self> {
|
||||||
|
self.builder.write_bytes(16, data)?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn finish(self) -> Result<&'b mut [u8]> {
|
||||||
|
self.builder.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user