olai-codegen 0.0.8

Proto-driven code generation for REST handlers, clients, and resource registries
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
//! Type conversion utilities for code generation
//!
//! This module provides utilities for converting between protobuf types and Rust types
//! during code generation.

use convert_case::{Case, Casing};
use proc_macro2::TokenStream;
use quote::quote;

use crate::codegen::Runtime;
use crate::utils::{extract_qualified_type_name, extract_simple_type_name};

/// Context for rendering types in different situations
#[derive(Debug, Clone, Copy)]
pub enum RenderContext {
    /// A constructor (new method) in Rust
    Constructor,
    /// when extracting from a request inside implementations of FromRequest or FromRequestParts
    /// (path params: renders enum as i32 for direct axum::extract::Path deserialization)
    Extractor,
    /// when building the QueryParams serde struct for query string deserialization
    /// (renders enum as its actual Rust type so serde can deserialize from string variant names)
    QueryExtractor,
    /// Regular parameter type
    Parameter,
    /// Return type
    ReturnType,
    /// Field type in a struct
    FieldType,
    /// Builder method parameter
    BuilderMethod,
    /// Python parameter type (Rust FFI signatures for PyO3 bindings)
    PythonParameter,
    /// NAPI parameter type (Rust NAPI function signatures for Node.js bindings)
    NapiParameter,
}

/// Trait for rendering a [`UnifiedType`] into a language-specific string.
///
/// Each language backend provides a concrete impl. The four existing free functions
/// (`unified_to_rust`, `unified_to_python_type`, `unified_to_napi`, `unified_to_typescript`)
/// are kept as thin wrappers so call-sites don't change.
pub trait TypeRenderer {
    fn render(&self, ty: &UnifiedType) -> String;
}

/// Rust type renderer — handles optional `impl Into<>` / `Option<>` wrapping.
pub struct RustRenderer(pub RenderContext);

impl TypeRenderer for RustRenderer {
    fn render(&self, ty: &UnifiedType) -> String {
        unified_to_rust(ty, self.0)
    }
}

/// Python type annotation renderer.
pub struct PythonRenderer;

impl TypeRenderer for PythonRenderer {
    fn render(&self, ty: &UnifiedType) -> String {
        unified_to_python_type(ty)
    }
}

/// Rust NAPI parameter type renderer.
pub struct NapiRenderer;

impl TypeRenderer for NapiRenderer {
    fn render(&self, ty: &UnifiedType) -> String {
        unified_to_napi(ty)
    }
}

/// TypeScript type annotation renderer.
pub struct TypeScriptRenderer;

impl TypeRenderer for TypeScriptRenderer {
    fn render(&self, ty: &UnifiedType) -> String {
        unified_to_typescript(ty)
    }
}

/// Convert a unified type to a Rust type string.
///
/// Note: the *type* a field renders as is currently runtime-independent. Under both prost
/// and buffa, generated builder/constructor params and the query-deserialization struct use
/// the bare enum type `E` (serde deserializes the variant-name string into it), and the
/// runtime-specific wrapping (`as i32` / `EnumValue::Known`) is applied by
/// [`field_assignment`]. If a future runtime needs a different field *type* (e.g. buffa
/// zero-copy view types), thread a `Runtime` argument back in here.
pub fn unified_to_rust(unified_type: &UnifiedType, context: RenderContext) -> String {
    let base_type_str = match &unified_type.base_type {
        BaseType::String => {
            if matches!(context, RenderContext::Constructor) && !unified_type.is_optional {
                "impl Into<String>".to_string()
            } else {
                "String".to_string()
            }
        }
        BaseType::Int32 => "i32".to_string(),
        BaseType::Int64 => "i64".to_string(),
        BaseType::Bool => "bool".to_string(),
        BaseType::Float64 => "f64".to_string(),
        BaseType::Float32 => "f32".to_string(),
        BaseType::Bytes => "Vec<u8>".to_string(),
        BaseType::Unit => "()".to_string(),
        BaseType::Message(name) => extract_simple_type_name(name),
        BaseType::Enum(name) => {
            if matches!(
                context,
                RenderContext::Extractor | RenderContext::NapiParameter
            ) {
                "i32".to_string()
            } else {
                convert_protobuf_enum_to_rust_type(&format!("TYPE_ENUM:{name}"))
            }
        }
        BaseType::OneOf(name) => extract_simple_type_name(name),
        BaseType::Map(key_type, value_type) => {
            if matches!(context, RenderContext::Constructor) && !unified_type.is_optional {
                "impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>".to_string()
            } else {
                let key_str = unified_to_rust(key_type, context);
                let value_str = unified_to_rust(value_type, context);
                format!("HashMap<{key_str}, {value_str}>")
            }
        }
    };

    let mut result = base_type_str;

    // In builder methods we require the inner type only.
    if unified_type.is_repeated && !matches!(context, RenderContext::BuilderMethod) {
        result = format!("Vec<{result}>");
    }

    if should_wrap_in_option(context, unified_type) {
        result = format!("Option<{result}>");
    }

    result
}

/// Return `true` when the Rust representation of `ty` in `ctx` should be wrapped in `Option<T>`.
///
/// Two independent conditions trigger wrapping:
///
/// 1. **Optional field outside a builder method** — builder methods use `impl Into<Option<T>>`
///    for their parameter type and leave the inner `Option<T>` implicit, so wrapping is skipped
///    in that context.
///
/// 2. **FFI boundaries (Python / NAPI) with maps or repeated fields** — at FFI boundaries we
///    distinguish "field absent" from "field is an empty collection" by wrapping maps and
///    repeated fields in `Option<T>`, even when the field is not marked optional in proto.
fn should_wrap_in_option(ctx: RenderContext, ty: &UnifiedType) -> bool {
    let is_optional_non_builder = ty.is_optional && !matches!(ctx, RenderContext::BuilderMethod);
    let is_ffi = matches!(
        ctx,
        RenderContext::PythonParameter | RenderContext::NapiParameter
    );
    // At FFI boundaries, collections (maps, repeated) are wrapped in `Option` so callers can
    // distinguish "absent" from "empty". Message/oneof optionality is carried by `ty.is_optional`
    // (set during analysis from the field's `REQUIRED` behavior), so a required message body
    // renders as a bare `T` required param while an optional one renders as `Option<T>`.
    let is_ffi_collection =
        is_ffi && (matches!(ty.base_type, BaseType::Map(_, _)) || ty.is_repeated);
    is_optional_non_builder || is_ffi_collection
}

/// Generate field assignment code.
///
/// Wraps a constructor/builder parameter so it can be stored into the request struct field.
/// `runtime` selects the ABI of the protobuf library the generated code consumes:
///
/// - [`Runtime::Prost`]: open enums are `i32` (`#field as i32`); singular message/oneof
///   fields are `Option<Box<T>>` (`Some(#field)`).
/// - [`Runtime::Buffa`]: open enums are `EnumValue<E>` (`buffa::EnumValue::Known(#field)`);
///   singular message/oneof fields are `MessageField<T>` (`buffa::MessageField::some(#field)`).
pub fn field_assignment(
    unified_type: &UnifiedType,
    field_ident: &proc_macro2::Ident,
    ctx: &RenderContext,
    runtime: Runtime,
) -> TokenStream {
    if matches!(ctx, RenderContext::BuilderMethod) {
        return flexible_optional_field_assignment(unified_type, field_ident, runtime);
    }
    match &unified_type.base_type {
        BaseType::String if !unified_type.is_optional => quote! { #field_ident.into() },
        BaseType::Enum(_) => match runtime {
            Runtime::Prost if unified_type.is_repeated => {
                quote! { #field_ident.into_iter().map(|v| v as i32).collect() }
            }
            Runtime::Prost => quote! { #field_ident as i32 },
            Runtime::Buffa if unified_type.is_repeated => {
                quote! { #field_ident.into_iter().map(buffa::EnumValue::Known).collect() }
            }
            Runtime::Buffa => quote! { buffa::EnumValue::Known(#field_ident) },
        },
        BaseType::Map(_, _) => quote! {
            #field_ident.into_iter().map(|(k, v)| (k.into(), v.into())).collect()
        },
        // Singular message/oneof fields wrap the bare `T` constructor param into the request
        // struct's optional-message representation. (Repeated messages are `Vec<T>`, not
        // optional, so they fall through unchanged.) Oneof fields are `Option<Oneof>` under both
        // runtimes, so they always take the `Some(..)` form regardless of `runtime`.
        BaseType::OneOf(_) if !unified_type.is_repeated => {
            quote! { Some(#field_ident) }
        }
        BaseType::Message(_) if !unified_type.is_repeated => match runtime {
            // prost: `Option<Box<T>>`
            Runtime::Prost => quote! { Some(#field_ident) },
            // buffa: `MessageField<T>`
            Runtime::Buffa => quote! { buffa::MessageField::some(#field_ident) },
        },
        _ => quote! { #field_ident },
    }
}

/// Convert a unified type to a Python type annotation string
pub fn unified_to_python_type(unified_type: &UnifiedType) -> String {
    let base_type_str = match &unified_type.base_type {
        BaseType::String => "str".to_string(),
        BaseType::Int32 | BaseType::Int64 => "int".to_string(),
        BaseType::Bool => "bool".to_string(),
        BaseType::Float64 | BaseType::Float32 => "float".to_string(),
        BaseType::Bytes => "bytes".to_string(),
        BaseType::Unit => "None".to_string(),
        // Python typings emit flat classes, so nested types use the parent-qualified name (e.g.
        // `GenerateTemporaryTableCredentialsRequestOperation`) to stay collision-free. Top-level
        // types are unaffected (qualified == simple).
        BaseType::Message(name) => extract_qualified_type_name(name),
        BaseType::Enum(name) => extract_qualified_type_name(name),
        BaseType::OneOf(name) => extract_qualified_type_name(name),
        BaseType::Map(key_type, value_type) => {
            let key_str = unified_to_python_type(key_type);
            let value_str = unified_to_python_type(value_type);
            format!("Dict[{key_str}, {value_str}]")
        }
    };

    let mut result = base_type_str;

    if unified_type.is_repeated {
        result = format!("List[{result}]");
    }

    if unified_type.is_optional {
        result = format!("Optional[{result}]");
    }

    result
}

/// Convert a unified type to a Rust NAPI parameter type string.
///
/// NAPI requires concrete types (no `impl Into<>`). Optional fields
/// become `Option<T>`, maps become `Option<HashMap<K, V>>`.
pub fn unified_to_napi(unified_type: &UnifiedType) -> String {
    let base_type_str = match &unified_type.base_type {
        BaseType::String => "String".to_string(),
        BaseType::Int32 => "i32".to_string(),
        BaseType::Int64 => "i64".to_string(),
        BaseType::Bool => "bool".to_string(),
        BaseType::Float64 => "f64".to_string(),
        BaseType::Float32 => "f32".to_string(),
        BaseType::Bytes => "Vec<u8>".to_string(),
        BaseType::Unit => "()".to_string(),
        BaseType::Message(name) => extract_simple_type_name(name),
        BaseType::Enum(name) => convert_protobuf_enum_to_rust_type(&format!("TYPE_ENUM:{name}")),
        BaseType::OneOf(name) => extract_simple_type_name(name),
        BaseType::Map(key_type, value_type) => {
            let key_str = unified_to_napi(key_type);
            let value_str = unified_to_napi(value_type);
            format!("HashMap<{key_str}, {value_str}>")
        }
    };

    let mut result = base_type_str;

    if unified_type.is_repeated {
        result = format!("Vec<{result}>");
    }

    if unified_type.is_optional
        || matches!(unified_type.base_type, BaseType::Map(_, _))
        || unified_type.is_repeated
    {
        result = format!("Option<{result}>");
    }

    result
}

/// Convert a unified type to a TypeScript type annotation string.
///
/// Enums are mapped to `number` since they cross the NAPI boundary as `i32`.
pub fn unified_to_typescript(unified_type: &UnifiedType) -> String {
    let base_type_str = match &unified_type.base_type {
        BaseType::String => "string".to_string(),
        BaseType::Int32 | BaseType::Int64 => "number".to_string(),
        BaseType::Bool => "boolean".to_string(),
        BaseType::Float64 | BaseType::Float32 => "number".to_string(),
        BaseType::Bytes => "Uint8Array".to_string(),
        BaseType::Unit => "void".to_string(),
        BaseType::Message(name) => extract_simple_type_name(name),
        BaseType::Enum(_) => "number".to_string(),
        BaseType::OneOf(name) => extract_simple_type_name(name),
        BaseType::Map(key_type, value_type) => {
            let key_str = unified_to_typescript(key_type);
            let value_str = unified_to_typescript(value_type);
            format!("Record<{key_str}, {value_str}>")
        }
    };

    let mut result = base_type_str;

    if unified_type.is_repeated {
        result = format!("{result}[]");
    }

    if unified_type.is_optional {
        result = format!("{result} | undefined");
    }

    result
}

fn flexible_optional_field_assignment(
    unified_type: &UnifiedType,
    field_ident: &proc_macro2::Ident,
    runtime: Runtime,
) -> TokenStream {
    if unified_type.is_optional {
        match (&unified_type.base_type, runtime) {
            (BaseType::Enum(_), Runtime::Prost) => quote! { #field_ident.into().map(|e| e as i32) },
            (BaseType::Enum(_), Runtime::Buffa) => {
                quote! { #field_ident.into().map(buffa::EnumValue::Known) }
            }
            // buffa stores singular message/oneof fields as `MessageField<T>`. The
            // setter arg is `impl Into<Option<T>>`, so resolve to `Option<T>` first
            // (annotating the intermediate disambiguates the chained `.into()`), then
            // convert via `MessageField: From<Option<T>>`.
            (BaseType::Message(_) | BaseType::OneOf(_), Runtime::Buffa) => quote! {
                {
                    let #field_ident: ::core::option::Option<_> = #field_ident.into();
                    buffa::MessageField::from(#field_ident)
                }
            },
            _ => quote! { #field_ident.into() },
        }
    } else {
        match (&unified_type.base_type, runtime) {
            (BaseType::String, _) => quote! { #field_ident.into() },
            (
                BaseType::Int32
                | BaseType::Int64
                | BaseType::Bool
                | BaseType::Float64
                | BaseType::Float32,
                _,
            ) => {
                quote! { #field_ident.into() }
            }
            (BaseType::Enum(_), Runtime::Prost) => quote! { #field_ident as i32 },
            (BaseType::Enum(_), Runtime::Buffa) => quote! { buffa::EnumValue::Known(#field_ident) },
            // Message, OneOf, and Map types are always handled by their own
            // dedicated branches in `builder_with_impl` and never reach this path.
            _ => quote! { #field_ident },
        }
    }
}

/// Convert protobuf enum type to Rust type
///
/// # FIXME: Fragile heuristic for nested vs package-level enums
///
/// This function guesses whether an enum is nested inside a message (e.g.
/// `example.Catalog.Status`) or defined at package level (e.g.
/// `example.catalog.v1.CatalogType`) by inspecting the casing of the last
/// path segment before the enum name.  The special-case for `"V1"` and the
/// all-lowercase check are workarounds for common proto package naming
/// conventions, not a principled solution.
///
/// The correct fix (Phase 3): accept a `package_prefix: &str` parameter so
/// callers can strip the known package prefix and always identify the boundary
/// between package segments and message/enum names deterministically.
pub(crate) fn convert_protobuf_enum_to_rust_type(proto_type: &str) -> String {
    if let Some(enum_name) = proto_type.strip_prefix("TYPE_ENUM:") {
        // Remove leading dot if present
        let enum_name = enum_name.trim_start_matches('.');

        // Check if this is a nested enum (has a parent message)
        // Nested enums have PascalCase message names before the enum name
        if let Some(last_dot) = enum_name.rfind('.') {
            let parent_part = &enum_name[..last_dot];
            let enum_simple_name = &enum_name[last_dot + 1..];

            // Check if the parent part ends with a PascalCase message name (nested enum)
            // vs. a package name like "v1" (package-level enum)
            let parent_parts: Vec<&str> = parent_part.split('.').collect();
            if let Some(last_part) = parent_parts.last() {
                // If the last part starts with uppercase, it's likely a message name (nested enum)
                // If it's "v1" or similar version, it's a package-level enum
                if last_part.chars().next().is_some_and(|c| c.is_uppercase())
                    && *last_part != "V1"
                    && !last_part
                        .chars()
                        .all(|c| c.is_lowercase() || c.is_numeric())
                {
                    // This is a nested enum - convert parent message to snake_case module
                    let snake_case_module = last_part.to_case(Case::Snake);

                    // Convert enum name from UPPER_SNAKE_CASE to PascalCase if needed
                    let enum_rust_name = if enum_simple_name.contains('_')
                        && enum_simple_name
                            .chars()
                            .all(|c| c.is_uppercase() || c == '_')
                    {
                        enum_simple_name.to_case(Case::Pascal)
                    } else {
                        enum_simple_name.to_string()
                    };

                    format!("{snake_case_module}::{enum_rust_name}")
                } else {
                    // This is a package-level enum - just use the simple name
                    convert_enum_name_to_rust(enum_simple_name)
                }
            } else {
                // Fallback to simple enum name
                convert_enum_name_to_rust(enum_simple_name)
            }
        } else {
            // Simple enum name without dots
            convert_enum_name_to_rust(enum_name)
        }
    } else {
        "i32".to_string() // Fallback for unknown enum types
    }
}

/// Convert enum name from proto format to Rust format
fn convert_enum_name_to_rust(enum_name: &str) -> String {
    // Convert from proto naming to Rust naming if needed
    if enum_name.contains('_') && enum_name.chars().all(|c| c.is_uppercase() || c == '_') {
        enum_name.to_case(Case::Pascal)
    } else {
        enum_name.to_string()
    }
}

/// A language-neutral description of a protobuf field's type.
///
/// `UnifiedType` is the bridge between parsing and generation: a proto field's type and
/// cardinality are normalized into this single shape once, then each language backend renders it
/// via a [`TypeRenderer`] (e.g. [`unified_to_rust`]). The [`base_type`](Self::base_type) carries
/// the underlying category while [`is_optional`](Self::is_optional) and
/// [`is_repeated`](Self::is_repeated) describe the cardinality wrapping applied around it.
#[derive(Debug, Clone)]
pub struct UnifiedType {
    /// The underlying type category.
    pub base_type: BaseType,
    /// Whether this type is optional (`Option<T>` in Rust).
    pub is_optional: bool,
    /// Whether this type is repeated (`Vec<T>` in Rust).
    pub is_repeated: bool,
}

/// The underlying category of a [`UnifiedType`], independent of optional/repeated wrapping.
///
/// Scalars map directly to a primitive in each target language; the named variants
/// ([`Message`](Self::Message), [`Enum`](Self::Enum), [`OneOf`](Self::OneOf)) carry the
/// fully-qualified protobuf type name, from which renderers take the final segment.
#[derive(Debug, Clone)]
pub enum BaseType {
    /// String type
    String,
    /// 32-bit integer
    Int32,
    /// 64-bit integer
    Int64,
    /// Boolean
    Bool,
    /// 64-bit float
    Float64,
    /// 32-bit float
    Float32,
    /// Byte array
    Bytes,
    /// Protobuf message type
    Message(String),
    /// Protobuf enum type
    Enum(String),
    /// Protobuf oneof type
    OneOf(String),
    /// Map type with key and value types
    Map(Box<UnifiedType>, Box<UnifiedType>),
    /// Unit/void type
    Unit,
}

impl UnifiedType {
    /// Extract a syn::Ident for the inner type name (last segment of a qualified name).
    pub fn type_ident(&self) -> syn::Ident {
        let name = match &self.base_type {
            BaseType::Message(n) | BaseType::Enum(n) | BaseType::OneOf(n) => {
                n.split('.').next_back().unwrap_or(n)
            }
            BaseType::String => "String",
            BaseType::Int32 => "i32",
            BaseType::Int64 => "i64",
            BaseType::Bool => "bool",
            BaseType::Float64 => "f64",
            BaseType::Float32 => "f32",
            BaseType::Bytes => "Bytes",
            BaseType::Unit => "()",
            BaseType::Map(_, _) => "HashMap",
        };
        quote::format_ident!("{}", name)
    }

    /// Create a simple string type
    pub fn string() -> Self {
        Self {
            base_type: BaseType::String,
            is_optional: false,
            is_repeated: false,
        }
    }

    /// Create an optional version of this type
    pub fn optional(mut self) -> Self {
        self.is_optional = true;
        self
    }

    /// Create a repeated version of this type
    pub fn repeated(mut self) -> Self {
        self.is_repeated = true;
        self
    }

    /// Create a map type
    pub fn map(key: UnifiedType, value: UnifiedType) -> Self {
        Self {
            base_type: BaseType::Map(Box::new(key), Box::new(value)),
            is_optional: false,
            is_repeated: false,
        }
    }
}