oxiproto-build 0.1.2

Pure Rust .proto → Rust codegen (no protoc required)
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
#![forbid(unsafe_code)]

//! Name resolution for the native parser.
//!
//! Takes a parsed [`ProtoFile`] AST and resolves all type references
//! (`FieldType::Named(s)`, RPC input/output types) to leading-dot
//! fully-qualified names such as `.my.pkg.Foo`.
//!
//! Also performs a duplicate field-number check across each message body.

use std::collections::{HashMap, HashSet};

use crate::parser::{
    ast::{Field, FieldType, Message, Method, ProtoFile, Service},
    error::ParseError,
    span::Span,
};
// ExtendBlock is used in the resolve path for symbol collection
use crate::parser::ast::ExtendBlock;

#[cfg(feature = "native-parser")]
use crate::parser::loader::FileSymbols;

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Resolve all type references in `proto_file` to fully-qualified leading-dot
/// paths and verify that no two fields in the same message share a field
/// number.
///
/// Returns a cloned [`ProtoFile`] with all `FieldType::Named(s)` strings
/// replaced by their fully-qualified form, or a [`ParseError`] on the first
/// resolution or validation failure.
pub fn resolve(proto_file: &ProtoFile) -> Result<ProtoFile, ParseError> {
    let mut resolved = proto_file.clone();

    // Build the symbol table (FQNs for all defined types + enum FQN set).
    let pkg = proto_file.package.as_deref().unwrap_or("");
    let mut symbols: HashSet<String> = HashSet::new();
    let mut enum_fqns: HashSet<String> = HashSet::new();

    for msg in &proto_file.messages {
        collect_message_symbols(msg, pkg, &mut symbols, &mut enum_fqns);
    }
    for en in &proto_file.enums {
        let fqn = make_fqn(pkg, &[], &en.name);
        symbols.insert(fqn.clone());
        enum_fqns.insert(fqn);
    }

    let has_imports = !proto_file.imports.is_empty();

    // Resolve top-level messages.
    for msg in &mut resolved.messages {
        resolve_message(msg, pkg, &[], &symbols, has_imports)?;
    }

    // Resolve top-level services.
    for svc in &mut resolved.services {
        resolve_service(svc, pkg, &symbols, has_imports)?;
    }

    // Resolve field types inside extend blocks.
    for eb in &mut resolved.extends {
        resolve_extend_block(eb, pkg, &symbols, has_imports)?;
    }

    Ok(resolved)
}

/// Resolve field types inside a top-level extend block.
fn resolve_extend_block(
    eb: &mut ExtendBlock,
    pkg: &str,
    symbols: &HashSet<String>,
    has_imports: bool,
) -> Result<(), ParseError> {
    for field in &mut eb.fields {
        resolve_field(field, pkg, &[], symbols, has_imports)?;
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Symbol collection helpers
// ---------------------------------------------------------------------------

fn make_fqn(pkg: &str, scope: &[&str], name: &str) -> String {
    let mut fqn = String::with_capacity(
        1 + pkg.len() + scope.iter().map(|s| s.len() + 1).sum::<usize>() + name.len() + 1,
    );
    fqn.push('.');
    if !pkg.is_empty() {
        fqn.push_str(pkg);
        fqn.push('.');
    }
    for part in scope {
        fqn.push_str(part);
        fqn.push('.');
    }
    fqn.push_str(name);
    fqn
}

fn collect_message_symbols(
    msg: &Message,
    pkg: &str,
    symbols: &mut HashSet<String>,
    enum_fqns: &mut HashSet<String>,
) {
    collect_message_symbols_scoped(msg, pkg, &[], symbols, enum_fqns);
}

fn collect_message_symbols_scoped(
    msg: &Message,
    pkg: &str,
    scope: &[&str],
    symbols: &mut HashSet<String>,
    enum_fqns: &mut HashSet<String>,
) {
    let fqn = make_fqn(pkg, scope, &msg.name);
    symbols.insert(fqn.clone());

    // Build the new scope by appending the current message name.
    let mut new_scope: Vec<&str> = scope.to_vec();
    new_scope.push(&msg.name);
    let new_scope_ref: Vec<&str> = new_scope.clone();

    for nested in &msg.nested_messages {
        collect_message_symbols_scoped(nested, pkg, &new_scope_ref, symbols, enum_fqns);
    }
    for en in &msg.nested_enums {
        let en_fqn = make_fqn(pkg, &new_scope_ref, &en.name);
        symbols.insert(en_fqn.clone());
        enum_fqns.insert(en_fqn);
    }
}

// ---------------------------------------------------------------------------
// Resolution helpers
// ---------------------------------------------------------------------------

/// Attempt to resolve a bare (non-dot-prefixed) type name `s` from the given
/// innermost scope outward, using the symbol table.  Returns the first
/// fully-qualified name found, or `None` if no match exists.
fn lookup(name: &str, pkg: &str, scope: &[&str], symbols: &HashSet<String>) -> Option<String> {
    // Walk from innermost scope to outermost, then to package-root, then root.
    let scope_depth = scope.len();
    for depth in (0..=scope_depth).rev() {
        let candidate = make_fqn(pkg, &scope[..depth], name);
        if symbols.contains(&candidate) {
            return Some(candidate);
        }
    }
    None
}

fn resolve_field_type(
    ty: &mut FieldType,
    pkg: &str,
    scope: &[&str],
    symbols: &HashSet<String>,
    has_imports: bool,
    span: &Span,
) -> Result<(), ParseError> {
    match ty {
        FieldType::Scalar(_) => {}
        FieldType::Map { key: _, value } => {
            resolve_field_type(value.as_mut(), pkg, scope, symbols, has_imports, span)?;
        }
        FieldType::Named(s) | FieldType::Group(s) => {
            if s.starts_with('.') {
                // Already fully qualified — leave as-is.
            } else {
                match lookup(s.as_str(), pkg, scope, symbols) {
                    Some(fqn) => *s = fqn,
                    None if has_imports => {
                        // Best-guess: apply leading dot + package prefix.
                        // The type might come from an imported file.
                        let guessed = if pkg.is_empty() {
                            format!(".{s}")
                        } else {
                            format!(".{pkg}.{s}")
                        };
                        *s = guessed;
                    }
                    None => {
                        return Err(ParseError::UnexpectedToken {
                            expected: "known type".to_owned(),
                            found: s.clone(),
                            span: *span,
                        });
                    }
                }
            }
        }
    }
    Ok(())
}

fn resolve_field(
    field: &mut Field,
    pkg: &str,
    scope: &[&str],
    symbols: &HashSet<String>,
    has_imports: bool,
) -> Result<(), ParseError> {
    resolve_field_type(&mut field.ty, pkg, scope, symbols, has_imports, &field.span)
}

fn check_duplicate_field_numbers(fields_all: &[Field], span: &Span) -> Result<(), ParseError> {
    let mut seen: HashMap<i32, &str> = HashMap::new();
    for f in fields_all {
        if let Some(prev_name) = seen.insert(f.number, &f.name) {
            return Err(ParseError::UnexpectedToken {
                expected: format!(
                    "unique field number (field '{}' already uses number {})",
                    prev_name, f.number
                ),
                found: format!("field '{}' also uses number {}", f.name, f.number),
                span: *span,
            });
        }
    }
    Ok(())
}

fn resolve_message(
    msg: &mut Message,
    pkg: &str,
    scope: &[&str],
    symbols: &HashSet<String>,
    has_imports: bool,
) -> Result<(), ParseError> {
    // We need the name to live long enough; borrow from msg.name.
    let msg_name_ref: &str = &msg.name;

    // Collect all direct fields (regular + oneof members) for dup-number check.
    let mut all_fields: Vec<Field> = msg.fields.clone();
    for oneof in &msg.oneofs {
        all_fields.extend_from_slice(&oneof.fields);
    }
    check_duplicate_field_numbers(&all_fields, &msg.span)?;

    // Build scope with this message name appended.
    let mut inner_scope = scope.to_vec();
    inner_scope.push(msg_name_ref);
    let inner_scope_ref: Vec<&str> = inner_scope.clone();

    // Resolve regular fields.
    for field in &mut msg.fields {
        resolve_field(field, pkg, &inner_scope_ref, symbols, has_imports)?;
    }

    // Resolve oneof member fields.
    for oneof in &mut msg.oneofs {
        for field in &mut oneof.fields {
            resolve_field(field, pkg, &inner_scope_ref, symbols, has_imports)?;
        }
    }

    // Recurse into nested messages.
    for nested in &mut msg.nested_messages {
        resolve_message(nested, pkg, &inner_scope_ref, symbols, has_imports)?;
    }

    Ok(())
}

fn resolve_rpc_type(
    type_name: &mut String,
    pkg: &str,
    symbols: &HashSet<String>,
    has_imports: bool,
    span: &Span,
) -> Result<(), ParseError> {
    if type_name.starts_with('.') {
        return Ok(());
    }
    match lookup(type_name.as_str(), pkg, &[], symbols) {
        Some(fqn) => *type_name = fqn,
        None if has_imports => {
            let guessed = if pkg.is_empty() {
                format!(".{type_name}")
            } else {
                format!(".{pkg}.{type_name}")
            };
            *type_name = guessed;
        }
        None => {
            return Err(ParseError::UnexpectedToken {
                expected: "known message type".to_owned(),
                found: type_name.clone(),
                span: *span,
            });
        }
    }
    Ok(())
}

fn resolve_service(
    svc: &mut Service,
    pkg: &str,
    symbols: &HashSet<String>,
    has_imports: bool,
) -> Result<(), ParseError> {
    for method in &mut svc.methods {
        resolve_method(method, pkg, symbols, has_imports)?;
    }
    Ok(())
}

fn resolve_method(
    method: &mut Method,
    pkg: &str,
    symbols: &HashSet<String>,
    has_imports: bool,
) -> Result<(), ParseError> {
    resolve_rpc_type(
        &mut method.input_type,
        pkg,
        symbols,
        has_imports,
        &method.span,
    )?;
    resolve_rpc_type(
        &mut method.output_type,
        pkg,
        symbols,
        has_imports,
        &method.span,
    )
}

// ---------------------------------------------------------------------------
// Cross-file resolution (native-parser feature)
// ---------------------------------------------------------------------------

/// Lookup a name in the cross-file visible symbol set.
///
/// Searches from innermost scope outward, then tries a root-anchored fallback
/// (e.g. `google.protobuf.Timestamp` → `.google.protobuf.Timestamp`).
#[cfg(feature = "native-parser")]
fn lookup_in_visible(
    name: &str,
    pkg: &str,
    scope: &[&str],
    visible: &FileSymbols,
) -> Option<String> {
    // 1. Innermost scope outward (standard proto lookup priority)
    for depth in (0..=scope.len()).rev() {
        let cand = make_fqn(pkg, &scope[..depth], name);
        if visible.contains(&cand) {
            return Some(cand);
        }
    }
    // 2. Root-anchored fallback for cross-file/cross-package refs
    //    e.g. "google.protobuf.Timestamp" → ".google.protobuf.Timestamp"
    let root = format!(".{name}");
    if visible.contains(&root) {
        return Some(root);
    }
    None
}

#[cfg(feature = "native-parser")]
fn resolve_field_type_with_context(
    ty: &mut FieldType,
    pkg: &str,
    scope: &[&str],
    visible: &FileSymbols,
    span: &Span,
) -> Result<(), ParseError> {
    match ty {
        FieldType::Scalar(_) => {}
        FieldType::Map { key: _, value } => {
            resolve_field_type_with_context(value.as_mut(), pkg, scope, visible, span)?;
        }
        FieldType::Named(s) | FieldType::Group(s) => {
            if s.starts_with('.') {
                // Already fully qualified — leave as-is.
            } else {
                match lookup_in_visible(s.as_str(), pkg, scope, visible) {
                    Some(fqn) => *s = fqn,
                    None => {
                        return Err(ParseError::UnexpectedToken {
                            expected: "known type".to_owned(),
                            found: s.clone(),
                            span: *span,
                        });
                    }
                }
            }
        }
    }
    Ok(())
}

#[cfg(feature = "native-parser")]
fn resolve_field_with_context(
    field: &mut Field,
    pkg: &str,
    scope: &[&str],
    visible: &FileSymbols,
) -> Result<(), ParseError> {
    resolve_field_type_with_context(&mut field.ty, pkg, scope, visible, &field.span)
}

#[cfg(feature = "native-parser")]
fn resolve_message_with_context(
    msg: &mut Message,
    pkg: &str,
    scope: &[&str],
    visible: &FileSymbols,
) -> Result<(), ParseError> {
    let msg_name_ref: &str = &msg.name;

    // Collect all direct fields (regular + oneof members) for dup-number check.
    let mut all_fields: Vec<Field> = msg.fields.clone();
    for oneof in &msg.oneofs {
        all_fields.extend_from_slice(&oneof.fields);
    }
    check_duplicate_field_numbers(&all_fields, &msg.span)?;

    // Build scope with this message name appended.
    let mut inner_scope = scope.to_vec();
    inner_scope.push(msg_name_ref);
    let inner_scope_ref: Vec<&str> = inner_scope.clone();

    // Resolve regular fields.
    for field in &mut msg.fields {
        resolve_field_with_context(field, pkg, &inner_scope_ref, visible)?;
    }

    // Resolve oneof member fields.
    for oneof in &mut msg.oneofs {
        for field in &mut oneof.fields {
            resolve_field_with_context(field, pkg, &inner_scope_ref, visible)?;
        }
    }

    // Recurse into nested messages.
    for nested in &mut msg.nested_messages {
        resolve_message_with_context(nested, pkg, &inner_scope_ref, visible)?;
    }

    Ok(())
}

#[cfg(feature = "native-parser")]
fn resolve_rpc_type_with_context(
    type_name: &mut String,
    pkg: &str,
    visible: &FileSymbols,
    span: &Span,
) -> Result<(), ParseError> {
    if type_name.starts_with('.') {
        return Ok(());
    }
    match lookup_in_visible(type_name.as_str(), pkg, &[], visible) {
        Some(fqn) => *type_name = fqn,
        None => {
            return Err(ParseError::UnexpectedToken {
                expected: "known message type".to_owned(),
                found: type_name.clone(),
                span: *span,
            });
        }
    }
    Ok(())
}

#[cfg(feature = "native-parser")]
fn resolve_service_with_context(
    svc: &mut Service,
    pkg: &str,
    visible: &FileSymbols,
) -> Result<(), ParseError> {
    for method in &mut svc.methods {
        resolve_rpc_type_with_context(&mut method.input_type, pkg, visible, &method.span)?;
        resolve_rpc_type_with_context(&mut method.output_type, pkg, visible, &method.span)?;
    }
    Ok(())
}

/// Resolve all type references in `proto_file` using a pre-built cross-file
/// visible symbol set and verify that no two fields in the same message share
/// a field number.
///
/// Unlike [`resolve`], this function:
/// - Uses the supplied `visible` set for lookup (includes imported symbols).
/// - Errors when a name cannot be resolved (no guess heuristic).
/// - Performs root-anchored fallback (`google.protobuf.Timestamp` →
///   `.google.protobuf.Timestamp`) for cross-package references.
#[cfg(feature = "native-parser")]
pub(crate) fn resolve_with_context(
    proto_file: &ProtoFile,
    visible: &FileSymbols,
    _all_enums: &HashSet<String>,
) -> Result<ProtoFile, ParseError> {
    let mut resolved = proto_file.clone();
    let pkg = proto_file.package.as_deref().unwrap_or("");

    // Resolve top-level messages.
    for msg in &mut resolved.messages {
        resolve_message_with_context(msg, pkg, &[], visible)?;
    }

    // Resolve top-level services.
    for svc in &mut resolved.services {
        resolve_service_with_context(svc, pkg, visible)?;
    }

    // Resolve field types inside extend blocks.
    for eb in &mut resolved.extends {
        for field in &mut eb.fields {
            resolve_field_with_context(field, pkg, &[], visible)?;
        }
    }

    Ok(resolved)
}