protto_derive 0.6.2

Automatically derive Protobuf and Rust conversions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use crate::analysis::{expect_analysis::ExpectMode, type_analysis};
use crate::debug::CallStackDebug;
use crate::field::{
    FieldProcessingContext,
    custom_conversion::CustomConversionStrategy,
    error_mode::ErrorMode,
    info::{self as field_info, ProtoFieldInfo, RustFieldInfo},
};

/// Consolidated field conversion strategy
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldConversionStrategy {
    /// Field is ignored in proto conversion
    Ignore,

    /// Uses custom user-provided functions
    Custom(CustomConversionStrategy),

    /// Custom functions that need error handling
    CustomWithError(CustomConversionStrategy, ErrorMode),

    /// Direct assignment or conversion between compatible types
    Direct(DirectStrategy),

    /// Handles optionality mismatches between rust and proto
    Option(OptionStrategy),

    /// Transparent wrapper field conversion
    Transparent(ErrorMode),

    /// Collection (Vec, etc.) conversions
    Collection(CollectionStrategy),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DirectStrategy {
    /// T -> T (same types, no conversion)
    Assignment,

    /// T -> U (different types, use Into trait)
    WithConversion,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OptionStrategy {
    /// T -> Some(T) (required -> optional)
    Wrap,

    /// Some(T) -> T (optional -> required)
    Unwrap(ErrorMode),

    /// Option<T> -> Option<U> (optional -> optional)
    Map,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CollectionStrategy {
    /// Vec<T> -> Vec<U> with conversion
    Collect(ErrorMode),

    /// Option<Vec<T>> -> Option<Vec<U>>
    MapOption,

    /// Vec<ProtoType> -> Vec<ProtoType> (no conversion)
    DirectAssignment,
}

impl FieldConversionStrategy {
    /// Create consolidated strategy from field analysis using simplified decision tree
    pub fn from_field_info(
        ctx: &FieldProcessingContext,
        _field: &syn::Field,
        rust_field_info: &RustFieldInfo,
        proto_field_info: &ProtoFieldInfo,
    ) -> Self {
        let trace = CallStackDebug::with_context(
            "field::conversion_strategy::FieldConversionStrategy",
            "from_field_info",
            ctx.struct_name,
            ctx.field_name,
            &[
                (
                    "rust_has_ignore",
                    &rust_field_info.has_proto_ignore.to_string(),
                ),
                ("rust_is_option", &rust_field_info.is_option.to_string()),
                ("rust_is_vec", &rust_field_info.is_vec.to_string()),
                (
                    "proto_is_optional",
                    &proto_field_info.is_optional().to_string(),
                ),
                (
                    "proto_is_repeated",
                    &proto_field_info.is_repeated().to_string(),
                ),
            ],
        );

        // Handle special cases first (sequential elimination)
        if rust_field_info.has_proto_ignore {
            trace.decision("proto_ignore", "Field marked with #[protto(ignore)]");
            Self::Ignore
        } else if let Some(custom_strategy) =
            CustomConversionStrategy::from_field_info(ctx.struct_name, rust_field_info)
        {
            trace.decision("custom_functions", "Custom conversion functions detected");
            if Self::custom_needs_error_handling(ctx, rust_field_info, proto_field_info) {
                let error_mode = ErrorMode::from_field_context(ctx, rust_field_info);
                Self::CustomWithError(custom_strategy, error_mode)
            } else {
                Self::Custom(custom_strategy)
            }
        } else if rust_field_info.has_transparent {
            trace.decision("transparent_field", "Transparent wrapper detected");
            let error_mode = ErrorMode::from_field_context(ctx, rust_field_info);
            Self::Transparent(error_mode)
        } else if Self::is_collection_conversion(rust_field_info, proto_field_info) {
            trace.decision("collection_conversion", "Collection type detected");
            Self::Collection(Self::determine_collection_strategy(
                ctx,
                rust_field_info,
                proto_field_info,
                &trace,
            ))
        } else if rust_field_info.has_default || ctx.default_fn.is_some() {
            if rust_field_info.is_option
                && proto_field_info.is_optional()
                && ctx.default_fn.is_none()
                && !rust_field_info.has_default
            {
                trace.decision("map_optional", "Option<T> -> Option<U>");
                Self::Option(OptionStrategy::Map)
            } else {
                trace.decision("default_field", "Field has default value");
                let error_mode = ErrorMode::from_field_context(ctx, rust_field_info);
                Self::Option(OptionStrategy::Unwrap(error_mode))
            }
        } else {
            // Handle optionality patterns (simple 2x2 matrix)
            let rust_optional = rust_field_info.is_option;
            let proto_optional = proto_field_info.is_optional();

            match (rust_optional, proto_optional) {
                (true, false) => {
                    trace.decision("wrap_optional", "Rust Option<T> -> Proto T");
                    Self::Option(OptionStrategy::Wrap)
                }
                (false, true) => {
                    trace.decision("unwrap_optional", "Proto Option<T> -> Rust T");
                    let error_mode = ErrorMode::from_field_context(ctx, rust_field_info);
                    trace.checkpoint_data(
                        "optional_strategy",
                        &[("error_mode", &format!("{error_mode:?}"))],
                    );
                    Self::Option(OptionStrategy::Unwrap(error_mode))
                }
                (true, true) if rust_field_info.expect_mode == ExpectMode::None => {
                    trace.decision("map_optional", "Option<T> -> Option<U>");
                    Self::Option(OptionStrategy::Map)
                }
                (true, true) => {
                    trace.decision(
                        "unwrap_optional_for_some_wrap",
                        "Proto optional -> unwrap -> wrap in Some",
                    );
                    let error_mode = ErrorMode::from_field_context(ctx, rust_field_info);
                    Self::Option(OptionStrategy::Unwrap(error_mode))
                }
                (false, false) => {
                    trace.decision("direct_conversion", "Both required -> direct conversion");
                    Self::Direct(Self::determine_direct_strategy(
                        ctx,
                        rust_field_info,
                        proto_field_info,
                        &trace,
                    ))
                }
            }
        }
    }

    /// Determine if custom functions need error handling based on field context
    fn custom_needs_error_handling(
        ctx: &FieldProcessingContext,
        rust_field_info: &RustFieldInfo,
        proto_field_info: &ProtoFieldInfo,
    ) -> bool {
        // Custom functions need error handling when:

        // 1. Explicit error handling attributes
        if rust_field_info.expect_mode != crate::analysis::expect_analysis::ExpectMode::None {
            return true;
        }

        // 2. Field has error function specified
        if ctx.struct_level_error_fn.is_some() {
            return true;
        }

        // 3. Proto optional -> Rust required pattern (needs unwrapping)
        if proto_field_info.is_optional() && !rust_field_info.is_option {
            return true;
        }

        // 4. Collection that might be empty but rust expects content
        if proto_field_info.is_repeated()
            && rust_field_info.is_vec
            && !rust_field_info.has_default
            && !rust_field_info.is_option
        {
            return true;
        }

        // 5. Complex custom types with bidirectional functions get error handling by default (compatibility)
        if !rust_field_info.is_option
            && !rust_field_info.is_primitive
            && rust_field_info.is_custom
            && rust_field_info.from_proto_fn.is_some()
            && rust_field_info.to_proto_fn.is_some()
        {
            return true;
        }

        false
    }

    fn is_collection_conversion(
        rust_field_info: &RustFieldInfo,
        proto_field_info: &ProtoFieldInfo,
    ) -> bool {
        rust_field_info.is_vec
            || proto_field_info.is_repeated()
            || Self::is_option_vec_type(&rust_field_info.field_type)
    }

    fn is_option_vec_type(field_type: &syn::Type) -> bool {
        type_analysis::get_inner_type_from_option(field_type)
            .map(|inner| type_analysis::is_vec_type(&inner))
            .unwrap_or(false)
    }

    fn determine_collection_strategy(
        ctx: &FieldProcessingContext,
        rust_field_info: &RustFieldInfo,
        _proto_field_info: &ProtoFieldInfo,
        trace: &CallStackDebug,
    ) -> CollectionStrategy {
        let _trace = CallStackDebug::with_context(
            "field::converstion_strategy::FieldConversionStrategy",
            "determine_collection_strategy",
            ctx.struct_name,
            &rust_field_info.field_name,
            &[
                ("rust.has_default", &rust_field_info.has_default.to_string()),
                (
                    "rust.expect_mode",
                    &format!("{:?}", rust_field_info.expect_mode),
                ),
                ("ctx.default_fn", &format!("{:?}", ctx.default_fn)),
            ],
        );

        if Self::is_option_vec_type(&rust_field_info.field_type) {
            trace.decision("option_vec", "Option<Vec<T>> detected");
            CollectionStrategy::MapOption
        } else if let Some(inner_type) =
            type_analysis::get_inner_type_from_vec(&rust_field_info.field_type)
            && type_analysis::is_proto_type(&inner_type, ctx.proto_module)
        {
            // Check for direct assignment (proto types)
            trace.decision("proto_vec_direct", "Vec<ProtoType> -> direct assignment");
            CollectionStrategy::DirectAssignment
        } else if rust_field_info.has_default || ctx.default_fn.is_some() {
            trace.decision(
                "collection_with_default",
                "Collection with default detected",
            );
            // Only apply error handling for collections when there's a default (matches old system)
            let error_mode = ErrorMode::from_field_context(ctx, rust_field_info);
            match error_mode {
                ErrorMode::Error => {
                    trace.decision(
                        "rust_has_default_or_default_fn_w_error",
                        "Vec<ProtoType> -> Standard collection conversion",
                    );
                    CollectionStrategy::Collect(ErrorMode::Error)
                }
                ErrorMode::Default(_) => {
                    let default_fn = if ctx.default_fn.is_none() && rust_field_info.has_default {
                        None
                    } else {
                        ctx.default_fn.clone()
                    };

                    trace.decision("collection_default", "Collection with default value");
                    CollectionStrategy::Collect(ErrorMode::Default(default_fn))
                }
                _ => {
                    trace.decision(
                        "rust_has_default_or_default_fn_wo_error",
                        "Vec<ProtoType> -> Standard collection w default conversion",
                    );
                    CollectionStrategy::Collect(ErrorMode::Default(ctx.default_fn.clone()))
                }
            }
        } else {
            trace.decision("standard_collection", "Standard collection conversion");
            let error_mode = ErrorMode::None;
            CollectionStrategy::Collect(error_mode)
        }
    }

    fn determine_direct_strategy(
        ctx: &FieldProcessingContext,
        rust_field_info: &RustFieldInfo,
        proto_field_info: &ProtoFieldInfo,
        trace: &CallStackDebug,
    ) -> DirectStrategy {
        // Check if types are identical or can be directly assigned
        if Self::types_are_identical(ctx, rust_field_info, proto_field_info) {
            trace.decision(
                "identical_types",
                "Types are identical -> direct assignment",
            );
            DirectStrategy::Assignment
        } else {
            trace.decision("conversion_needed", "Types differ -> conversion with Into");
            DirectStrategy::WithConversion
        }
    }

    fn types_are_identical(
        ctx: &FieldProcessingContext,
        rust_field_info: &RustFieldInfo,
        proto_field_info: &ProtoFieldInfo,
    ) -> bool {
        (rust_field_info.is_primitive && proto_field_info.mapping == field_info::ProtoMapping::Scalar) // Primitive scalar types
            || type_analysis::is_proto_type(&rust_field_info.field_type, ctx.proto_module) // Proto types (same module)
    }

    /// Validate that default_fn is not used with repeated/collection fields
    fn validate_default_fn_compatibility(
        ctx: &FieldProcessingContext,
        rust_field_info: &RustFieldInfo,
        proto_field_info: &ProtoFieldInfo,
    ) -> Result<(), FieldGenerationError> {
        // Reject default_fn on repeated fields - they can't be "missing", only empty
        if ctx.default_fn.is_some() && (proto_field_info.is_repeated() || rust_field_info.is_vec) {
            return Err(FieldGenerationError::ConversionValidation(format!(
                "default_fn cannot be used with repeated/collection fields. \
                     Proto3 repeated fields cannot be 'missing' (only empty []). \
                     Field '{}' is a collection type.",
                rust_field_info.field_name
            )));
        }
        Ok(())
    }

    /// Get a human-readable description of this strategy
    #[allow(unused)]
    pub fn description(&self) -> &'static str {
        match self {
            Self::Ignore => "field ignored - not in proto",
            Self::Direct(direct) => match direct {
                DirectStrategy::Assignment => "direct assignment (no conversion)",
                DirectStrategy::WithConversion => "direct conversion with Into",
            },
            Self::Option(option) => match option {
                OptionStrategy::Wrap => "wrap value in Some()",
                OptionStrategy::Unwrap(_) => "unwrap Optional with error handling",
                OptionStrategy::Map => "map through optional conversion",
            },
            Self::Transparent(_) => "transparent wrapper conversion",
            Self::Collection(collection) => match collection {
                CollectionStrategy::Collect(_) => "collect vector with conversion",
                CollectionStrategy::MapOption => "map optional vector",
                CollectionStrategy::DirectAssignment => "direct vector assignment",
            },
            Self::Custom(custom) | Self::CustomWithError(custom, ErrorMode::None) => match custom {
                CustomConversionStrategy::FromFn(_) => "custom proto->rust function",
                CustomConversionStrategy::IntoFn(_) => "custom rust->proto function",
                CustomConversionStrategy::Bidirectional(_, _) => "bidirectional custom functions",
            },
            Self::CustomWithError(custom, ErrorMode::Error) => match custom {
                CustomConversionStrategy::FromFn(_) => "custom proto->rust function + error",
                CustomConversionStrategy::IntoFn(_) => "custom rust->proto function + error",
                CustomConversionStrategy::Bidirectional(_, _) => {
                    "bidirectional custom functions + error"
                }
            },
            Self::CustomWithError(custom, ErrorMode::Panic) => match custom {
                CustomConversionStrategy::FromFn(_) => "custom proto->rust function + panic",
                CustomConversionStrategy::IntoFn(_) => "custom rust->proto function + panic",
                CustomConversionStrategy::Bidirectional(_, _) => {
                    "bidirectional custom functions + panic"
                }
            },
            Self::CustomWithError(custom, ErrorMode::Default(_)) => match custom {
                CustomConversionStrategy::FromFn(_) => "custom proto->rust function + default",
                CustomConversionStrategy::IntoFn(_) => "custom rust->proto function + default",
                CustomConversionStrategy::Bidirectional(_, _) => {
                    "bidirectional custom functions + default"
                }
            },
        }
    }

    /// Get the category of this strategy for grouping
    #[allow(unused)]
    pub fn category(&self) -> &'static str {
        match self {
            Self::Ignore => "ignore",
            Self::Custom(_) | Self::CustomWithError(_, _) => "custom",
            Self::Direct(_) => "direct",
            Self::Option(_) => "option",
            Self::Transparent(_) => "transparent",
            Self::Collection(_) => "collection",
        }
    }
}

/// Migration error types
#[derive(Debug)]
pub enum FieldGenerationError {
    ConversionValidation(String),
}

impl std::fmt::Display for FieldGenerationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FieldGenerationError::ConversionValidation(msg) => {
                write!(f, "field conversion validation failed: {}", msg)
            }
        }
    }
}

impl std::error::Error for FieldGenerationError {}

/// Main entry point for field conversion with migration support
pub fn generate_field_conversions(
    field: &syn::Field,
    ctx: &FieldProcessingContext,
) -> Result<(proc_macro2::TokenStream, proc_macro2::TokenStream), FieldGenerationError> {
    // Analyze field using new system
    let rust_field_info = RustFieldInfo::analyze(ctx, field);
    let proto_field_info = ProtoFieldInfo::infer_from(ctx, field, &rust_field_info);
    let strategy =
        FieldConversionStrategy::from_field_info(ctx, field, &rust_field_info, &proto_field_info);
    strategy.validate_for_context(ctx, &rust_field_info, &proto_field_info)?;

    let _trace = CallStackDebug::with_context(
        "field::conversion_strategy",
        "generate_field_conversions",
        ctx.struct_name,
        &rust_field_info.field_name,
        &[
            ("conversion_category", strategy.category()),
            ("conversion_strategy", strategy.description()),
        ],
    );

    // Generate code
    let proto_to_rust =
        strategy.generate_proto_to_rust_conversion(ctx, field, &rust_field_info, &proto_field_info);
    let rust_to_proto =
        strategy.generate_rust_to_proto_conversion(ctx, field, &rust_field_info, &proto_field_info);

    Ok((proto_to_rust, rust_to_proto))
}

// Integration with existing field analysis
impl FieldConversionStrategy {
    /// Validate that this strategy is compatible with the given context
    pub fn validate_for_context(
        &self,
        ctx: &FieldProcessingContext,
        rust_field_info: &RustFieldInfo,
        proto_field_info: &ProtoFieldInfo,
    ) -> Result<(), FieldGenerationError> {
        Self::validate_default_fn_compatibility(ctx, rust_field_info, proto_field_info)?;
        // Use the existing validation logic from the new system
        match self {
            FieldConversionStrategy::Ignore => {
                if !rust_field_info.has_proto_ignore {
                    return Err(FieldGenerationError::ConversionValidation(
                        "Ignore strategy requires #[protto(ignore)] attribute".to_string(),
                    ));
                }
            }
            FieldConversionStrategy::Custom(custom_strategy) => {
                custom_strategy
                    .validate()
                    .map_err(FieldGenerationError::ConversionValidation)?;
            }
            FieldConversionStrategy::Transparent(_) => {
                if !rust_field_info.has_transparent {
                    return Err(FieldGenerationError::ConversionValidation(
                        "Transparent strategy requires #[protto(transparent)] attribute"
                            .to_string(),
                    ));
                }
                // Additional transparent-specific validation could go here
            }
            FieldConversionStrategy::Collection(_) => {
                if !rust_field_info.is_vec && !proto_field_info.is_repeated() {
                    return Err(FieldGenerationError::ConversionValidation(
                        "Collection strategy requires Vec or repeated field".to_string(),
                    ));
                }
            }
            _ => {
                // Other strategies have their own validation logic
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_strategy_descriptions() {
        let ignore = FieldConversionStrategy::Ignore;
        assert_eq!(ignore.description(), "field ignored - not in proto");
        assert_eq!(ignore.category(), "ignore");

        let direct = FieldConversionStrategy::Direct(DirectStrategy::Assignment);
        assert_eq!(direct.category(), "direct");
        assert!(direct.description().contains("direct assignment"));
    }
}