let tag = match tag.or(inferred_tag) {
Some(tag) => tag,
None => bail!("missing tag attribute"),
};
let has_default = default.is_some(); let default = default.map_or_else(
|| Ok(DefaultValue::new(&ty)),
|lit| DefaultValue::from_lit(&ty, lit),
)?;
let kind = match (label, packed, has_default) {
(None, Some(true), _)
| (Some(Label::Optional), Some(true), _)
| (Some(Label::Required), Some(true), _) => {
bail!("packed attribute may only be applied to repeated fields");
}
(Some(Label::Repeated), Some(true), _) if !ty.is_numeric() => {
bail!("packed attribute may only be applied to numeric types");
}
(Some(Label::Repeated), _, true) => {
bail!("repeated fields may not have a default value");
}
/// Returns an expression which evaluates to the result of merging a decoded /// scalar value into the field. pubfn merge(&self, ident: TokenStream) -> TokenStream { let module = self.ty.module(); let merge_fn = matchself.kind {
Kind::Plain(..) | Kind::Optional(..) | Kind::Required(..) => quote!(merge),
Kind::Repeated | Kind::Packed => quote!(merge_repeated),
}; let merge_fn = quote!(::prost::encoding::#module::#merge_fn);
/// Returns an expression which evaluates to the encoded length of the field. pubfn encoded_len(&self, ident: TokenStream) -> TokenStream { let module = self.ty.module(); let encoded_len_fn = matchself.kind {
Kind::Plain(..) | Kind::Optional(..) | Kind::Required(..) => quote!(encoded_len),
Kind::Repeated => quote!(encoded_len_repeated),
Kind::Packed => quote!(encoded_len_packed),
}; let encoded_len_fn = quote!(::prost::encoding::#module::#encoded_len_fn); let tag = self.tag;
/// Returns an expression which evaluates to the default value of the field. pubfn default(&self) -> TokenStream { matchself.kind {
Kind::Plain(ref value) | Kind::Required(ref value) => value.owned(),
Kind::Optional(_) => quote!(::core::option::Option::None),
Kind::Repeated | Kind::Packed => quote!(::prost::alloc::vec::Vec::new()),
}
}
/// An inner debug wrapper, around the base type. fn debug_inner(&self, wrap_name: TokenStream) -> TokenStream { iflet Ty::Enumeration(ref ty) = self.ty {
quote! { struct#wrap_name<'a>(&'a i32); impl<'a> ::core::fmt::Debug for #wrap_name<'a> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { let res: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(*self.0); match res {
Err(_) => ::core::fmt::Debug::fmt(&self.0, f),
Ok(en) => ::core::fmt::Debug::fmt(&en, f),
}
}
}
}
} else {
quote! { #[allow(non_snake_case)] fn#wrap_name<T>(v: T) -> T { v }
}
}
}
/// Returns a fragment for formatting the field `ident` in `Debug`. pubfn debug(&self, wrapper_name: TokenStream) -> TokenStream { let wrapper = self.debug_inner(quote!(Inner)); let inner_ty = self.ty.rust_type(); matchself.kind {
Kind::Plain(_) | Kind::Required(_) => self.debug_inner(wrapper_name),
Kind::Optional(_) => quote! { struct#wrapper_name<'a>(&'a ::core::option::Option<#inner_ty>); impl<'a> ::core::fmt::Debug for #wrapper_name<'a> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { #wrapper
::core::fmt::Debug::fmt(&self.0.as_ref().map(Inner), f)
}
}
},
Kind::Repeated | Kind::Packed => {
quote! { struct#wrapper_name<'a>(&'a ::prost::alloc::vec::Vec<#inner_ty>); impl<'a> ::core::fmt::Debug for #wrapper_name<'a> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { letmut vec_builder = f.debug_list(); for v inself.0 { #wrapper
vec_builder.entry(&Inner(v));
}
vec_builder.finish()
}
}
}
}
}
}
/// Returns methods to embed in the message. pubfn methods(&self, ident: &TokenStream) -> Option<TokenStream> { letmut ident_str = ident.to_string(); if ident_str.starts_with("r#") {
ident_str = ident_str[2..].to_owned();
}
// Prepend `get_` for getter methods of tuple structs. let get = match syn::parse_str::<Index>(&ident_str) {
Ok(index) => { let get = Ident::new(&format!("get_{}", index.index), Span::call_site());
quote!(#get)
}
Err(_) => quote!(#ident),
};
iflet Ty::Enumeration(ref ty) = self.ty { let set = Ident::new(&format!("set_{}", ident_str), Span::call_site()); let set_doc = format!("Sets `{}` to the provided enum value.", ident_str);
Some(matchself.kind {
Kind::Plain(ref default) | Kind::Required(ref default) => { let get_doc = format!( "Returns the enum value of `{}`, \
or the default if the field is set to an invalid enum value.",
ident_str,
);
quote! { #[doc=#get_doc] pubfn#get(&self) -> #ty {
::core::convert::TryFrom::try_from(self.#ident).unwrap_or(#default)
}
#[doc=#set_doc] pubfn#set(&mutself, value: #ty) { self.#ident = value as i32;
}
}
}
Kind::Optional(ref default) => { let get_doc = format!( "Returns the enum value of `{}`, \
or the default if the field is unset or set to an invalid enum value.",
ident_str,
);
quote! { #[doc=#get_doc] pubfn#get(&self) -> #ty { self.#ident.and_then(|x| { let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
result.ok()
}).unwrap_or(#default)
}
#[doc=#set_doc] pubfn#set(&mutself, value: #ty) { self.#ident = ::core::option::Option::Some(value as i32);
}
}
}
Kind::Repeated | Kind::Packed => { let iter_doc = format!( "Returns an iterator which yields the valid enum values contained in `{}`.",
ident_str,
); let push = Ident::new(&format!("push_{}", ident_str), Span::call_site()); let push_doc = format!("Appends the provided enum value to `{}`.", ident_str);
quote! { #[doc=#iter_doc] pubfn#get(&self) -> ::core::iter::FilterMap<
::core::iter::Cloned<::core::slice::Iter<i32>>, fn(i32) -> ::core::option::Option<#ty>,
> { self.#ident.iter().cloned().filter_map(|x| { let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x);
result.ok()
})
} #[doc=#push_doc] pubfn#push(&mutself, value: #ty) { self.#ident.push(value as i32);
}
}
}
})
} elseiflet Kind::Optional(ref default) = self.kind { let ty = self.ty.rust_ref_type();
/// Returns false if the scalar type is length delimited (i.e., `string` or `bytes`). pubfn is_numeric(&self) -> bool {
!matches!(self, Ty::String | Ty::Bytes(..))
}
}
impl fmt::Debug for Ty { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl fmt::Display for Ty { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
/// Scalar Protobuf field types. #[derive(Clone)] pubenum Kind { /// A plain proto3 scalar field.
Plain(DefaultValue), /// An optional scalar field.
Optional(DefaultValue), /// A required proto2 scalar field.
Required(DefaultValue), /// A repeated scalar field.
Repeated, /// A packed repeated scalar field.
Packed,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.