Skip to content

Commit

Permalink
Change rust_gen_arrayvec to (rust_max_length)
Browse files Browse the repository at this point in the history
  • Loading branch information
mullr committed Sep 9, 2019
1 parent b4db89e commit d5bf6f7
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 2,070 deletions.
26 changes: 22 additions & 4 deletions pb-rs/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,18 @@ named!(
)
);

named!(
option_key<&str>,
alt_complete!(delimited!(tag!("("), word_ref, tag!(")"))
| word_ref)
);

named!(
key_val<(&str, &str)>,
do_parse!(
tag!("[")
>> many0!(br)
>> key: word_ref
>> key: option_key
>> many0!(br)
>> tag!("=")
>> many0!(br)
Expand Down Expand Up @@ -265,12 +271,12 @@ named!(
.find(|&&(k, _)| k == "deprecated")
.map_or(false, |&(_, v)| str::FromStr::from_str(v)
.expect("Cannot parse Deprecated value")),
gen_arrayvec: key_vals
max_length: key_vals
.iter()
.find(|&&(k, _)| k == "rust_gen_arrayvec")
.find(|&&(k, _)| k == "rust_max_length")
.map(|&(_, v)| v
.parse::<u32>()
.expect("Cannot parse rust_gen_arrayvec value")),
.expect("Cannot parse rust_max_length value")),
})
)
);
Expand Down Expand Up @@ -673,4 +679,16 @@ mod test {
}
}

#[test]
fn test_option_key_plain() {
let s = option_key("test".as_bytes()).unwrap().1;
assert_eq!("test", s);
}

#[test]
fn test_option_key_parens() {
let s = option_key("(test)".as_bytes()).unwrap().1;
assert_eq!("test", s);
}

}
20 changes: 10 additions & 10 deletions pb-rs/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl FieldType {
fn has_lifetime(
&self,
desc: &FileDescriptor,
gen_arrayvec: bool,
max_length: bool,
packed: bool,
ignore: &mut Vec<MessageIndex>,
) -> bool {
Expand All @@ -278,7 +278,7 @@ impl FieldType {
| FieldType::Sfixed32
| FieldType::String_
| FieldType::Bytes_
| FieldType::Float => !gen_arrayvec && packed, // Cow<[M]>
| FieldType::Float => !max_length && packed, // Cow<[M]>
FieldType::Map(ref key, ref value) => {
key.has_lifetime(desc, false, false, ignore)
|| value.has_lifetime(desc, false, false, ignore)
Expand Down Expand Up @@ -434,12 +434,12 @@ pub struct Field {
pub packed: Option<bool>,
pub boxed: bool,
pub deprecated: bool,
pub gen_arrayvec: Option<u32>,
pub max_length: Option<u32>,
}

impl Field {
fn gen_arrayvec(&self) -> bool {
self.gen_arrayvec.is_some()
fn max_length(&self) -> bool {
self.max_length.is_some()
}

fn packed(&self) -> bool {
Expand Down Expand Up @@ -502,11 +502,11 @@ impl Field {
{
writeln!(w, "Option<{}>,", rust_type)?
}
Frequency::Repeated if self.gen_arrayvec.is_some() => writeln!(
Frequency::Repeated if self.max_length.is_some() => writeln!(
w,
"arrayvec::ArrayVec<[{}; {}]>,",
rust_type,
self.gen_arrayvec.unwrap()
self.max_length.unwrap()
)?,
Frequency::Repeated
if self.packed() && self.typ.is_fixed_size() && !config.dont_use_cow =>
Expand Down Expand Up @@ -553,7 +553,7 @@ impl Field {
Frequency::Required | Frequency::Optional => {
writeln!(w, "msg.{} = {},", name, val_cow)?
}
Frequency::Repeated if self.packed() && self.gen_arrayvec.is_some() => {
Frequency::Repeated if self.packed() && self.max_length.is_some() => {
writeln!(
w,
"msg.{} = r.read_packed_arrayvec(bytes, |r, bytes| Ok({}))?,",
Expand Down Expand Up @@ -822,7 +822,7 @@ impl Message {
ignore.push(self.index.clone());
let res = self.all_fields().any(|f| {
f.typ
.has_lifetime(desc, f.gen_arrayvec(), f.packed(), ignore)
.has_lifetime(desc, f.max_length(), f.packed(), ignore)
});
ignore.pop();
res
Expand Down Expand Up @@ -1458,7 +1458,7 @@ impl OneOf {
self.fields.iter().any(|f| {
!f.deprecated
&& f.typ
.has_lifetime(desc, f.gen_arrayvec(), f.packed(), &mut Vec::new())
.has_lifetime(desc, f.max_length(), f.packed(), &mut Vec::new())
})
}

Expand Down
Loading

0 comments on commit d5bf6f7

Please sign in to comment.