Skip to content
Open
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
26 changes: 21 additions & 5 deletions prost-derive/src/field/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ impl Field {
unknown_attrs.push(attr);
}
}

let ty = match ty {
Some(ty) => ty,
None => return Ok(None),
Expand Down Expand Up @@ -112,7 +111,10 @@ impl Field {
Kind::Repeated => quote!(encode_repeated),
Kind::Packed => quote!(encode_packed),
};
let encode_fn = quote!(::prost::encoding::#module::#encode_fn);
let encode_fn = match self.ty {
Ty::Foreign(ref path) => quote!(#path::#encode_fn),
_ => quote!(::prost::encoding::#module::#encode_fn),
};
let tag = self.tag;

match self.kind {
Expand Down Expand Up @@ -143,7 +145,10 @@ impl Field {
Kind::Plain(..) | Kind::Optional(..) | Kind::Required(..) => quote!(merge),
Kind::Repeated | Kind::Packed => quote!(merge_repeated),
};
let merge_fn = quote!(::prost::encoding::#module::#merge_fn);
let merge_fn = match self.ty {
Ty::Foreign(ref path) => quote!(#path::#merge_fn),
_ => quote!(::prost::encoding::#module::#merge_fn),
};

match self.kind {
Kind::Plain(..) | Kind::Required(..) | Kind::Repeated | Kind::Packed => quote! {
Expand All @@ -166,7 +171,11 @@ impl Field {
Kind::Repeated => quote!(encoded_len_repeated),
Kind::Packed => quote!(encoded_len_packed),
};
let encoded_len_fn = quote!(::prost::encoding::#module::#encoded_len_fn);

let encoded_len_fn = match self.ty {
Ty::Foreign(ref path) => quote!(#path::#encoded_len_fn),
_ => quote!(::prost::encoding::#module::#encoded_len_fn),
};
let tag = self.tag;

match self.kind {
Expand Down Expand Up @@ -400,6 +409,7 @@ pub enum Ty {
String,
Bytes(BytesTy),
Enumeration(Path),
Foreign(Path),
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -443,6 +453,7 @@ impl Ty {
Meta::Path(ref name) if name.is_ident("bool") => Ty::Bool,
Meta::Path(ref name) if name.is_ident("string") => Ty::String,
Meta::Path(ref name) if name.is_ident("bytes") => Ty::Bytes(BytesTy::Vec),
Meta::Path(ref name) if name.leading_colon.is_some() => Ty::Foreign(name.clone()),
Meta::NameValue(MetaNameValue {
ref path,
value:
Expand Down Expand Up @@ -525,6 +536,7 @@ impl Ty {
Ty::String => "string",
Ty::Bytes(..) => "bytes",
Ty::Enumeration(..) => "enum",
Ty::Foreign(..) => "asdfasdf",
}
}

Expand Down Expand Up @@ -556,6 +568,7 @@ impl Ty {
Ty::String => quote!(&str),
Ty::Bytes(..) => quote!(&[u8]),
Ty::Enumeration(..) => quote!(i32),
Ty::Foreign(ref path) => quote!(#path),
}
}

Expand Down Expand Up @@ -612,6 +625,7 @@ pub enum DefaultValue {
String(String),
Bytes(Vec<u8>),
Enumeration(TokenStream),
Foreign(TokenStream),
Path(Path),
}

Expand Down Expand Up @@ -771,11 +785,11 @@ impl DefaultValue {
Ty::Int64 | Ty::Sint64 | Ty::Sfixed64 => DefaultValue::I64(0),
Ty::Uint32 | Ty::Fixed32 => DefaultValue::U32(0),
Ty::Uint64 | Ty::Fixed64 => DefaultValue::U64(0),

Ty::Bool => DefaultValue::Bool(false),
Ty::String => DefaultValue::String(String::new()),
Ty::Bytes(..) => DefaultValue::Bytes(Vec::new()),
Ty::Enumeration(ref path) => DefaultValue::Enumeration(quote!(#path::default())),
Ty::Foreign(ref path) => DefaultValue::Foreign(quote!(#path::default())),
}
}

Expand All @@ -792,6 +806,7 @@ impl DefaultValue {
let lit = LitByteStr::new(value, Span::call_site());
quote!(#lit.as_ref().into())
}
DefaultValue::Foreign(ref path) => quote!(#path),

ref other => other.typed(),
}
Expand Down Expand Up @@ -822,6 +837,7 @@ impl ToTokens for DefaultValue {
tokens.append_all(quote!(#byte_str as &[u8]));
}
DefaultValue::Enumeration(ref value) => value.to_tokens(tokens),
DefaultValue::Foreign(ref value) => value.to_tokens(tokens),
DefaultValue::Path(ref value) => value.to_tokens(tokens),
}
}
Expand Down
Loading