Refactor crate into multiple subcrates
This commit is contained in:
@@ -0,0 +1 @@
|
||||
data
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "roto-benches"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
roto-runtime = { path = "../runtime" }
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
log = "0.4"
|
||||
env_logger = "0.11"
|
||||
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = { version = "0.5", features = ["html_reports"] }
|
||||
|
||||
[[bench]]
|
||||
name = "hackers_bench"
|
||||
harness = false
|
||||
@@ -27,7 +27,7 @@
|
||||
//! - `iterate` — counting repeated fields at different nesting depths
|
||||
|
||||
use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
|
||||
use roto::hackers::{Campaign, Hacker, Operation, Worm};
|
||||
use roto_benches::hackers::{Campaign, Hacker, Operation, Worm};
|
||||
use std::hint::black_box;
|
||||
|
||||
// =============================================================================
|
||||
@@ -0,0 +1,478 @@
|
||||
//! Generates Hackers-themed benchmark proto binaries using the roto builder API.
|
||||
//!
|
||||
//! Run this once to create the data files that `hackers_bench` loads:
|
||||
//!
|
||||
//! ```sh
|
||||
//! cargo run --release --bin gen_bench_data -- --preset tiny
|
||||
//! cargo run --release --bin gen_bench_data -- --preset small
|
||||
//! cargo run --release --bin gen_bench_data -- --preset medium
|
||||
//! cargo run --release --bin gen_bench_data -- --preset large
|
||||
//! cargo run --release --bin gen_bench_data -- --preset huge
|
||||
//!
|
||||
//! # Custom: ~50 MB — 500 ops × 100 KB stolen_data each
|
||||
//! cargo run --release --bin gen_bench_data -- \
|
||||
//! --ops 500 --stolen-kb 100 --output data/bench/50mb.pb
|
||||
//! ```
|
||||
//!
|
||||
//! Files land in `data/bench/` by default.
|
||||
|
||||
use clap::Parser;
|
||||
use roto_benches::hackers::{
|
||||
CampaignBuilder, ConnectionBuilder, HackerBuilder, OperationBuilder, ToolBuilder, WormBuilder,
|
||||
};
|
||||
use std::io::{self, Write};
|
||||
use std::path::Path;
|
||||
|
||||
// =============================================================================
|
||||
// CLI
|
||||
// =============================================================================
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(
|
||||
name = "gen_bench_data",
|
||||
about = "Generate Hackers-themed proto binaries for benchmarks"
|
||||
)]
|
||||
struct Args {
|
||||
/// Output file. Defaults to data/bench/<preset>.pb when --preset is used.
|
||||
#[arg(short, long)]
|
||||
output: Option<String>,
|
||||
|
||||
/// Named size preset: tiny | small | medium | large | huge
|
||||
#[arg(short, long)]
|
||||
preset: Option<String>,
|
||||
|
||||
/// Number of Operation messages.
|
||||
#[arg(long, default_value_t = 100)]
|
||||
ops: usize,
|
||||
|
||||
/// Kilobytes of random stolen_data padding per Operation.
|
||||
#[arg(long, default_value_t = 0)]
|
||||
stolen_kb: usize,
|
||||
|
||||
/// Hacker crew members per Operation.
|
||||
#[arg(long, default_value_t = 3)]
|
||||
crew: usize,
|
||||
|
||||
/// RNG seed.
|
||||
#[arg(long, default_value_t = 42)]
|
||||
seed: u64,
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Minimal xorshift64 RNG
|
||||
// =============================================================================
|
||||
|
||||
struct Rng(u64);
|
||||
|
||||
impl Rng {
|
||||
fn new(seed: u64) -> Self {
|
||||
Self(if seed == 0 { 0xdeadbeef_cafebabe } else { seed })
|
||||
}
|
||||
fn next(&mut self) -> u64 {
|
||||
self.0 ^= self.0 << 13;
|
||||
self.0 ^= self.0 >> 7;
|
||||
self.0 ^= self.0 << 17;
|
||||
self.0
|
||||
}
|
||||
fn below(&mut self, n: usize) -> usize {
|
||||
(self.next() as usize) % n
|
||||
}
|
||||
fn range(&mut self, lo: u64, hi: u64) -> u64 {
|
||||
lo + self.next() % (hi - lo)
|
||||
}
|
||||
fn bool(&mut self) -> bool {
|
||||
self.next() & 1 == 0
|
||||
}
|
||||
fn pick<'a, T>(&mut self, s: &'a [T]) -> &'a T {
|
||||
&s[self.below(s.len())]
|
||||
}
|
||||
fn bytes(&mut self, n: usize) -> Vec<u8> {
|
||||
(0..n).map(|_| self.next() as u8).collect()
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Flavour text
|
||||
// =============================================================================
|
||||
|
||||
const HANDLES: &[&str] = &[
|
||||
"Zero Cool",
|
||||
"Acid Burn",
|
||||
"Phantom Phreak",
|
||||
"Cereal Killer",
|
||||
"Lord Nikon",
|
||||
"The Plague",
|
||||
"Crash Override",
|
||||
];
|
||||
const REAL_NAMES: &[&str] = &[
|
||||
"Dade Murphy",
|
||||
"Kate Libby",
|
||||
"Richard Gill",
|
||||
"Emmanuel Goldstein",
|
||||
"Paul Cook",
|
||||
"Eugene Belford",
|
||||
];
|
||||
const EXPLOITS: &[&str] = &[
|
||||
"buffer overflow",
|
||||
"stack smash",
|
||||
"heap spray",
|
||||
"race condition",
|
||||
"SQL injection",
|
||||
"CSRF",
|
||||
"XSS",
|
||||
"RCE",
|
||||
"privesc",
|
||||
"kernel panic",
|
||||
];
|
||||
const TOOL_NAMES: &[&str] = &[
|
||||
"nmap",
|
||||
"metasploit",
|
||||
"netcat",
|
||||
"tcpdump",
|
||||
"Wireshark",
|
||||
"sqlmap",
|
||||
"Burp Suite",
|
||||
"hashcat",
|
||||
"john",
|
||||
];
|
||||
const CORPS: &[&str] = &[
|
||||
"ELLINGSON MINERAL",
|
||||
"Cyberdelia",
|
||||
"The Gibson",
|
||||
"Prism BBS",
|
||||
"Elite BBS",
|
||||
"CRT Systems",
|
||||
];
|
||||
const LOG_LINES: &[&str] = &[
|
||||
"Hack the planet!",
|
||||
"Mess with the best, die like the rest.",
|
||||
"I'm in.",
|
||||
"They're tracing us.",
|
||||
"It's a Unix system! I know this!",
|
||||
"You are elite.",
|
||||
"Garbage file accessed.",
|
||||
];
|
||||
const WORM_TARGETS: &[&str] = &[
|
||||
"ELLINGSON MINERAL",
|
||||
"Cyberdelia",
|
||||
"The Gibson",
|
||||
"Prism",
|
||||
"CRT BBS",
|
||||
];
|
||||
const OP_NAMES: &[&str] = &[
|
||||
"OPERATION HACK THE PLANET",
|
||||
"GIBSON BREACH",
|
||||
"ELLINGSON STING",
|
||||
"WORM UNLEASHED",
|
||||
"PHANTOM ACCESS",
|
||||
"DA VINCI",
|
||||
];
|
||||
|
||||
// =============================================================================
|
||||
// Message builders using the generated roto::hackers API
|
||||
// =============================================================================
|
||||
|
||||
fn gen_tool(rng: &mut Rng) -> Vec<u8> {
|
||||
let payload_n = rng.range(8, 64) as usize;
|
||||
let payload = rng.bytes(payload_n);
|
||||
let version = format!("{}.{}", rng.range(1, 9), rng.range(0, 99));
|
||||
let mut buf = vec![0u8; 512];
|
||||
ToolBuilder::builder(&mut buf)
|
||||
.name(*rng.pick(TOOL_NAMES))
|
||||
.unwrap()
|
||||
.version(&version)
|
||||
.unwrap()
|
||||
.payload(&payload)
|
||||
.unwrap()
|
||||
.is_active(rng.bool() as u64)
|
||||
.unwrap()
|
||||
.exploit_count(rng.range(0, 50) as i32)
|
||||
.unwrap()
|
||||
.finish()
|
||||
.unwrap()
|
||||
.to_vec()
|
||||
}
|
||||
|
||||
fn gen_connection(rng: &mut Rng) -> Vec<u8> {
|
||||
let host = format!("192.168.{}.{}", rng.range(1, 254), rng.range(1, 254));
|
||||
let session_key = rng.bytes(32);
|
||||
let mut buf = vec![0u8; 256];
|
||||
ConnectionBuilder::builder(&mut buf)
|
||||
.host(&host)
|
||||
.unwrap()
|
||||
.port(rng.range(1024, 65535) as i32)
|
||||
.unwrap()
|
||||
.encrypted(rng.bool() as u64)
|
||||
.unwrap()
|
||||
.bandwidth_bps(rng.range(1200, 1_000_000_000))
|
||||
.unwrap()
|
||||
.session_key(&session_key)
|
||||
.unwrap()
|
||||
.finish()
|
||||
.unwrap()
|
||||
.to_vec()
|
||||
}
|
||||
|
||||
fn gen_hacker(rng: &mut Rng) -> Vec<u8> {
|
||||
let tools: Vec<Vec<u8>> = (0..rng.range(1, 4)).map(|_| gen_tool(rng)).collect();
|
||||
let connection = gen_connection(rng);
|
||||
// Float (skill_level) is written as raw bytes by the generated builder
|
||||
let skill_bits = (rng.range(10, 100) as f32 / 10.0).to_bits().to_le_bytes();
|
||||
let handle = *rng.pick(HANDLES);
|
||||
let real_name = *rng.pick(REAL_NAMES);
|
||||
let n_exploits = rng.range(2, 5) as usize;
|
||||
let exploits: Vec<&str> = (0..n_exploits).map(|_| *rng.pick(EXPLOITS)).collect();
|
||||
let crew_id = rng.next();
|
||||
|
||||
let mut buf = vec![0u8; 8 * 1024];
|
||||
let mut b = HackerBuilder::builder(&mut buf)
|
||||
.handle(handle)
|
||||
.unwrap()
|
||||
.real_name(real_name)
|
||||
.unwrap()
|
||||
.age(rng.range(16, 35) as i32)
|
||||
.unwrap()
|
||||
.skill_level(&skill_bits)
|
||||
.unwrap()
|
||||
.is_elite(rng.bool() as u64)
|
||||
.unwrap()
|
||||
.crew_id(crew_id)
|
||||
.unwrap();
|
||||
for e in &exploits {
|
||||
b = b.exploits(e).unwrap();
|
||||
}
|
||||
for t in &tools {
|
||||
b = b.tools(t).unwrap();
|
||||
}
|
||||
b.active_connection(&connection)
|
||||
.unwrap()
|
||||
.finish()
|
||||
.unwrap()
|
||||
.to_vec()
|
||||
}
|
||||
|
||||
fn gen_worm(rng: &mut Rng) -> Vec<u8> {
|
||||
let payload = rng.bytes(64);
|
||||
let name = format!("da_vinci.{}", rng.range(1, 99));
|
||||
let n_targets = rng.range(1, 4) as usize;
|
||||
let targets: Vec<&str> = (0..n_targets).map(|_| *rng.pick(WORM_TARGETS)).collect();
|
||||
|
||||
let mut buf = vec![0u8; 1024];
|
||||
let mut b = WormBuilder::builder(&mut buf)
|
||||
.name(&name)
|
||||
.unwrap()
|
||||
.variant(rng.range(1, 5) as i32)
|
||||
.unwrap()
|
||||
.size_bytes(rng.range(1024, 10_000_000))
|
||||
.unwrap()
|
||||
.payload(&payload)
|
||||
.unwrap()
|
||||
.polymorphic(rng.bool() as u64)
|
||||
.unwrap();
|
||||
for t in &targets {
|
||||
b = b.targets(t).unwrap();
|
||||
}
|
||||
b.finish().unwrap().to_vec()
|
||||
}
|
||||
|
||||
fn gen_operation(rng: &mut Rng, crew_count: usize, stolen_bytes: usize) -> Vec<u8> {
|
||||
let crew: Vec<Vec<u8>> = (0..crew_count).map(|_| gen_hacker(rng)).collect();
|
||||
let worm = gen_worm(rng);
|
||||
let stolen = if stolen_bytes > 0 {
|
||||
rng.bytes(stolen_bytes)
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
let codename = *rng.pick(OP_NAMES);
|
||||
let corp = *rng.pick(CORPS);
|
||||
let ts = 810_000_000u64 + rng.range(0, 10_000_000);
|
||||
let n_logs = rng.range(2, 6) as usize;
|
||||
let logs: Vec<&str> = (0..n_logs).map(|_| *rng.pick(LOG_LINES)).collect();
|
||||
|
||||
// Operation buffer: crew + worm + stolen_data + small overhead
|
||||
let crew_size: usize = crew.iter().map(|h| h.len() + 5).sum();
|
||||
let buf_size = crew_size + worm.len() + stolen_bytes + 1024;
|
||||
let mut buf = vec![0u8; buf_size];
|
||||
|
||||
let mut b = OperationBuilder::builder(&mut buf)
|
||||
.codename(codename)
|
||||
.unwrap()
|
||||
.target_corp(corp)
|
||||
.unwrap()
|
||||
.timestamp(ts)
|
||||
.unwrap()
|
||||
.successful(rng.bool() as u64)
|
||||
.unwrap();
|
||||
if stolen_bytes > 0 {
|
||||
b = b.stolen_data(&stolen).unwrap();
|
||||
}
|
||||
for h in &crew {
|
||||
b = b.crew(h).unwrap();
|
||||
}
|
||||
b = b.worm(&worm).unwrap();
|
||||
for l in &logs {
|
||||
b = b.log_entries(l).unwrap();
|
||||
}
|
||||
b.severity(rng.range(1, 10) as i32)
|
||||
.unwrap()
|
||||
.finish()
|
||||
.unwrap()
|
||||
.to_vec()
|
||||
}
|
||||
|
||||
fn gen_campaign(
|
||||
rng: &mut Rng,
|
||||
op_count: usize,
|
||||
crew_per_op: usize,
|
||||
stolen_bytes: usize,
|
||||
) -> Vec<u8> {
|
||||
let ops: Vec<Vec<u8>> = (0..op_count)
|
||||
.map(|i| {
|
||||
if i > 0 && i % 100 == 0 {
|
||||
eprintln!(
|
||||
" {i}/{op_count} operations ({:.1} MB in ops so far)…",
|
||||
i * (stolen_bytes + 10_000) / 1_000_000
|
||||
);
|
||||
}
|
||||
gen_operation(rng, crew_per_op, stolen_bytes)
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Pre-compute campaign buffer size: for each op the wire encoding is
|
||||
// tag(1B) + varint_length(1-5B) + op_bytes
|
||||
let ops_wire_size: usize = ops
|
||||
.iter()
|
||||
.map(|o| 1 + varint_len(o.len() as u64) + o.len())
|
||||
.sum();
|
||||
let mut buf = vec![0u8; ops_wire_size + 64];
|
||||
|
||||
let mut b = CampaignBuilder::builder(&mut buf)
|
||||
.name("HACK THE PLANET CAMPAIGN")
|
||||
.unwrap();
|
||||
for op in &ops {
|
||||
b = b.operations(op).unwrap();
|
||||
}
|
||||
b.total_bytes_stolen((stolen_bytes * op_count) as u64)
|
||||
.unwrap()
|
||||
.finish()
|
||||
.unwrap()
|
||||
.to_vec()
|
||||
}
|
||||
|
||||
/// Number of bytes needed to encode `v` as a varint.
|
||||
fn varint_len(mut v: u64) -> usize {
|
||||
let mut n = 1usize;
|
||||
while v >= 128 {
|
||||
v >>= 7;
|
||||
n += 1;
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Preset table
|
||||
// =============================================================================
|
||||
|
||||
struct Preset {
|
||||
ops: usize,
|
||||
crew: usize,
|
||||
stolen_kb: usize,
|
||||
}
|
||||
|
||||
fn resolve(name: &str) -> Option<Preset> {
|
||||
match name {
|
||||
// ops crew stolen_kb approx size
|
||||
"tiny" => Some(Preset {
|
||||
ops: 1,
|
||||
crew: 1,
|
||||
stolen_kb: 0,
|
||||
}), // ~400 B
|
||||
"small" => Some(Preset {
|
||||
ops: 20,
|
||||
crew: 3,
|
||||
stolen_kb: 0,
|
||||
}), // ~25 KB
|
||||
"medium" => Some(Preset {
|
||||
ops: 2_000,
|
||||
crew: 3,
|
||||
stolen_kb: 0,
|
||||
}), // ~2 MB
|
||||
"large" => Some(Preset {
|
||||
ops: 200,
|
||||
crew: 3,
|
||||
stolen_kb: 500,
|
||||
}), // ~100 MB
|
||||
"huge" => Some(Preset {
|
||||
ops: 1_000,
|
||||
crew: 3,
|
||||
stolen_kb: 500,
|
||||
}), // ~500 MB
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// main
|
||||
// =============================================================================
|
||||
|
||||
fn main() {
|
||||
let mut args = Args::parse();
|
||||
|
||||
if let Some(ref name) = args.preset.clone() {
|
||||
match resolve(name) {
|
||||
Some(p) => {
|
||||
args.ops = p.ops;
|
||||
args.crew = p.crew;
|
||||
args.stolen_kb = p.stolen_kb;
|
||||
}
|
||||
None => {
|
||||
eprintln!("Unknown preset '{name}'. Valid: tiny, small, medium, large, huge");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let stolen_bytes = args.stolen_kb * 1024;
|
||||
let approx_mb = args.ops * (700 + args.crew * 300 + stolen_bytes) / 1_000_000;
|
||||
eprintln!(
|
||||
"Generating: {} ops × {} crew, {} KB stolen_data each → ~{} MB",
|
||||
args.ops, args.crew, args.stolen_kb, approx_mb
|
||||
);
|
||||
|
||||
let mut rng = Rng::new(args.seed);
|
||||
let data = gen_campaign(&mut rng, args.ops, args.crew, stolen_bytes);
|
||||
|
||||
eprintln!(
|
||||
"Generated {} bytes ({:.2} MB)",
|
||||
data.len(),
|
||||
data.len() as f64 / 1_000_000.0
|
||||
);
|
||||
|
||||
// Default output: data/bench/<preset>.pb, or stdout if no output and no preset
|
||||
let out_path = args.output.clone().or_else(|| {
|
||||
args.preset
|
||||
.as_ref()
|
||||
.map(|p| format!("benches/data/bench/{}.pb", p))
|
||||
});
|
||||
|
||||
match out_path {
|
||||
Some(ref path) => {
|
||||
if let Some(parent) = Path::new(path).parent() {
|
||||
if !parent.as_os_str().is_empty() {
|
||||
std::fs::create_dir_all(parent).unwrap_or_else(|e| eprintln!("Warning: {e}"));
|
||||
}
|
||||
}
|
||||
std::fs::write(path, &data).unwrap_or_else(|e| {
|
||||
eprintln!("Error writing {path}: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
eprintln!("Saved to {path}");
|
||||
}
|
||||
None => {
|
||||
io::stdout().write_all(&data).unwrap_or_else(|e| {
|
||||
eprintln!("Error: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod protobuf;
|
||||
@@ -0,0 +1 @@
|
||||
pub mod plugin;
|
||||
@@ -0,0 +1,600 @@
|
||||
// @generated by protoc-gen-roto — do not edit
|
||||
#![allow(unused_imports)]
|
||||
|
||||
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 fn raw_fields(&self) -> crate::RawFieldIterator<'a> {
|
||||
self.accessor.raw_fields()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct VersionBuilder<'b> {
|
||||
builder: crate::ProtoBuilder<'b>,
|
||||
major_written: bool,
|
||||
minor_written: bool,
|
||||
patch_written: bool,
|
||||
suffix_written: bool,
|
||||
}
|
||||
|
||||
impl<'b> VersionBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> VersionBuilder<'_> {
|
||||
VersionBuilder {
|
||||
builder: crate::ProtoBuilder::new(buf),
|
||||
major_written: false,
|
||||
minor_written: false,
|
||||
patch_written: false,
|
||||
suffix_written: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn major(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(1, value)?;
|
||||
self.major_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn minor(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(2, value)?;
|
||||
self.minor_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn patch(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(3, value)?;
|
||||
self.patch_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn suffix(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(4, value)?;
|
||||
self.suffix_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &Version<'_>) -> crate::Result<Self> {
|
||||
for item in msg.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
1 => self.major_written,
|
||||
2 => self.minor_written,
|
||||
3 => self.patch_written,
|
||||
4 => self.suffix_written,
|
||||
_ => false,
|
||||
};
|
||||
if !is_written {
|
||||
self.builder.write_raw(raw_bytes)?;
|
||||
}
|
||||
}
|
||||
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 fn raw_fields(&self) -> crate::RawFieldIterator<'a> {
|
||||
self.accessor.raw_fields()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct CodeGeneratorRequestBuilder<'b> {
|
||||
builder: crate::ProtoBuilder<'b>,
|
||||
file_to_generate_written: bool,
|
||||
parameter_written: bool,
|
||||
proto_file_written: bool,
|
||||
source_file_descriptors_written: bool,
|
||||
compiler_version_written: bool,
|
||||
}
|
||||
|
||||
impl<'b> CodeGeneratorRequestBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> CodeGeneratorRequestBuilder<'_> {
|
||||
CodeGeneratorRequestBuilder {
|
||||
builder: crate::ProtoBuilder::new(buf),
|
||||
file_to_generate_written: false,
|
||||
parameter_written: false,
|
||||
proto_file_written: false,
|
||||
source_file_descriptors_written: false,
|
||||
compiler_version_written: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_to_generate(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(1, value)?;
|
||||
self.file_to_generate_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn parameter(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(2, value)?;
|
||||
self.parameter_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn proto_file(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(15, value)?;
|
||||
self.proto_file_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn source_file_descriptors(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(17, value)?;
|
||||
self.source_file_descriptors_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn compiler_version(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(3, value)?;
|
||||
self.compiler_version_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &CodeGeneratorRequest<'_>) -> crate::Result<Self> {
|
||||
for item in msg.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
1 => self.file_to_generate_written,
|
||||
2 => self.parameter_written,
|
||||
15 => self.proto_file_written,
|
||||
17 => self.source_file_descriptors_written,
|
||||
3 => self.compiler_version_written,
|
||||
_ => false,
|
||||
};
|
||||
if !is_written {
|
||||
self.builder.write_raw(raw_bytes)?;
|
||||
}
|
||||
}
|
||||
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 fn raw_fields(&self) -> crate::RawFieldIterator<'a> {
|
||||
self.accessor.raw_fields()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct CodeGeneratorResponseBuilder<'b> {
|
||||
builder: crate::ProtoBuilder<'b>,
|
||||
error_written: bool,
|
||||
supported_features_written: bool,
|
||||
minimum_edition_written: bool,
|
||||
maximum_edition_written: bool,
|
||||
file_written: bool,
|
||||
}
|
||||
|
||||
impl<'b> CodeGeneratorResponseBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> CodeGeneratorResponseBuilder<'_> {
|
||||
CodeGeneratorResponseBuilder {
|
||||
builder: crate::ProtoBuilder::new(buf),
|
||||
error_written: false,
|
||||
supported_features_written: false,
|
||||
minimum_edition_written: false,
|
||||
maximum_edition_written: false,
|
||||
file_written: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn error(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(1, value)?;
|
||||
self.error_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn supported_features(mut self, value: u64) -> crate::Result<Self> {
|
||||
self.builder.write_varint(2, value)?;
|
||||
self.supported_features_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn minimum_edition(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(3, value)?;
|
||||
self.minimum_edition_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn maximum_edition(mut self, value: i32) -> crate::Result<Self> {
|
||||
self.builder.write_int32(4, value)?;
|
||||
self.maximum_edition_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn file(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(15, value)?;
|
||||
self.file_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &CodeGeneratorResponse<'_>) -> crate::Result<Self> {
|
||||
for item in msg.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
1 => self.error_written,
|
||||
2 => self.supported_features_written,
|
||||
3 => self.minimum_edition_written,
|
||||
4 => self.maximum_edition_written,
|
||||
15 => self.file_written,
|
||||
_ => false,
|
||||
};
|
||||
if !is_written {
|
||||
self.builder.write_raw(raw_bytes)?;
|
||||
}
|
||||
}
|
||||
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 fn raw_fields(&self) -> crate::RawFieldIterator<'a> {
|
||||
self.accessor.raw_fields()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct FileBuilder<'b> {
|
||||
builder: crate::ProtoBuilder<'b>,
|
||||
name_written: bool,
|
||||
insertion_point_written: bool,
|
||||
content_written: bool,
|
||||
generated_code_info_written: bool,
|
||||
}
|
||||
|
||||
impl<'b> FileBuilder<'b> {
|
||||
pub fn builder(buf: &mut [u8]) -> FileBuilder<'_> {
|
||||
FileBuilder {
|
||||
builder: crate::ProtoBuilder::new(buf),
|
||||
name_written: false,
|
||||
insertion_point_written: false,
|
||||
content_written: false,
|
||||
generated_code_info_written: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(1, value)?;
|
||||
self.name_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn insertion_point(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(2, value)?;
|
||||
self.insertion_point_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn content(mut self, value: &str) -> crate::Result<Self> {
|
||||
self.builder.write_string(15, value)?;
|
||||
self.content_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn generated_code_info(mut self, value: &[u8]) -> crate::Result<Self> {
|
||||
self.builder.write_bytes(16, value)?;
|
||||
self.generated_code_info_written = true;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn with(mut self, msg: &File<'_>) -> crate::Result<Self> {
|
||||
for item in msg.raw_fields() {
|
||||
let (field_number, raw_bytes) = item?;
|
||||
let is_written = match field_number {
|
||||
1 => self.name_written,
|
||||
2 => self.insertion_point_written,
|
||||
15 => self.content_written,
|
||||
16 => self.generated_code_info_written,
|
||||
_ => false,
|
||||
};
|
||||
if !is_written {
|
||||
self.builder.write_raw(raw_bytes)?;
|
||||
}
|
||||
}
|
||||
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;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1 @@
|
||||
pub mod hackers;
|
||||
Reference in New Issue
Block a user