Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse extensions correctly. Allow options more. #242

Merged
merged 1 commit into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 192 additions & 31 deletions pb-rs/src/parser.rs

Large diffs are not rendered by default.

38 changes: 32 additions & 6 deletions pb-rs/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,16 @@ fn get_modules(module: &str, imported: bool, desc: &FileDescriptor) -> String {
.collect()
}

#[derive(Debug, Clone, Default)]
pub struct Extend {
/// The message being extended.
pub name: String,
/// All fields that are being added to the extended message.
pub fields: Vec<Field>,
}

impl Extend {}
Comment on lines +784 to +792
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created this extend struct, which hopefully will be incorporated into the compiler eventually (possibly by me), but we may want to switch to #235 first.


#[derive(Debug, Clone, Default)]
pub struct Message {
pub name: String,
Expand All @@ -796,6 +806,8 @@ pub struct Message {
pub path: PathBuf,
pub import: PathBuf,
pub index: MessageIndex,
/// Allowed extensions for this message, None if no extensions.
pub extensions: Option<Extensions>,
}

impl Message {
Expand Down Expand Up @@ -885,13 +897,10 @@ impl Message {
writeln!(w, "use std::borrow::Cow;")?;
}
}
} else if config.nostd {
if messages
} else if config.nostd && messages
.iter()
.any(|m| m.all_fields().any(|f| (f.typ.has_bytes_and_string())))
{
writeln!(w, "use alloc::borrow::ToOwned;")?;
}
.any(|m| m.all_fields().any(|f| (f.typ.has_bytes_and_string()))) {
writeln!(w, "use alloc::borrow::ToOwned;")?;
}

if config.nostd
Expand Down Expand Up @@ -1476,6 +1485,22 @@ impl RpcService {

pub type RpcGeneratorFunction = Box<dyn Fn(&RpcService, &mut dyn Write) -> Result<()>>;

#[derive(Debug, Clone, Default)]
pub struct Extensions {
pub from: i32,
/// Max number is 536,870,911 (2^29 - 1), as defined in the
/// protobuf docs
pub to: i32,
}

impl Extensions {
/// The max field number that can be used as an extension.
pub fn max() -> i32 {
536870911
}

}
Comment on lines +1488 to +1502
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear this is related, but orthogonal functionality to

extend X { ... }

This is the functionality for

message A {
  ...
  extensions 500 to max
}

message B {
  ...
  extensions 39 to 339
}


#[derive(Debug, Clone, Default)]
pub struct Enumerator {
pub name: String,
Expand Down Expand Up @@ -1841,6 +1866,7 @@ pub struct FileDescriptor {
pub package: String,
pub syntax: Syntax,
pub messages: Vec<Message>,
pub message_extends: Vec<Extend>,
pub enums: Vec<Enumerator>,
pub module: String,
pub rpc_services: Vec<RpcService>,
Expand Down
58 changes: 58 additions & 0 deletions quick-protobuf/examples/pb_rs_v3/owned/a/b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Automatically generated rust module for 'data_types_import.proto' file

#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(unused_imports)]
#![allow(unknown_lints)]
#![allow(clippy::all)]
#![cfg_attr(rustfmt, rustfmt_skip)]


use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result};
use core::convert::{TryFrom, TryInto};
use quick_protobuf::sizeofs::*;
use super::super::*;

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct ImportedMessage {
pub i: bool,
}

impl<'a> MessageRead<'a> for ImportedMessage {
fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
let mut msg = Self::default();
while !r.is_eof() {
match r.next_tag(bytes) {
Ok(8) => msg.i = r.read_bool(bytes)?,
Ok(t) => { r.read_unknown(bytes, t)?; }
Err(e) => return Err(e),
}
}
Ok(msg)
}
}

impl MessageWrite for ImportedMessage {
fn get_size(&self) -> usize {
0
+ if self.i == false { 0 } else { 1 + sizeof_varint(*(&self.i) as u64) }
}

fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
if self.i != false { w.write_with_tag(8, |w| w.write_bool(*&self.i))?; }
Ok(())
}
}


impl TryFrom<&[u8]> for ImportedMessage {
type Error=quick_protobuf::Error;

fn try_from(buf: &[u8]) -> Result<Self> {
let mut reader = BytesReader::from_bytes(&buf);
Ok(ImportedMessage::from_reader(&mut reader, &buf)?)
}
}

2 changes: 2 additions & 0 deletions quick-protobuf/examples/pb_rs_v3/owned/a/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Automatically generated mod.rs
pub mod b;
Loading