use proc_macro2::TokenStream;
use quote::quote;
use crate::context::CodeGenContext;
use crate::generated::descriptor::field_descriptor_proto::Type;
use crate::CodeGenError;
pub(crate) fn denied_view_serialize(name: &proc_macro2::Ident) -> TokenStream {
quote! {
impl<'__a> ::serde::Serialize for #name<'__a> {
fn serialize<__S: ::serde::Serializer>(&self, _serializer: __S)
-> ::core::result::Result<__S::Ok, __S::Error>
{
Err(<__S::Error as ::serde::ser::Error>::custom(
::buffa::json_helpers::ViewJsonErrorReason::Denied
))
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum JsonPolicyKind {
Bytes,
String,
}
impl JsonPolicyKind {
pub(crate) fn of_field(
ctx: &CodeGenContext<'_>,
field_type: Type,
bytes_repr: &crate::BytesRepr,
string_repr: &crate::StringRepr,
) -> Option<Self> {
match (field_type, bytes_repr, string_repr) {
(Type::TYPE_BYTES, crate::BytesRepr::Custom(path), _)
if ctx.is_json_policy_type(path) =>
{
ctx.record_json_policy_type(path);
Some(Self::Bytes)
}
(Type::TYPE_STRING, _, crate::StringRepr::Custom(path))
if ctx.is_json_policy_type(path) =>
{
ctx.record_json_policy_type(path);
Some(Self::String)
}
_ => None,
}
}
pub(crate) fn borrowed(self) -> TokenStream {
match self {
Self::Bytes => quote! { [u8] },
Self::String => quote! { str },
}
}
pub(crate) fn serialize_borrowed(
self,
rust_type: &TokenStream,
value: &TokenStream,
) -> TokenStream {
match self {
Self::Bytes => {
quote! { &::buffa::json_helpers::BytesPolicyJson::<#rust_type>::new(#value) }
}
Self::String => {
quote! { &::buffa::json_helpers::StringPolicyJson::<#rust_type>::new(#value) }
}
}
}
pub(crate) fn serialize_owned(
self,
rust_type: &TokenStream,
value: &TokenStream,
) -> TokenStream {
let borrowed = self.borrowed();
self.serialize_borrowed(
rust_type,
"e! { ::core::convert::AsRef::<#borrowed>::as_ref(#value) },
)
}
pub(crate) fn seed(self, rust_type: &TokenStream) -> TokenStream {
match self {
Self::Bytes => quote! { ::buffa::json_helpers::BytesPolicySeed::<#rust_type>::new() },
Self::String => quote! { ::buffa::json_helpers::StringPolicySeed::<#rust_type>::new() },
}
}
}
pub(crate) fn of_view_field(
ctx: &CodeGenContext<'_>,
proto_fqn: &str,
field_name: &str,
field_type: Type,
) -> Result<Option<(JsonPolicyKind, TokenStream)>, CodeGenError> {
let path = match field_type {
Type::TYPE_BYTES => match crate::impl_message::field_bytes_repr(ctx, proto_fqn, field_name)
{
crate::BytesRepr::Custom(path) => (JsonPolicyKind::Bytes, path),
_ => return Ok(None),
},
Type::TYPE_STRING => {
match crate::impl_message::field_string_repr(ctx, proto_fqn, field_name) {
crate::StringRepr::Custom(path) => (JsonPolicyKind::String, path),
_ => return Ok(None),
}
}
_ => return Ok(None),
};
let (kind, path) = path;
if !ctx.is_json_policy_type(&path) {
return Ok(None);
}
ctx.record_json_policy_type(&path);
Ok(Some((kind, crate::parse_custom_type_path(&path)?)))
}
pub(crate) fn of_map_slot(
ctx: &CodeGenContext<'_>,
kind: JsonPolicyKind,
repr_path: Option<&str>,
) -> Result<Option<(JsonPolicyKind, TokenStream)>, CodeGenError> {
match repr_path {
Some(path) if ctx.is_json_policy_type(path) => {
ctx.record_json_policy_type(path);
Ok(Some((kind, crate::parse_custom_type_path(path)?)))
}
_ => Ok(None),
}
}
impl JsonPolicyKind {
pub(crate) fn serialize_seq(self, rust_type: &TokenStream, value: &TokenStream) -> TokenStream {
match self {
Self::Bytes => {
quote! { &::buffa::json_helpers::BytesPolicySeqJson::<#rust_type>::new(#value) }
}
Self::String => {
quote! { &::buffa::json_helpers::StringPolicySeqJson::<#rust_type>::new(#value) }
}
}
}
}