buffa-codegen 0.9.0

Shared code generation logic for buffa (descriptor → Rust source)
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Parsing of proto `default_value` strings into Rust expression `TokenStream`s.

use crate::generated::descriptor::FieldDescriptorProto;
use proc_macro2::TokenStream;
use quote::quote;

use crate::context::CodeGenContext;
use crate::features::ResolvedFeatures;
use crate::CodeGenError;

/// Parse a proto `default_value` string into a Rust expression `TokenStream`.
///
/// Returns `Ok(None)` if `default_value` is absent, or `Ok(Some(expr))` where
/// `expr` is the Rust literal or constructor for the default.
///
/// # Errors
///
/// Returns an error if the default value cannot be parsed for the field's type,
/// or if the enum variant lookup fails.
pub fn parse_default_value(
    field: &FieldDescriptorProto,
    ctx: &CodeGenContext,
    current_package: &str,
    features: &ResolvedFeatures,
    nesting: usize,
    string_repr: crate::StringRepr,
) -> Result<Option<TokenStream>, CodeGenError> {
    use crate::generated::descriptor::field_descriptor_proto::Type;

    let default_str = match field.default_value.as_deref() {
        Some(s) if !s.is_empty() => s,
        _ => return Ok(None),
    };

    // Custom defaults apply to fields with explicit storage semantics:
    // proto2, editions `EXPLICIT`, and editions `LEGACY_REQUIRED` (required
    // fields carry declared defaults exactly like proto2 `required`). Proto3
    // implicit fields and editions implicit presence ignore `default_value`.
    if !matches!(
        features.field_presence,
        crate::features::FieldPresence::Explicit | crate::features::FieldPresence::LegacyRequired
    ) {
        return Ok(None);
    }

    let field_name = field.name.as_deref().unwrap_or("<unknown>");
    let ty = field.r#type.unwrap_or_default();
    let expr = match ty {
        Type::TYPE_BOOL => match default_str {
            "true" => quote! { true },
            "false" => quote! { false },
            _ => {
                return Err(CodeGenError::Other(format!(
                    "field '{field_name}': invalid bool default: '{default_str}'"
                )))
            }
        },
        Type::TYPE_INT32 | Type::TYPE_SINT32 | Type::TYPE_SFIXED32 => {
            let val: i32 = default_str.parse().map_err(|_| {
                CodeGenError::Other(format!(
                    "field '{field_name}': invalid i32 default: '{default_str}'"
                ))
            })?;
            quote! { #val }
        }
        Type::TYPE_UINT32 | Type::TYPE_FIXED32 => {
            let val: u32 = default_str.parse().map_err(|_| {
                CodeGenError::Other(format!(
                    "field '{field_name}': invalid u32 default: '{default_str}'"
                ))
            })?;
            quote! { #val }
        }
        Type::TYPE_INT64 | Type::TYPE_SINT64 | Type::TYPE_SFIXED64 => {
            let val: i64 = default_str.parse().map_err(|_| {
                CodeGenError::Other(format!(
                    "field '{field_name}': invalid i64 default: '{default_str}'"
                ))
            })?;
            quote! { #val }
        }
        Type::TYPE_UINT64 | Type::TYPE_FIXED64 => {
            let val: u64 = default_str.parse().map_err(|_| {
                CodeGenError::Other(format!(
                    "field '{field_name}': invalid u64 default: '{default_str}'"
                ))
            })?;
            quote! { #val }
        }
        Type::TYPE_FLOAT => parse_float_default::<f32>(default_str)?,
        Type::TYPE_DOUBLE => parse_float_default::<f64>(default_str)?,
        Type::TYPE_STRING => {
            // default_value is the raw text, not escaped. When strict_utf8_mapping
            // normalizes this field to bytes, emit the literal as a Vec<u8> —
            // the proto source literal is valid UTF-8 by definition.
            if crate::impl_message::effective_type(ctx, field, features) == Type::TYPE_BYTES {
                quote! { ::buffa::alloc::string::String::from(#default_str).into_bytes() }
            } else if string_repr.is_default() {
                quote! { ::buffa::alloc::string::String::from(#default_str) }
            } else {
                // Non-default string types: convert via From<String>. The
                // surrounding context (Default initializer / clear assignment)
                // pins the target type, so `Into::into` infers it.
                quote! {
                    ::core::convert::Into::into(::buffa::alloc::string::String::from(#default_str))
                }
            }
        }
        Type::TYPE_BYTES => {
            let bytes = unescape_c_escape_string(default_str)?;
            let byte_literals = bytes.iter().map(|b| quote! { #b });
            quote! { ::buffa::alloc::vec![#(#byte_literals),*] }
        }
        Type::TYPE_ENUM => {
            // default_str is the proto value name (e.g. "BAR").
            let type_name = field
                .type_name
                .as_deref()
                .ok_or(CodeGenError::MissingField("field.type_name"))?;
            let path_str = ctx
                .rust_type_relative(type_name, current_package, nesting)
                .ok_or_else(|| {
                    CodeGenError::Other(format!(
                        "enum type '{type_name}' not found in descriptor set"
                    ))
                })?;
            let ty = crate::message::rust_path_to_tokens(&path_str);
            // Must use the same keyword-escaping as enumeration.rs so that
            // e.g. `[default = type]` → `r#type` and `[default = Self]` → `Self_`
            // match the actual variant ident emitted in the enum definition.
            let variant_ident = crate::message::make_field_ident(default_str);
            // Closed enum fields store bare `E`; open representation (native
            // or via an enum-type feature override) stores `EnumValue<E>`.
            if features.enum_type == crate::features::EnumType::Open {
                quote! { ::buffa::EnumValue::Known(#ty::#variant_ident) }
            } else {
                quote! { #ty::#variant_ident }
            }
        }
        _ => return Ok(None),
    };

    Ok(Some(expr))
}

/// Generated default expression for a bare-stored enum field whose default
/// differs from `EnumValue::<E>::default()` (raw wire zero).
///
/// Fires only for *required* (proto2 `required` / editions `LEGACY_REQUIRED`)
/// open-representation enum fields with either an explicit proto
/// `default_value` or a non-zero first declared enum value. Both can only
/// originate from proto2/closed declarations — the spec pins genuinely-open
/// enums' first value to zero, and implicit-presence fields cannot reference
/// closed enums — so in practice this covers closed enums opened by
/// [`FeatureOverride::EnumType`](crate::FeatureOverride), where the declared
/// default must survive the representation change. Returns `Ok(None)`
/// everywhere else, so ordinary open-enum output is untouched. The required
/// gate lives here (not in callers) so every surface that consults this —
/// owned `Default`, `clear()`, view `Default`, reflection `has()` — agrees by
/// construction.
///
/// Extern (`extern_path`) enums are not in the compilation set, so their
/// first declared value is unknown; an opened extern required enum field
/// falls back to the wire-zero default (consistently across all surfaces)
/// unless it carries an explicit `default_value`.
pub fn open_enum_bare_default_value(
    field: &FieldDescriptorProto,
    ctx: &CodeGenContext,
    current_package: &str,
    features: &ResolvedFeatures,
    nesting: usize,
) -> Result<Option<TokenStream>, CodeGenError> {
    use crate::generated::descriptor::field_descriptor_proto::Type;

    if field.r#type.unwrap_or_default() != Type::TYPE_ENUM
        || features.enum_type != crate::features::EnumType::Open
        || !crate::impl_message::is_required_field(field, features)
    {
        return Ok(None);
    }

    if let Some(expr) = parse_default_value(
        field,
        ctx,
        current_package,
        features,
        nesting,
        crate::StringRepr::String,
    )? {
        return Ok(Some(expr));
    }

    let type_name = field
        .type_name
        .as_deref()
        .ok_or(CodeGenError::MissingField("field.type_name"))?;
    if ctx.enum_first_value(type_name).unwrap_or(0) == 0 {
        return Ok(None);
    }

    let path_str = ctx
        .rust_type_relative(type_name, current_package, nesting)
        .ok_or_else(|| {
            CodeGenError::Other(format!(
                "enum type '{type_name}' not found in descriptor set"
            ))
        })?;
    let ty = crate::message::rust_path_to_tokens(&path_str);
    // The generated enum's `Default` is its first declared value.
    Ok(Some(quote! {
        ::buffa::EnumValue::Known(<#ty as ::core::default::Default>::default())
    }))
}

/// Parse a float/double default value, handling special values "inf", "-inf", "nan".
fn parse_float_default<F>(s: &str) -> Result<TokenStream, CodeGenError>
where
    F: std::str::FromStr + std::fmt::Display,
{
    let is_f32 = std::mem::size_of::<F>() == 4;
    match s {
        "inf" | "infinity" => {
            if is_f32 {
                Ok(quote! { f32::INFINITY })
            } else {
                Ok(quote! { f64::INFINITY })
            }
        }
        "-inf" | "-infinity" => {
            if is_f32 {
                Ok(quote! { f32::NEG_INFINITY })
            } else {
                Ok(quote! { f64::NEG_INFINITY })
            }
        }
        "nan" => {
            if is_f32 {
                Ok(quote! { f32::NAN })
            } else {
                Ok(quote! { f64::NAN })
            }
        }
        _ => {
            if is_f32 {
                let val: f32 = s
                    .parse()
                    .map_err(|_| CodeGenError::Other(format!("invalid f32 default: '{s}'")))?;
                Ok(quote! { #val })
            } else {
                let val: f64 = s
                    .parse()
                    .map_err(|_| CodeGenError::Other(format!("invalid f64 default: '{s}'")))?;
                Ok(quote! { #val })
            }
        }
    }
}

/// Unescape a C-escaped byte string as used in protobuf `default_value` for
/// bytes fields.
///
/// Based on `google::protobuf::UnescapeCEscapeString`.
///
/// # Errors
///
/// Returns an error for invalid escape sequences instead of panicking.
pub fn unescape_c_escape_string(s: &str) -> Result<Vec<u8>, CodeGenError> {
    let src = s.as_bytes();
    let len = src.len();
    let mut dst = Vec::with_capacity(len);
    let mut p = 0;

    while p < len {
        if src[p] != b'\\' {
            dst.push(src[p]);
            p += 1;
        } else {
            p += 1;
            if p == len {
                return Err(CodeGenError::Other(format!(
                    "invalid c-escaped default binary value ({s}): ends with '\\'"
                )));
            }
            match src[p] {
                b'a' => {
                    dst.push(0x07);
                    p += 1;
                }
                b'b' => {
                    dst.push(0x08);
                    p += 1;
                }
                b'f' => {
                    dst.push(0x0C);
                    p += 1;
                }
                b'n' => {
                    dst.push(0x0A);
                    p += 1;
                }
                b'r' => {
                    dst.push(0x0D);
                    p += 1;
                }
                b't' => {
                    dst.push(0x09);
                    p += 1;
                }
                b'v' => {
                    dst.push(0x0B);
                    p += 1;
                }
                b'\\' => {
                    dst.push(0x5C);
                    p += 1;
                }
                b'?' => {
                    dst.push(0x3F);
                    p += 1;
                }
                b'\'' => {
                    dst.push(0x27);
                    p += 1;
                }
                b'"' => {
                    dst.push(0x22);
                    p += 1;
                }
                b'0'..=b'7' => {
                    let mut octal = 0u32;
                    for _ in 0..3 {
                        if p < len && src[p] >= b'0' && src[p] <= b'7' {
                            octal = octal * 8 + (src[p] - b'0') as u32;
                            p += 1;
                        } else {
                            break;
                        }
                    }
                    if octal > 255 {
                        return Err(CodeGenError::Other(format!(
                            "invalid c-escaped default binary value ({s}): octal escape \\{octal:o} out of range (max \\377)"
                        )));
                    }
                    dst.push(octal as u8);
                }
                b'x' | b'X' => {
                    p += 1;
                    if p == len {
                        return Err(CodeGenError::Other(format!(
                            "invalid c-escaped default binary value ({s}): incomplete hex value"
                        )));
                    }
                    if !src[p].is_ascii_hexdigit() {
                        return Err(CodeGenError::Other(format!(
                            "invalid c-escaped default binary value ({s}): invalid hex value"
                        )));
                    }
                    // C++ consumes an arbitrary run of hex digits and then
                    // range-checks the accumulated value; saturate so absurdly
                    // long runs cannot wrap (they still fail the range check).
                    let mut hex = 0u32;
                    while p < len {
                        let Some(digit) = char::from(src[p]).to_digit(16) else {
                            break;
                        };
                        hex = hex.saturating_mul(16).saturating_add(digit);
                        p += 1;
                    }
                    if hex > 255 {
                        return Err(CodeGenError::Other(format!(
                            "invalid c-escaped default binary value ({s}): hex escape \\x{hex:x} out of range (max \\xff)"
                        )));
                    }
                    dst.push(hex as u8);
                }
                _ => {
                    return Err(CodeGenError::Other(format!(
                        "invalid c-escaped default binary value ({s}): invalid escape"
                    )));
                }
            }
        }
    }
    Ok(dst)
}

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

    #[test]
    fn unescape_plain_text() {
        assert_eq!(
            unescape_c_escape_string("hello world").unwrap(),
            b"hello world"
        );
    }

    #[test]
    fn unescape_null() {
        assert_eq!(unescape_c_escape_string(r"\0").unwrap(), b"\0");
    }

    #[test]
    fn unescape_octal() {
        assert_eq!(
            unescape_c_escape_string(r"\012\156").unwrap(),
            &[0o012, 0o156]
        );
    }

    #[test]
    fn unescape_hex() {
        assert_eq!(
            unescape_c_escape_string(r"\x01\x02").unwrap(),
            &[0x01, 0x02]
        );
    }

    #[test]
    fn unescape_all_escapes() {
        assert_eq!(
            unescape_c_escape_string(r#"\0\001\a\b\f\n\r\t\v\\\'\"\xfe"#).unwrap(),
            b"\0\x01\x07\x08\x0C\n\r\t\x0B\\\'\"\xFE"
        );
    }

    #[test]
    fn unescape_incomplete_hex() {
        // `\x` with no digits at all is incomplete.
        assert!(unescape_c_escape_string(r"\x").is_err());
    }

    #[test]
    fn unescape_hex_single_digit() {
        // C++ accepts any run of one or more hex digits; a single digit at
        // end of input is a complete escape.
        assert_eq!(unescape_c_escape_string(r"\x1").unwrap(), &[0x01]);
    }

    #[test]
    fn unescape_trailing_backslash() {
        assert!(unescape_c_escape_string(r"\").is_err());
    }

    #[test]
    fn unescape_question_mark() {
        assert_eq!(unescape_c_escape_string(r"\?").unwrap(), &[0x3F]);
    }

    #[test]
    fn unescape_uppercase_hex() {
        assert_eq!(unescape_c_escape_string(r"\XAB").unwrap(), &[0xAB]);
    }

    #[test]
    fn unescape_invalid_escape() {
        assert!(unescape_c_escape_string(r"\z").is_err());
    }

    #[test]
    fn unescape_invalid_hex_digits() {
        assert!(unescape_c_escape_string(r"\xGG").is_err());
    }

    #[test]
    fn unescape_empty_input() {
        assert_eq!(unescape_c_escape_string("").unwrap(), &[] as &[u8]);
    }

    #[test]
    fn unescape_hex_overflow_rejected() {
        // \xff = 255; boundary value, must succeed.
        assert_eq!(unescape_c_escape_string(r"\xff").unwrap(), &[0xFF]);
        // \xfff = 4095; C++ consumes all three digits and rejects the
        // accumulated value as out of range (it does NOT decode \xff and
        // leave a literal 'f').
        assert!(unescape_c_escape_string(r"\xfff").is_err());
    }

    #[test]
    fn unescape_hex_greedy_leading_zeros() {
        // A long run of digits whose accumulated value fits in a byte is
        // accepted: C++ range-checks the value, not the digit count.
        assert_eq!(unescape_c_escape_string(r"\x000000ff").unwrap(), &[0xFF]);
    }

    #[test]
    fn unescape_hex_long_run_no_overflow() {
        // 10 'f's would wrap a u32 (0xffffffffff > u32::MAX); the
        // accumulator must saturate and reject, not wrap or panic.
        assert!(unescape_c_escape_string(r"\xffffffffff").is_err());
    }

    #[test]
    fn unescape_hex_greedy_stops_at_non_hex_digit() {
        // The digit run ends at the first non-hex character.
        assert_eq!(
            unescape_c_escape_string(r"\x41z").unwrap(),
            &[0x41, b'z'] as &[u8]
        );
    }

    #[test]
    fn unescape_octal_overflow_rejected() {
        // \400 = 256, which exceeds the single-byte range; must be an error.
        assert!(unescape_c_escape_string(r"\400").is_err());
        // \777 = 511; also out of range.
        assert!(unescape_c_escape_string(r"\777").is_err());
        // \377 = 255; boundary value, must still succeed.
        assert_eq!(unescape_c_escape_string(r"\377").unwrap(), &[0xFF]);
    }

    #[test]
    fn parse_float_inf() {
        let ts = parse_float_default::<f32>("inf").unwrap();
        assert!(ts.to_string().contains("INFINITY"));
    }

    #[test]
    fn parse_float_neg_inf() {
        let ts = parse_float_default::<f64>("-inf").unwrap();
        assert!(ts.to_string().contains("NEG_INFINITY"));
    }

    #[test]
    fn parse_float_nan() {
        let ts = parse_float_default::<f32>("nan").unwrap();
        assert!(ts.to_string().contains("NAN"));
    }

    #[test]
    fn parse_float_infinity_long_form() {
        let ts = parse_float_default::<f32>("infinity").unwrap();
        assert!(ts.to_string().contains("INFINITY"));
    }

    #[test]
    fn parse_float_neg_infinity_long_form() {
        let ts = parse_float_default::<f64>("-infinity").unwrap();
        assert!(ts.to_string().contains("NEG_INFINITY"));
    }

    #[test]
    fn parse_float_normal() {
        let ts = parse_float_default::<f64>("3.14").unwrap();
        let s = ts.to_string();
        assert!(s.contains("3.14"), "expected 3.14 in '{s}'");
    }

    #[test]
    fn parse_float_invalid() {
        assert!(parse_float_default::<f32>("not_a_number").is_err());
    }
}