Skip to main content

tonneau_build/codegen/
serde.rs

1use anyhow::Context;
2use quote::{ToTokens, quote};
3use syn::parse_quote;
4
5use super::Package;
6use super::cel::compiler::{CompiledExpr, Compiler};
7use super::cel::types::CelType;
8use super::cel::{CelExpression, eval_message_fmt, functions};
9use crate::types::{
10    ProtoEnumType, ProtoFieldOptions, ProtoFieldSerdeOmittable, ProtoMessageField,
11    ProtoMessageType, ProtoModifiedValueType, ProtoOneOfType, ProtoType, ProtoTypeRegistry,
12    ProtoValueType, ProtoVisibility, Tagged,
13};
14
15fn handle_oneof(
16    package: &mut Package,
17    field_name: &str,
18    oneof: &ProtoOneOfType,
19    registry: &ProtoTypeRegistry,
20    visibility: ProtoVisibility,
21) -> anyhow::Result<()> {
22    let message_config = package.message_config(&oneof.message);
23    message_config.field_attribute(field_name, parse_quote!(#[tonneau(oneof)]));
24
25    let oneof_config = message_config.oneof_config(field_name);
26
27    if visibility.has_output() {
28        oneof_config
29            .attribute(parse_quote!(#[derive(::tonneau::reexports::serde_derive::Serialize)]));
30    }
31
32    oneof_config.attribute(parse_quote!(#[derive(::tonneau::__private::Tracker)]));
33
34    let variant_identifier_ident = quote::format_ident!("___identifier");
35    let mut oneof_identifier_for_ident = variant_identifier_ident.clone();
36    let mut variant_idents = Vec::new();
37    let mut variant_name_fn = Vec::new();
38    let mut variant_from_str_fn = Vec::new();
39    let mut variant_fields = Vec::new();
40    let mut variant_enum_ident = Vec::new();
41    let mut deserializer_impl_fn = Vec::new();
42    let mut validate_message_impl = Vec::new();
43
44    let tagged_impl = if let Some(Tagged { tag, content }) = &oneof.options.tagged {
45        oneof_config.attribute(parse_quote!(#[serde(tag = #tag, content = #content)]));
46        oneof_config.attribute(parse_quote!(#[tonneau(tagged)]));
47        oneof_identifier_for_ident = quote::format_ident!("___tagged_identifier");
48        quote! {
49            #[derive(
50                ::std::fmt::Debug,
51                ::std::clone::Clone,
52                ::core::marker::Copy,
53                ::core::cmp::PartialEq,
54                ::core::cmp::Eq,
55                ::core::hash::Hash,
56                ::core::cmp::Ord,
57                ::core::cmp::PartialOrd,
58            )]
59            #[allow(non_camel_case_types)]
60            pub enum #oneof_identifier_for_ident {
61                ___tag,
62                ___content,
63            }
64
65            impl ::tonneau::__private::Identifier for #oneof_identifier_for_ident {
66                const OPTIONS: &'static [&'static str] = &[
67                    #tag,
68                    #content,
69                ];
70
71                fn name(&self) -> &'static str {
72                    match self {
73                        #oneof_identifier_for_ident::___tag => #tag,
74                        #oneof_identifier_for_ident::___content => #content,
75                    }
76                }
77            }
78
79            impl ::core::str::FromStr for #oneof_identifier_for_ident {
80                type Err = ();
81
82                fn from_str(s: &str) -> Result<Self, Self::Err> {
83                    ::tonneau::__tonneau_field_from_str!(s,
84                        #tag => #oneof_identifier_for_ident::___tag,
85                        #content => #oneof_identifier_for_ident::___content,
86                    )
87                }
88            }
89
90            impl ::tonneau::__private::TaggedOneOfIdentifier for #oneof_identifier_for_ident {
91                const TAG: Self = #oneof_identifier_for_ident::___tag;
92                const CONTENT: Self = #oneof_identifier_for_ident::___content;
93            }
94        }
95    } else {
96        quote! {}
97    };
98
99    for (field_name, field) in &oneof.fields {
100        anyhow::ensure!(!field.options.flatten, "oneof fields cannot be flattened");
101
102        let ident = quote::format_ident!("__field_{field_name}");
103        let serde_name = &field.options.serde_name;
104
105        oneof_config.field_attribute(field_name, parse_quote!(#[serde(rename = #serde_name)]));
106        if visibility.has_output() && !field.options.visibility.has_output() {
107            oneof_config.field_attribute(field_name, parse_quote!(#[serde(skip_serializing)]));
108        }
109
110        if field.options.visibility.has_input() {
111            variant_idents.push(ident.clone());
112            variant_name_fn.push(quote! {
113                #variant_identifier_ident::#ident => #serde_name
114            });
115            variant_from_str_fn.push(quote! {
116                #serde_name => #variant_identifier_ident::#ident
117            });
118            variant_fields.push(quote! {
119                #serde_name
120            });
121            let enum_ident = field.rust_ident();
122            variant_enum_ident.push(enum_ident.clone());
123            deserializer_impl_fn.push(quote! {
124                #variant_identifier_ident::#ident => {
125                    let tracker = match tracker {
126                        ::core::option::Option::None => {
127                            let ___Tracker::#enum_ident(tracker) = tracker.get_or_insert_with(|| ___Tracker::#enum_ident(Default::default())) else {
128                                ::core::unreachable!()
129                            };
130
131                            tracker
132                        },
133                        ::core::option::Option::Some(___Tracker::#enum_ident(tracker)) => {
134                            if !::tonneau::__private::tracker_allow_duplicates(Some(tracker)) {
135                                return ::tonneau::__private::report_tracked_error(
136                                    ::tonneau::__private::TrackedError::duplicate_field(),
137                                );
138                            }
139
140                            tracker
141                        },
142                        ::core::option::Option::Some(tracker) => {
143                            return ::core::result::Result::Err(
144                                ::tonneau::reexports::serde::de::Error::invalid_type(
145                                    ::tonneau::reexports::serde::de::Unexpected::Other(
146                                        ::tonneau::__private::Identifier::name(&Self::tracker_to_identifier(tracker)),
147                                    ),
148                                    &::tonneau::__private::Identifier::name(&#variant_identifier_ident::#ident),
149                                ),
150                            );
151                        }
152                    };
153
154                    let value = match value.get_or_insert_with(|| Self::#enum_ident(Default::default())) {
155                        Self::#enum_ident(value) => value,
156                        value => {
157                            return ::core::result::Result::Err(
158                                ::tonneau::reexports::serde::de::Error::invalid_type(
159                                    ::tonneau::reexports::serde::de::Unexpected::Other(
160                                        ::tonneau::__private::Identifier::name(&Self::value_to_identifier(value)),
161                                    ),
162                                    &::tonneau::__private::Identifier::name(&#variant_identifier_ident::#ident),
163                                ),
164                            );
165                        }
166                    };
167
168                    ::tonneau::__private::TrackerDeserializer::deserialize(
169                        tracker,
170                        value,
171                        deserializer,
172                    )?;
173                }
174            });
175
176            let cel_validation_fn = cel_expressions(
177                registry,
178                &ProtoType::Value(field.ty.clone()),
179                &field.full_name,
180                &field.options,
181                quote!(*value),
182                quote!(tracker),
183            )?;
184
185            let serde_name = if let Some(tagged) = &oneof.options.tagged {
186                tagged.content.as_str()
187            } else {
188                field.options.serde_name.as_str()
189            };
190
191            validate_message_impl.push(quote! {
192                (Self::#enum_ident(value)) => {
193                    let _token = ::tonneau::__private::ProtoPathToken::push_field(#field_name);
194                    let _token = ::tonneau::__private::SerdePathToken::push_field(#serde_name);
195                    let tracker = match tracker {
196                        ::core::option::Option::Some(___Tracker::#enum_ident(tracker)) => ::core::option::Option::Some(tracker),
197                        ::core::option::Option::Some(t) => return ::core::result::Result::Err(
198                            ::tonneau::reexports::serde::de::Error::custom(format!(
199                                "tracker and value do not match: {:?} != {:?}",
200                                ::tonneau::__private::Identifier::name(&<Self as ::tonneau::__private::TrackedOneOfDeserializer<'_>>::tracker_to_identifier(t)),
201                                ::tonneau::__private::Identifier::name(&<Self as ::tonneau::__private::TrackedOneOfDeserializer<'_>>::value_to_identifier(self)),
202                            )),
203                        ),
204                        ::core::option::Option::None => ::core::option::Option::None,
205                    };
206                    #(#cel_validation_fn)*
207                }
208            });
209        }
210
211        match &field.ty {
212            ProtoValueType::Enum(path) => {
213                let path_str = registry
214                    .resolve_rust_path(&oneof.message, path)
215                    .expect("enum not found")
216                    .to_token_stream()
217                    .to_string();
218
219                if field.options.visibility.has_output() {
220                    let serialize_with =
221                        format!("::tonneau::__private::serialize_enum::<{path_str}, _, _>");
222                    oneof_config.field_attribute(
223                        field_name,
224                        parse_quote!(#[serde(serialize_with = #serialize_with)]),
225                    );
226                }
227
228                oneof_config
229                    .field_attribute(field_name, parse_quote!(#[tonneau(enum = #path_str)]));
230            }
231            ProtoValueType::WellKnown(_) | ProtoValueType::Bytes => {
232                if field.options.visibility.has_output() {
233                    oneof_config.field_attribute(
234                        field_name,
235                        parse_quote!(#[serde(serialize_with = "::tonneau::__private::serialize_well_known")]),
236                    );
237                }
238            }
239            ProtoValueType::Float | ProtoValueType::Double
240                if registry.support_non_finite_vals(&field.full_name) =>
241            {
242                if field.options.visibility.has_output() {
243                    oneof_config.field_attribute(
244                            field_name,
245                            parse_quote!(#[serde(serialize_with = "::tonneau::__private::serialize_floats_with_non_finite")]),
246                        );
247                }
248                if field.options.visibility.has_input() {
249                    oneof_config.field_attribute(
250                        field_name,
251                        parse_quote!(#[tonneau(with_non_finite_values)]),
252                    );
253                }
254            }
255            _ => {}
256        }
257    }
258
259    let message = registry
260        .get_message(&oneof.message)
261        .expect("message not found");
262
263    let oneof_path = oneof.rust_path(&message.package);
264    let oneof_ident = oneof_path.segments.last().unwrap().ident.clone();
265    let bug_message = quote::quote!(::core::unreachable!(
266        "oneof has no valid variants, this should never happen, please report this as a bug in tonneau"
267    ));
268
269    package.push_item(parse_quote! {
270        #[allow(clippy::all, dead_code, unused_imports, unused_variables, unused_parens, unreachable_patterns, unreachable_code)]
271        const _: () = {
272            #tagged_impl
273
274            #[derive(
275                ::std::fmt::Debug,
276                ::std::clone::Clone,
277                ::core::marker::Copy,
278                ::core::cmp::PartialEq,
279                ::core::cmp::Eq,
280                ::core::hash::Hash,
281                ::core::cmp::Ord,
282                ::core::cmp::PartialOrd,
283            )]
284            #[allow(non_camel_case_types)]
285            pub enum #variant_identifier_ident {
286                #(#variant_idents),*
287            }
288
289            impl ::tonneau::__private::Identifier for #variant_identifier_ident {
290                const OPTIONS: &'static [&'static str] = &[#(#variant_fields),*];
291
292                fn name(&self) -> &'static str {
293                    match self {
294                        #(#variant_name_fn,)*
295                        _ => #bug_message,
296                    }
297                }
298            }
299
300            impl ::core::str::FromStr for #variant_identifier_ident {
301                type Err = ();
302
303                fn from_str(s: &str) -> Result<Self, Self::Err> {
304                    ::tonneau::__tonneau_field_from_str!(s, #(#variant_from_str_fn),*)
305                }
306            }
307
308            impl ::tonneau::__private::Expected for #oneof_path {
309                fn expecting(formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
310                    write!(formatter, stringify!(#oneof_ident))
311                }
312            }
313
314            impl ::tonneau::__private::IdentifierFor for #oneof_path {
315                const NAME: &'static str = stringify!(#oneof_ident);
316                type Identifier = #oneof_identifier_for_ident;
317            }
318
319            impl ::tonneau::__private::TrackedOneOfVariant for #oneof_path {
320                type Variant = #variant_identifier_ident;
321            }
322
323            type ___Tracker = <<#oneof_path as ::tonneau::__private::TrackerFor>::Tracker as ::tonneau::__private::TrackerWrapper>::Tracker;
324
325            impl<'de> ::tonneau::__private::TrackedOneOfDeserializer<'de> for #oneof_path {
326                fn deserialize<D>(
327                    value: &mut ::core::option::Option<#oneof_path>,
328                    variant: #variant_identifier_ident,
329                    tracker: &mut ::core::option::Option<___Tracker>,
330                    deserializer: D,
331                ) -> ::core::result::Result<(), D::Error>
332                where
333                    D: ::tonneau::__private::DeserializeContent<'de>
334                {
335                    match variant {
336                        #(#deserializer_impl_fn,)*
337                        _ => #bug_message,
338                    }
339
340                    ::core::result::Result::Ok(())
341                }
342
343                fn tracker_to_identifier(v: &___Tracker) -> #variant_identifier_ident {
344                    match v {
345                        #(___Tracker::#variant_enum_ident(_) => #variant_identifier_ident::#variant_idents,)*
346                        _ => #bug_message,
347                    }
348                }
349
350                fn value_to_identifier(v: &#oneof_path) -> #variant_identifier_ident {
351                    match v {
352                        #(#oneof_path::#variant_enum_ident(_) => #variant_identifier_ident::#variant_idents,)*
353                        _ => #bug_message,
354                    }
355                }
356            }
357
358            impl ::tonneau::__private::TonneauValidate for #oneof_path {
359                fn validate(&self, tracker: Option<&<#oneof_path as ::tonneau::__private::TrackerFor>::Tracker>) -> ::core::result::Result<(), ::tonneau::__private::ValidationError> {
360                    let tracker = tracker.and_then(|t| t.as_ref());
361                    match self {
362                        #(#validate_message_impl,)*
363                        _ => #bug_message,
364                    }
365
366                    ::core::result::Result::Ok(())
367                }
368            }
369        };
370    });
371
372    Ok(())
373}
374
375struct FieldBuilder<'a> {
376    deserializer_fields: &'a mut Vec<proc_macro2::TokenStream>,
377    field_enum_variants: &'a mut Vec<proc_macro2::TokenStream>,
378    field_enum_name_fn: &'a mut Vec<proc_macro2::TokenStream>,
379    field_enum_from_str_fn: &'a mut Vec<proc_macro2::TokenStream>,
380    field_enum_from_str_flattened_fn: &'a mut Vec<proc_macro2::TokenStream>,
381    deserializer_fn: &'a mut Vec<proc_macro2::TokenStream>,
382    cel_validation_fn: &'a mut Vec<proc_macro2::TokenStream>,
383}
384
385fn handle_message_field(
386    package: &mut Package,
387    field_name: &str,
388    field: &ProtoMessageField,
389    field_builder: FieldBuilder<'_>,
390    field_enum_ident: &syn::Ident,
391    registry: &ProtoTypeRegistry,
392) -> anyhow::Result<()> {
393    let serde_name = &field.options.serde_name;
394
395    let message_config = package.message_config(&field.message);
396
397    message_config.field_attribute(field_name, parse_quote!(#[serde(rename = #serde_name)]));
398
399    let message = registry
400        .get_message(&field.message)
401        .expect("message not found");
402
403    let ident = quote::format_ident!("__field_{field_name}");
404    if field.options.flatten {
405        let flattened_ty_path = match &field.ty {
406            ProtoType::Modified(ProtoModifiedValueType::Optional(ProtoValueType::Message(
407                path,
408            )))
409            | ProtoType::Value(ProtoValueType::Message(path)) => registry
410                .resolve_rust_path(&message.package, path)
411                .expect("message not found"),
412            ProtoType::Modified(ProtoModifiedValueType::OneOf(oneof)) => {
413                oneof.rust_path(&message.package)
414            }
415            _ => anyhow::bail!("flattened fields must be messages or oneofs"),
416        };
417
418        if field.options.visibility.has_output() {
419            message_config.field_attribute(field_name, parse_quote!(#[serde(flatten)]));
420        }
421
422        if field.options.visibility.has_input() {
423            let flattened_identifier = quote! {
424                <#flattened_ty_path as ::tonneau::__private::IdentifierFor>::Identifier
425            };
426
427            field_builder.deserializer_fields.push(quote! {
428                <#flattened_identifier as ::tonneau::__private::Identifier>::OPTIONS
429            });
430            field_builder.field_enum_variants.push(quote! {
431                #ident(#flattened_identifier)
432            });
433            field_builder.field_enum_name_fn.push(quote! {
434                #field_enum_ident::#ident(flatten) => ::tonneau::__private::Identifier::name(flatten)
435            });
436            field_builder.field_enum_from_str_flattened_fn.push(quote! {
437                #ident
438            });
439        }
440    } else if field.options.visibility.has_input() {
441        field_builder.deserializer_fields.push(quote! {
442            &[#serde_name]
443        });
444        field_builder.field_enum_variants.push(quote! {
445            #ident
446        });
447        field_builder.field_enum_name_fn.push(quote! {
448            #field_enum_ident::#ident => #serde_name
449        });
450        field_builder.field_enum_from_str_fn.push(quote! {
451            #serde_name => #field_enum_ident::#ident
452        });
453    }
454
455    if field.options.visibility.has_output() {
456        if matches!(
457            field.options.serde_omittable,
458            ProtoFieldSerdeOmittable::True
459        ) {
460            message_config.field_attribute(
461                field_name,
462                parse_quote!(#[serde(skip_serializing_if = "::tonneau::__private::serde_ser_skip_default")]),
463            );
464        }
465    } else {
466        message_config.field_attribute(field_name, parse_quote!(#[serde(skip_serializing)]));
467    }
468
469    match field.ty.value_type() {
470        Some(ProtoValueType::Enum(path)) => {
471            let path_str = registry
472                .resolve_rust_path(message.full_name.trim_last_segment(), path)
473                .expect("enum not found")
474                .to_token_stream()
475                .to_string();
476
477            if field.options.visibility.has_output() {
478                let serialize_with =
479                    format!("::tonneau::__private::serialize_enum::<{path_str}, _, _>");
480                message_config.field_attribute(
481                    field_name,
482                    parse_quote!(#[serde(serialize_with = #serialize_with)]),
483                );
484            }
485
486            message_config.field_attribute(field_name, parse_quote!(#[tonneau(enum = #path_str)]));
487        }
488        Some(ProtoValueType::WellKnown(_) | ProtoValueType::Bytes) => {
489            if field.options.visibility.has_output() {
490                message_config.field_attribute(
491                    field_name,
492                    parse_quote!(#[serde(serialize_with = "::tonneau::__private::serialize_well_known")]),
493                );
494            }
495        }
496        Some(ProtoValueType::Float | ProtoValueType::Double)
497            if registry.support_non_finite_vals(&field.full_name) =>
498        {
499            if field.options.visibility.has_output() {
500                message_config.field_attribute(
501                        field_name,
502                        parse_quote!(#[serde(serialize_with = "::tonneau::__private::serialize_floats_with_non_finite")]),
503                    );
504            }
505            if field.options.visibility.has_input() {
506                message_config
507                    .field_attribute(field_name, parse_quote!(#[tonneau(with_non_finite_values)]));
508            }
509        }
510        _ => {}
511    }
512
513    if let ProtoType::Modified(ProtoModifiedValueType::OneOf(oneof)) = &field.ty {
514        handle_oneof(
515            package,
516            field_name,
517            oneof,
518            registry,
519            field.options.visibility,
520        )?;
521    }
522
523    let field_ident = field.rust_ident();
524
525    let mut tracker = quote! {
526        &mut tracker.#field_ident
527    };
528
529    let mut value = quote! {
530        &mut self.#field_ident
531    };
532
533    // When a field is not nullable but prost generates an option<T>, we need to
534    // remove the option before deserializing otherwise null will be a valid input.
535    if matches!(
536        field.ty,
537        ProtoType::Modified(ProtoModifiedValueType::Optional(_))
538    ) && (!field.options.nullable || field.options.flatten)
539    {
540        tracker = quote! {
541            (#tracker).get_or_insert_default()
542        };
543
544        value = quote! {
545            (#value).get_or_insert_default()
546        };
547    }
548
549    if field.options.visibility.has_input() {
550        if field.options.flatten {
551            field_builder.deserializer_fn.push(quote! {
552                #field_enum_ident::#ident(field) => {
553                    if let Err(error) = ::tonneau::__private::TrackerDeserializeIdentifier::<'de>::deserialize(
554                        (#tracker).get_or_insert_default(),
555                        #value,
556                        field,
557                        deserializer,
558                    ) {
559                        return ::tonneau::__private::report_de_error(error);
560                    }
561                }
562            });
563        } else {
564            field_builder.deserializer_fn.push(quote! {
565                #field_enum_ident::#ident => {
566                    let tracker = #tracker;
567
568                    if !::tonneau::__private::tracker_allow_duplicates(tracker.as_ref()) {
569                        return ::tonneau::__private::report_tracked_error(
570                            ::tonneau::__private::TrackedError::duplicate_field(),
571                        );
572                    }
573
574                    if let Err(error) = ::tonneau::__private::TrackerDeserializer::deserialize(
575                        tracker.get_or_insert_default(),
576                        #value,
577                        deserializer,
578                    ) {
579                        return ::tonneau::__private::report_de_error(error);
580                    }
581                }
582            });
583        }
584    }
585
586    let push_field_token = if !field.options.flatten {
587        quote! {
588            let _token = ::tonneau::__private::SerdePathToken::push_field(
589                ::tonneau::__private::Identifier::name(&#field_enum_ident::#ident),
590            );
591        }
592    } else {
593        quote! {}
594    };
595
596    let missing = if matches!(
597        field.options.serde_omittable,
598        ProtoFieldSerdeOmittable::False
599    ) && !field.options.flatten
600    {
601        quote! {
602            ::tonneau::__private::report_tracked_error(
603                ::tonneau::__private::TrackedError::missing_field(),
604            )?;
605        }
606    } else {
607        quote! {}
608    };
609
610    let mut tracker_access = quote!(tracker.and_then(|t| t.#field_ident.as_ref()));
611    if matches!(
612        field.ty,
613        ProtoType::Modified(ProtoModifiedValueType::Optional(_))
614    ) {
615        tracker_access = quote!(#tracker_access.and_then(|t| t.as_ref()))
616    }
617
618    if field.options.visibility.has_input() {
619        let cel_validation_fn = cel_expressions(
620            registry,
621            &field.ty,
622            &field.full_name,
623            &field.options,
624            quote!(self.#field_ident),
625            tracker_access,
626        )?;
627
628        field_builder.cel_validation_fn.push(quote!({
629            let _token = ::tonneau::__private::ProtoPathToken::push_field(#field_name);
630            #push_field_token
631
632            // TODO: this seems wrong. I feel as if we should validate it even if omittable is true.
633            if tracker.is_none_or(|t| t.#field_ident.is_some()) {
634                #(#cel_validation_fn)*
635            } else {
636                #missing
637            }
638        }));
639    }
640
641    Ok(())
642}
643
644fn cel_expressions(
645    registry: &ProtoTypeRegistry,
646    ty: &ProtoType,
647    field_full_name: &str,
648    options: &ProtoFieldOptions,
649    value_accessor: proc_macro2::TokenStream,
650    tracker_accessor: proc_macro2::TokenStream,
651) -> anyhow::Result<Vec<proc_macro2::TokenStream>> {
652    let compiler = Compiler::new(registry);
653    let mut cel_validation_fn = Vec::new();
654
655    let evaluate_expr = |ctx: &Compiler, expr: &CelExpression| {
656        let mut ctx = ctx.child();
657        if let Some(this) = expr.this.clone() {
658            ctx.add_variable("this", CompiledExpr::constant(this));
659        }
660        let parsed = cel_parser::parse(&expr.expression).context("expression parse")?;
661        let resolved = ctx.resolve(&parsed).context("cel expression")?;
662        let expr_str = &expr.expression;
663        let message = eval_message_fmt(field_full_name, &expr.message, &ctx).context("message")?;
664
665        anyhow::Ok(quote! {
666            if !::tonneau::__private::cel::to_bool({
667                (|| {
668                    ::core::result::Result::Ok::<_, ::tonneau::__private::cel::CelError>(#resolved)
669                })().map_err(|err| {
670                    ::tonneau::__private::ValidationError::Expression {
671                        error: err.to_string().into_boxed_str(),
672                        field: #field_full_name,
673                        expression: #expr_str,
674                    }
675                })?
676            }) {
677                ::tonneau::__private::report_tracked_error(
678                    ::tonneau::__private::TrackedError::invalid_field(#message)
679                )?;
680            }
681        })
682    };
683
684    {
685        let mut compiler = compiler.child();
686        let (value_match, field_type) = match ty {
687            ProtoType::Modified(ProtoModifiedValueType::Optional(ty)) => {
688                (quote!(Some(value)), ProtoType::Value(ty.clone()))
689            }
690            ty @ ProtoType::Modified(ProtoModifiedValueType::OneOf(_)) => {
691                (quote!(Some(value)), ty.clone())
692            }
693            _ => (quote!(value), ty.clone()),
694        };
695
696        if let ProtoType::Value(ProtoValueType::Enum(path))
697        | ProtoType::Modified(ProtoModifiedValueType::Optional(ProtoValueType::Enum(path))) = ty
698        {
699            compiler.register_function(functions::Enum(Some(path.clone())));
700        }
701
702        let recursive_validate = matches!(
703            field_type,
704            ProtoType::Value(ProtoValueType::Message(_))
705                | ProtoType::Modified(ProtoModifiedValueType::OneOf(_))
706        );
707
708        compiler.add_variable(
709            "input",
710            CompiledExpr::runtime(CelType::Proto(field_type), parse_quote!(value)),
711        );
712        let mut exprs = options
713            .cel_exprs
714            .field
715            .iter()
716            .map(|expr| evaluate_expr(&compiler, expr))
717            .collect::<anyhow::Result<Vec<_>>>()?;
718
719        if recursive_validate {
720            exprs.push(quote! {
721                ::tonneau::__private::TonneauValidate::validate(value, #tracker_accessor)?;
722            })
723        }
724
725        if !exprs.is_empty() {
726            cel_validation_fn.push(quote! {{
727                #[allow(irrefutable_let_patterns)]
728                if let #value_match = &#value_accessor {
729                    #(#exprs)*
730                }
731            }});
732        }
733
734        if !options.nullable
735            && matches!(
736                &ty,
737                ProtoType::Modified(
738                    ProtoModifiedValueType::Optional(_) | ProtoModifiedValueType::OneOf(_)
739                )
740            )
741        {
742            cel_validation_fn.push(quote! {{
743                if #value_accessor.is_none() {
744                    ::tonneau::__private::report_tracked_error(
745                        ::tonneau::__private::TrackedError::missing_field()
746                    )?;
747                }
748            }})
749        }
750    }
751
752    match ty {
753        ProtoType::Modified(ProtoModifiedValueType::Map(key, value))
754            if !options.cel_exprs.map_key.is_empty()
755                || !options.cel_exprs.map_value.is_empty()
756                || matches!(value, ProtoValueType::Message(_)) =>
757        {
758            let key_exprs = {
759                let mut compiler = compiler.child();
760
761                if let ProtoValueType::Enum(path) = key {
762                    compiler.register_function(functions::Enum(Some(path.clone())));
763                }
764
765                compiler.add_variable(
766                    "input",
767                    CompiledExpr::runtime(
768                        CelType::Proto(ProtoType::Value(key.clone())),
769                        parse_quote!(key),
770                    ),
771                );
772                options
773                    .cel_exprs
774                    .map_key
775                    .iter()
776                    .map(|expr| evaluate_expr(&compiler, expr))
777                    .collect::<anyhow::Result<Vec<_>>>()?
778            };
779
780            let is_message = matches!(value, ProtoValueType::Message(_));
781
782            let mut value_exprs = {
783                let mut compiler = compiler.child();
784                if let ProtoValueType::Enum(path) = value {
785                    compiler.register_function(functions::Enum(Some(path.clone())));
786                }
787                compiler.add_variable(
788                    "input",
789                    CompiledExpr::runtime(
790                        CelType::Proto(ProtoType::Value(value.clone())),
791                        parse_quote!(value),
792                    ),
793                );
794                options
795                    .cel_exprs
796                    .map_value
797                    .iter()
798                    .map(|expr| evaluate_expr(&compiler, expr))
799                    .collect::<anyhow::Result<Vec<_>>>()?
800            };
801
802            if is_message {
803                value_exprs.push(quote!({
804                    let tracker = match #tracker_accessor {
805                        ::core::option::Option::Some(t) => Some(t.get(key).expect("map tracker state missing item, this is a bug report it.")),
806                        ::core::option::Option::None => None
807                    };
808                    ::tonneau::__private::TonneauValidate::validate(value, tracker)?;
809                }));
810            }
811
812            cel_validation_fn.push(quote! {{
813                for (key, value) in &#value_accessor {
814                    let _token = ::tonneau::__private::ProtoPathToken::push_field(key);
815                    let _token = ::tonneau::__private::SerdePathToken::push_field(key);
816                    #(#key_exprs)*
817                    #(#value_exprs)*
818                }
819            }});
820        }
821        ProtoType::Modified(ProtoModifiedValueType::Repeated(item))
822            if !options.cel_exprs.repeated_item.is_empty()
823                || matches!(item, ProtoValueType::Message(_)) =>
824        {
825            let is_message = matches!(item, ProtoValueType::Message(_));
826            let mut compiler = compiler.child();
827            if let ProtoValueType::Enum(path) = item {
828                compiler.register_function(functions::Enum(Some(path.clone())));
829            }
830            compiler.add_variable(
831                "input",
832                CompiledExpr::runtime(
833                    CelType::Proto(ProtoType::Value(item.clone())),
834                    parse_quote!(item),
835                ),
836            );
837
838            let mut exprs = options
839                .cel_exprs
840                .repeated_item
841                .iter()
842                .map(|expr| evaluate_expr(&compiler, expr))
843                .collect::<anyhow::Result<Vec<_>>>()?;
844
845            if is_message {
846                exprs.push(quote!({
847                    let tracker = match #tracker_accessor {
848                        ::core::option::Option::Some(t) => Some(t.get(idx).expect("repeated tracker state missing item, this is a bug report it.")),
849                        ::core::option::Option::None => None
850                    };
851                    ::tonneau::__private::TonneauValidate::validate(item, tracker)?;
852                }));
853            }
854
855            cel_validation_fn.push(quote! {{
856                for (idx, item) in #value_accessor.iter().enumerate() {
857                    let _token = ::tonneau::__private::ProtoPathToken::push_index(idx);
858                    let _token = ::tonneau::__private::SerdePathToken::push_index(idx);
859                    #(#exprs)*
860                }
861            }});
862        }
863        _ => {}
864    }
865
866    Ok(cel_validation_fn)
867}
868
869pub(super) fn handle_message(
870    message: &ProtoMessageType,
871    package: &mut Package,
872    registry: &ProtoTypeRegistry,
873) -> anyhow::Result<()> {
874    let message_config = package.message_config(&message.full_name);
875
876    message_config
877        .attribute(parse_quote!(#[derive(::tonneau::reexports::serde_derive::Serialize)]));
878    message_config.attribute(parse_quote!(#[serde(crate = "::tonneau::reexports::serde")]));
879    message_config.attribute(parse_quote!(#[derive(::tonneau::__private::Tracker)]));
880
881    let field_enum_ident = quote::format_ident!("___field_enum");
882
883    let mut field_enum_variants = Vec::new();
884    let mut field_enum_name_fn = Vec::new();
885    let mut field_enum_from_str_fn = Vec::new();
886    let mut field_enum_from_str_flattened_fn = Vec::new();
887    let mut deserializer_fields = Vec::new();
888    let mut deserializer_fn = Vec::new();
889    let mut cel_validation_fn = Vec::new();
890
891    for (field_name, field) in message.fields.iter() {
892        handle_message_field(
893            package,
894            field_name,
895            field,
896            FieldBuilder {
897                deserializer_fields: &mut deserializer_fields,
898                field_enum_variants: &mut field_enum_variants,
899                field_enum_name_fn: &mut field_enum_name_fn,
900                field_enum_from_str_fn: &mut field_enum_from_str_fn,
901                field_enum_from_str_flattened_fn: &mut field_enum_from_str_flattened_fn,
902                deserializer_fn: &mut deserializer_fn,
903                cel_validation_fn: &mut cel_validation_fn,
904            },
905            &field_enum_ident,
906            registry,
907        )?;
908    }
909
910    // Handle message-level CEL expressions
911    if !message.options.cel.is_empty() {
912        let compiler = Compiler::new(registry);
913        let mut ctx = compiler.child();
914        ctx.add_variable(
915            "input",
916            CompiledExpr::runtime(
917                CelType::Proto(ProtoType::Value(ProtoValueType::Message(
918                    message.full_name.clone(),
919                ))),
920                parse_quote!(___input),
921            ),
922        );
923
924        let mut message_cel_exprs = Vec::new();
925        for expr in &message.options.cel {
926            let mut expr_ctx = ctx.child();
927            if let Some(this) = expr.this.clone() {
928                expr_ctx.add_variable("this", CompiledExpr::constant(this));
929            }
930            let parsed = cel_parser::parse(&expr.expression)
931                .context("message-level cel expression parse")?;
932            let resolved = expr_ctx
933                .resolve(&parsed)
934                .context("message-level cel expression")?;
935            let expr_str = &expr.expression;
936            let message_str = eval_message_fmt(&message.full_name, &expr.message, &expr_ctx)
937                .context("message-level cel message")?;
938
939            message_cel_exprs.push(quote! {
940                if !::tonneau::__private::cel::to_bool({
941                    (|| {
942                        ::core::result::Result::Ok::<_, ::tonneau::__private::cel::CelError>(#resolved)
943                    })().map_err(|err| {
944                        ::tonneau::__private::ValidationError::Expression {
945                            error: err.to_string().into_boxed_str(),
946                            field: "",
947                            expression: #expr_str,
948                        }
949                    })?
950                }) {
951                    ::tonneau::__private::report_tracked_error(
952                        ::tonneau::__private::TrackedError::invalid_field(#message_str)
953                    )?;
954                }
955            });
956        }
957
958        cel_validation_fn.push(quote! {{
959            let ___input = self;
960            #(#message_cel_exprs)*
961        }});
962    }
963
964    let bug_message = quote::quote!(::core::unreachable!(
965        "message has no fields, this should never happen, please report this as a bug in tonneau"
966    ));
967
968    let message_path = registry
969        .resolve_rust_path(&message.package, &message.full_name)
970        .expect("message not found");
971    let message_ident = message_path.segments.last().unwrap().ident.clone();
972
973    package.push_item(parse_quote! {
974        #[allow(clippy::all, dead_code, unused_imports, unused_variables, unused_parens, unreachable_patterns, unreachable_code)]
975        const _: () = {
976            #[derive(
977                ::std::fmt::Debug,
978                ::std::clone::Clone,
979                ::core::marker::Copy,
980                ::core::cmp::PartialEq,
981                ::core::cmp::Eq,
982                ::core::hash::Hash,
983                ::core::cmp::Ord,
984                ::core::cmp::PartialOrd,
985            )]
986            #[allow(non_camel_case_types)]
987            pub enum #field_enum_ident {
988                #(#field_enum_variants),*
989            }
990
991            impl ::tonneau::__private::Identifier for #field_enum_ident {
992                const OPTIONS: &'static [&'static str] = ::tonneau::__private_const_concat_str_array!(#(#deserializer_fields),*);
993
994                fn name(&self) -> &'static str {
995                    match self {
996                        #(#field_enum_name_fn,)*
997                        _ => #bug_message,
998                    }
999                }
1000            }
1001
1002            impl ::core::str::FromStr for #field_enum_ident {
1003                type Err = ();
1004
1005                fn from_str(s: &str) -> Result<Self, Self::Err> {
1006                    ::tonneau::__tonneau_field_from_str!(s, #(#field_enum_from_str_fn),*, flattened: [#(#field_enum_from_str_flattened_fn),*])
1007                }
1008            }
1009
1010            impl ::tonneau::__private::IdentifierFor for #message_path {
1011                const NAME: &'static str = stringify!(#message_ident);
1012                type Identifier = #field_enum_ident;
1013            }
1014
1015            type ___Tracker = <<#message_path as ::tonneau::__private::TrackerFor>::Tracker as ::tonneau::__private::TrackerWrapper>::Tracker;
1016
1017            impl<'de> ::tonneau::__private::TrackedStructDeserializer<'de> for #message_path {
1018                #[allow(unused_mut, dead_code)]
1019                fn deserialize<D>(
1020                    &mut self,
1021                    field: Self::Identifier,
1022                    mut tracker: &mut ___Tracker,
1023                    deserializer: D,
1024                ) -> Result<(), D::Error>
1025                where
1026                    D: ::tonneau::__private::DeserializeContent<'de>,
1027                {
1028                    match field {
1029                        #(#deserializer_fn,)*
1030                        _ => #bug_message,
1031                    }
1032
1033                    ::core::result::Result::Ok(())
1034                }
1035            }
1036
1037            impl ::tonneau::__private::Expected for #message_path {
1038                fn expecting(formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
1039                    write!(formatter, stringify!(#message_ident))
1040                }
1041            }
1042
1043            impl ::tonneau::__private::TonneauValidate for #message_path {
1044                fn validate(&self, tracker: Option<&<#message_path as ::tonneau::__private::TrackerFor>::Tracker>) -> ::core::result::Result<(), ::tonneau::__private::ValidationError> {
1045                    let tracker = tracker.map(|t| &**t);
1046                    #(#cel_validation_fn)*
1047                    ::core::result::Result::Ok(())
1048                }
1049            }
1050        };
1051    });
1052
1053    Ok(())
1054}
1055
1056pub(super) fn handle_enum(
1057    enum_: &ProtoEnumType,
1058    package: &mut Package,
1059    registry: &ProtoTypeRegistry,
1060) -> anyhow::Result<()> {
1061    let enum_path = registry
1062        .resolve_rust_path(&enum_.package, &enum_.full_name)
1063        .expect("enum not found");
1064    let enum_ident = enum_path.segments.last().unwrap().ident.clone();
1065    let enum_config = package.enum_config(&enum_.full_name);
1066
1067    if enum_.options.repr_enum {
1068        enum_config
1069            .attribute(parse_quote!(#[derive(::tonneau::reexports::serde_repr::Serialize_repr)]));
1070        enum_config
1071            .attribute(parse_quote!(#[derive(::tonneau::reexports::serde_repr::Deserialize_repr)]));
1072    } else {
1073        enum_config
1074            .attribute(parse_quote!(#[derive(::tonneau::reexports::serde_derive::Serialize)]));
1075        enum_config
1076            .attribute(parse_quote!(#[derive(::tonneau::reexports::serde_derive::Deserialize)]));
1077    }
1078
1079    enum_config.attribute(parse_quote!(#[serde(crate = "::tonneau::reexports::serde")]));
1080
1081    let mut to_serde_matchers = if !enum_.options.repr_enum {
1082        Vec::new()
1083    } else {
1084        vec![quote! {
1085            item => ::tonneau::__private::cel::CelValueConv::conv(item as i32)
1086        }]
1087    };
1088
1089    for (name, variant) in &enum_.variants {
1090        if !enum_.options.repr_enum {
1091            let serde_name = &variant.options.serde_name;
1092            enum_config.variant_attribute(name, parse_quote!(#[serde(rename = #serde_name)]));
1093            let ident = &variant.rust_ident;
1094            to_serde_matchers.push(quote! {
1095                #enum_path::#ident => ::tonneau::__private::cel::CelValueConv::conv(#serde_name)
1096            })
1097        }
1098
1099        match variant.options.visibility {
1100            ProtoVisibility::InputOnly => {
1101                enum_config.variant_attribute(name, parse_quote!(#[serde(skip_serializing)]));
1102            }
1103            ProtoVisibility::OutputOnly => {
1104                enum_config.variant_attribute(name, parse_quote!(#[serde(skip_deserializing)]));
1105            }
1106            ProtoVisibility::Skip => {
1107                enum_config.variant_attribute(name, parse_quote!(#[serde(skip)]));
1108            }
1109            _ => {}
1110        }
1111    }
1112
1113    let proto_path = enum_.full_name.as_ref();
1114    let bug_message = quote::quote!(::core::unreachable!(
1115        "enum has no variants, this should never happen, please report this as a bug in tonneau"
1116    ));
1117
1118    package.push_item(parse_quote! {
1119        #[allow(clippy::all, dead_code, unused_imports, unused_variables, unused_parens, unreachable_patterns, unreachable_code)]
1120        const _: () = {
1121            impl ::tonneau::__private::Expected for #enum_path {
1122                fn expecting(formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
1123                    write!(formatter, "an enum of `")?;
1124                    write!(formatter, stringify!(#enum_ident))?;
1125                    write!(formatter, "`")
1126                }
1127            }
1128
1129            #[::tonneau::reexports::linkme::distributed_slice(::tonneau::__private::cel::TONNEAU_CEL_ENUM_VTABLE)]
1130            #[linkme(crate = ::tonneau::reexports::linkme)]
1131            static ENUM_VTABLE: ::tonneau::__private::cel::EnumVtable = ::tonneau::__private::cel::EnumVtable {
1132                proto_path: #proto_path,
1133                is_valid: |tag| {
1134                    <#enum_path as std::convert::TryFrom<i32>>::try_from(tag).is_ok()
1135                },
1136                to_serde: |tag| {
1137                    match <#enum_path as std::convert::TryFrom<i32>>::try_from(tag) {
1138                        Ok(value) => match value {
1139                            #(#to_serde_matchers,)*
1140                            _ => #bug_message,
1141                        }
1142                        Err(_) => ::tonneau::__private::cel::CelValue::Null,
1143                    }
1144                },
1145                to_proto: |tag| {
1146                    match <#enum_path as std::convert::TryFrom<i32>>::try_from(tag) {
1147                        Ok(value) => ::tonneau::__private::cel::CelValue::String(::tonneau::__private::cel::CelString::Borrowed(value.as_str_name())),
1148                        Err(_) => ::tonneau::__private::cel::CelValue::Null,
1149                    }
1150                }
1151            };
1152        };
1153    });
1154
1155    Ok(())
1156}