Skip to content

Commit

Permalink
Merge branch 'stepancheg:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonlevi authored Feb 26, 2024
2 parents c6f18ef + 16c9dc5 commit da277df
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 50 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## [3.4] - Unreleased

* [Unnecessary copy in print_to_string_internal](https://github.com/stepancheg/rust-protobuf/pull/684)
* [Ignore error of `flush` in `Drop` of `CodedOutputStream`](https://github.com/stepancheg/rust-protobuf/issues/714)
* [Faster `encoded_varint64_len`](https://github.com/stepancheg/rust-protobuf/pull/709)
* [`reserved` keyword in enums](https://github.com/stepancheg/rust-protobuf/pull/712)
* [Set streaming options in pure parser](https://github.com/stepancheg/rust-protobuf/pull/646)

## [3.3.0] - 2023-09-30

Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion protobuf-codegen/LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 19 additions & 0 deletions protobuf-json-mapping/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 Stepan Koltsov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
56 changes: 56 additions & 0 deletions protobuf-parse/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Copyright (c) 2019 Stepan Koltsov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.

-----

The following applies to src/proto/google/

Copyright 2008 Google Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Code generated by the Protocol Buffer compiler is owned by the owner
of the input file used when generating it. This code is not
standalone and requires a support library to be linked with it. This
support library is itself covered by the above license.
30 changes: 25 additions & 5 deletions protobuf-parse/src/pure/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
mod option_resolver;
mod type_resolver;

use protobuf;
use protobuf::descriptor::descriptor_proto::ReservedRange;
use protobuf::descriptor::enum_descriptor_proto::EnumReservedRange;
use protobuf::descriptor::field_descriptor_proto;
use protobuf::descriptor::field_descriptor_proto::Type;
use protobuf::descriptor::FieldDescriptorProto;
Expand Down Expand Up @@ -296,8 +296,8 @@ impl<'a> Resolver<'a> {

for ext in &input.extension_ranges {
let mut extension_range = protobuf::descriptor::descriptor_proto::ExtensionRange::new();
extension_range.set_start(ext.from);
extension_range.set_end(ext.to + 1);
extension_range.set_start(*ext.start());
extension_range.set_end(*ext.end() + 1);
output.extension_range.push(extension_range);
}
for ext in &input.extensions {
Expand All @@ -313,8 +313,8 @@ impl<'a> Resolver<'a> {

for reserved in &input.reserved_nums {
let mut reserved_range = ReservedRange::new();
reserved_range.set_start(reserved.from);
reserved_range.set_end(reserved.to + 1);
reserved_range.set_start(*reserved.start());
reserved_range.set_end(*reserved.end() + 1);
output.reserved_range.push(reserved_range);
}
output.reserved_name = input.reserved_names.clone().into();
Expand All @@ -341,6 +341,14 @@ impl<'a> Resolver<'a> {
.full_name
.to_string(),
);

if input.client_streaming {
output.set_client_streaming(input.client_streaming);
}

if input.server_streaming {
output.set_server_streaming(input.server_streaming);
}
Ok(output)
}

Expand Down Expand Up @@ -526,6 +534,18 @@ impl<'a> Resolver<'a> {
.iter()
.map(|v| self.enum_value(scope, &v))
.collect::<Result<_, _>>()?;

for reserved in &input.reserved_nums {
let mut reserved_range = EnumReservedRange::new();
reserved_range.set_start(*reserved.start());
// EnumReservedRange is inclusive, not like ExtensionRange and
// ReservedRange, which are exclusive.
reserved_range.set_end(*reserved.end());
output.reserved_range.push(reserved_range);
}

output.reserved_name = input.reserved_names.clone().into();

Ok(output)
}

Expand Down
20 changes: 7 additions & 13 deletions protobuf-parse/src/pure/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::fmt;
use std::fmt::Write;
use std::ops::Deref;
use std::ops::RangeInclusive;

use indexmap::IndexMap;
use protobuf::reflect::ReflectValueBox;
Expand Down Expand Up @@ -213,15 +214,6 @@ pub(crate) enum FieldOrOneOf {
OneOf(OneOf),
}

/// Extension range
#[derive(Default, Debug, Eq, PartialEq, Copy, Clone)]
pub(crate) struct FieldNumberRange {
/// First number
pub from: i32,
/// Inclusive
pub to: i32,
}

/// A protobuf message
#[derive(Debug, Clone, Default)]
pub(crate) struct Message {
Expand All @@ -230,9 +222,7 @@ pub(crate) struct Message {
/// Message fields and oneofs
pub fields: Vec<WithLoc<FieldOrOneOf>>,
/// Message reserved numbers
///
/// TODO: use RangeInclusive once stable
pub reserved_nums: Vec<FieldNumberRange>,
pub reserved_nums: Vec<RangeInclusive<i32>>,
/// Message reserved names
pub reserved_names: Vec<String>,
/// Nested messages
Expand All @@ -242,7 +232,7 @@ pub(crate) struct Message {
/// Non-builtin options
pub options: Vec<ProtobufOption>,
/// Extension field numbers
pub extension_ranges: Vec<FieldNumberRange>,
pub extension_ranges: Vec<RangeInclusive<i32>>,
/// Extensions
pub extensions: Vec<WithLoc<Extension>>,
}
Expand Down Expand Up @@ -318,6 +308,10 @@ pub(crate) struct Enumeration {
pub values: Vec<EnumValue>,
/// enum options
pub options: Vec<ProtobufOption>,
/// enum reserved numbers
pub reserved_nums: Vec<RangeInclusive<i32>>,
/// enum reserved names
pub reserved_names: Vec<String>,
}

/// A OneOf
Expand Down
55 changes: 39 additions & 16 deletions protobuf-parse/src/pure/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ops::RangeInclusive;
use std::str;

use protobuf_support::lexer::int;
Expand All @@ -21,7 +22,6 @@ use crate::pure::model::EnumValue;
use crate::pure::model::Enumeration;
use crate::pure::model::Extension;
use crate::pure::model::Field;
use crate::pure::model::FieldNumberRange;
use crate::pure::model::FieldOrOneOf;
use crate::pure::model::FieldType;
use crate::pure::model::FileDescriptor;
Expand Down Expand Up @@ -264,12 +264,12 @@ impl MessageBodyParseMode {
#[derive(Default)]
pub(crate) struct MessageBody {
pub fields: Vec<WithLoc<FieldOrOneOf>>,
pub reserved_nums: Vec<FieldNumberRange>,
pub reserved_nums: Vec<RangeInclusive<i32>>,
pub reserved_names: Vec<String>,
pub messages: Vec<WithLoc<Message>>,
pub enums: Vec<WithLoc<Enumeration>>,
pub options: Vec<ProtobufOption>,
pub extension_ranges: Vec<FieldNumberRange>,
pub extension_ranges: Vec<RangeInclusive<i32>>,
pub extensions: Vec<WithLoc<Extension>>,
}

Expand Down Expand Up @@ -775,7 +775,7 @@ impl<'a> Parser<'a> {
// Extensions

// range = intLit [ "to" ( intLit | "max" ) ]
fn next_range(&mut self) -> anyhow::Result<FieldNumberRange> {
fn next_range(&mut self) -> anyhow::Result<RangeInclusive<i32>> {
let from = self.next_field_number()?;
let to = if self.tokenizer.next_ident_if_eq("to")? {
if self.tokenizer.next_ident_if_eq("max")? {
Expand All @@ -786,11 +786,11 @@ impl<'a> Parser<'a> {
} else {
from
};
Ok(FieldNumberRange { from, to })
Ok(from..=to)
}

// ranges = range { "," range }
fn next_ranges(&mut self) -> anyhow::Result<Vec<FieldNumberRange>> {
fn next_ranges(&mut self) -> anyhow::Result<Vec<RangeInclusive<i32>>> {
let mut ranges = Vec::new();
ranges.push(self.next_range()?);
while self.tokenizer.next_symbol_if_eq(',')? {
Expand All @@ -800,7 +800,7 @@ impl<'a> Parser<'a> {
}

// extensions = "extensions" ranges ";"
fn next_extensions_opt(&mut self) -> anyhow::Result<Option<Vec<FieldNumberRange>>> {
fn next_extensions_opt(&mut self) -> anyhow::Result<Option<Vec<RangeInclusive<i32>>>> {
if self.tokenizer.next_ident_if_eq("extensions")? {
Ok(Some(self.next_ranges()?))
} else {
Expand All @@ -815,7 +815,7 @@ impl<'a> Parser<'a> {
// fieldNames = fieldName { "," fieldName }
fn next_reserved_opt(
&mut self,
) -> anyhow::Result<Option<(Vec<FieldNumberRange>, Vec<String>)>> {
) -> anyhow::Result<Option<(Vec<RangeInclusive<i32>>, Vec<String>)>> {
if self.tokenizer.next_ident_if_eq("reserved")? {
let (ranges, names) = if let &Token::StrLit(..) = self.tokenizer.lookahead_some()? {
let mut names = Vec::new();
Expand Down Expand Up @@ -886,7 +886,7 @@ impl<'a> Parser<'a> {
}

// enum = "enum" enumName enumBody
// enumBody = "{" { option | enumField | emptyStatement } "}"
// enumBody = "{" { option | enumField | emptyStatement | reserved } "}"
fn next_enum_opt(&mut self) -> anyhow::Result<Option<WithLoc<Enumeration>>> {
let loc = self.tokenizer.lookahead_loc();

Expand All @@ -895,6 +895,8 @@ impl<'a> Parser<'a> {

let mut values = Vec::new();
let mut options = Vec::new();
let mut reserved_nums = Vec::new();
let mut reserved_names = Vec::new();

self.tokenizer.next_symbol_expect_eq('{', "enum")?;
while self.tokenizer.lookahead_if_symbol()? != Some('}') {
Expand All @@ -903,6 +905,12 @@ impl<'a> Parser<'a> {
continue;
}

if let Some((field_nums, field_names)) = self.next_reserved_opt()? {
reserved_nums.extend(field_nums);
reserved_names.extend(field_names);
continue;
}

if let Some(o) = self.next_option_opt()? {
options.push(o);
continue;
Expand All @@ -915,6 +923,8 @@ impl<'a> Parser<'a> {
name,
values,
options,
reserved_nums,
reserved_names,
};
Ok(Some(WithLoc {
loc,
Expand Down Expand Up @@ -1470,7 +1480,7 @@ mod test {
}

#[test]
fn test_reserved() {
fn test_reserved_in_message() {
let msg = r#"message Sample {
reserved 4, 15, 17 to 20, 30;
reserved "foo", "bar";
Expand All @@ -1480,12 +1490,7 @@ mod test {

let mess = parse_opt(msg, |p| p.next_message_opt());
assert_eq!(
vec![
FieldNumberRange { from: 4, to: 4 },
FieldNumberRange { from: 15, to: 15 },
FieldNumberRange { from: 17, to: 20 },
FieldNumberRange { from: 30, to: 30 }
],
vec![4..=4, 15..=15, 17..=20, 30..=30,],
mess.t.reserved_nums
);
assert_eq!(
Expand All @@ -1495,6 +1500,24 @@ mod test {
assert_eq!(2, mess.t.fields.len());
}

#[test]
fn test_reserved_in_enum() {
let msg = r#"enum Sample {
reserved 4, 15, 17 to 20, 30;
reserved "foo", "bar";
}"#;

let enum_ = parse_opt(msg, |p| p.next_enum_opt());
assert_eq!(
vec![4..=4, 15..=15, 17..=20, 30..=30,],
enum_.t.reserved_nums
);
assert_eq!(
vec!["foo".to_string(), "bar".to_string()],
enum_.t.reserved_names
);
}

#[test]
fn test_default_value_int() {
let msg = r#"message Sample {
Expand Down
19 changes: 19 additions & 0 deletions protobuf-support/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 Stepan Koltsov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit da277df

Please sign in to comment.