-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for Unknown Fields.
- Loading branch information
Showing
12 changed files
with
286 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use anyhow::{bail, Error}; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::Meta; | ||
|
||
#[derive(Clone)] | ||
pub struct Field; | ||
|
||
impl Field { | ||
pub fn new(attrs: &[Meta]) -> Result<Option<Field>, Error> { | ||
if let Some(attr) = attrs.iter().next() { | ||
if attr.path().is_ident("unknown_fields") { | ||
if attrs.len() > 1 { | ||
bail!("invalid unknown_fields attribute(s): {:?}", &attrs[1..0]); | ||
} | ||
return Ok(Some(Field)); | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
pub fn encode(&self, ident: TokenStream) -> TokenStream { | ||
quote! { | ||
for field in #ident.iter() { | ||
::prost::encoding::bytes::encode(field.tag, &field.value, buf); | ||
} | ||
} | ||
} | ||
|
||
pub fn clear(&self, ident: TokenStream) -> TokenStream { | ||
quote!(#ident.clear()) | ||
} | ||
|
||
pub fn encoded_len(&self, ident: TokenStream) -> TokenStream { | ||
quote! { | ||
#ident | ||
.iter() | ||
.map(|field| ::prost::encoding::bytes::encoded_len(field.tag, &field.value)) | ||
.sum::<usize>() | ||
} | ||
} | ||
|
||
pub fn merge(&self, _ident: TokenStream) -> TokenStream { | ||
// Adding unknown fields is handled separately by the decoding logic | ||
quote!(Result::<(), ::prost::DecodeError>::Ok(())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.