buffa-reflect 0.2.0

Runtime reflection for the buffa protobuf implementation.
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
//! Textproto parser (logos-tokenized stream + recursive-descent).
//!
//! Handles scalars, repeated entries (one-per-line and `[a, b, c]`
//! short form), nested messages (`field { ... }`), maps (as repeated
//! entries with `key:` / `value:`), enum names and numbers, all C-style
//! escapes inside string/bytes literals, and `# to-EOL` comments.

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

use buffa::bytes::Bytes;
use logos::{Lexer, Logos};

use crate::{
    dynamic::{
        message::DynamicMessage,
        value::{MapKey, Value},
    },
    field::{FieldDescriptor, Kind},
    message::MessageDescriptor,
};

/// Parse error raised by [`crate::DynamicMessage::parse_text_format`].
#[derive(Debug)]
pub struct ParseError {
    /// 1-based source line.
    pub line: u32,
    /// 1-based source column.
    pub col: u32,
    /// Underlying parser error kind.
    pub kind: ParseErrorKind,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}: {:?}", self.line, self.col, self.kind)
    }
}

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

/// Tagged reasons a textproto parse can fail.
#[derive(Debug)]
pub enum ParseErrorKind {
    /// Lexer-level error (invalid token).
    InvalidToken,
    /// Unexpected token where another was required.
    UnexpectedToken(String),
    /// Reached EOF before the parse was complete.
    UnexpectedEof,
    /// Field name didn't match anything on the descriptor.
    UnknownField(String),
    /// Schema-level rejection (type mismatch, invalid enum name, etc.).
    Schema(String),
}

#[derive(Logos, Debug, PartialEq, Clone)]
#[logos(skip r"[ \t\r\n]+")]
#[logos(skip(r"#[^\n]*", allow_greedy = true))]
enum Token<'a> {
    #[regex(r"[a-zA-Z_][a-zA-Z0-9_]*", |lex| lex.slice())]
    Ident(&'a str),

    #[regex(r"-?(?:0[xX][0-9a-fA-F]+|0[0-7]+|[0-9]+)", |lex| lex.slice())]
    Int(&'a str),

    // Floats: optional sign, integer, fractional, exponent.
    #[regex(r"-?[0-9]+\.[0-9]*([eE][-+]?[0-9]+)?", |lex| lex.slice())]
    #[regex(r"-?\.[0-9]+([eE][-+]?[0-9]+)?", |lex| lex.slice())]
    #[regex(r"-?[0-9]+[eE][-+]?[0-9]+", |lex| lex.slice())]
    Float(&'a str),

    // String literal — captured raw (including quotes); we decode escapes
    // separately in `decode_string`.
    #[regex(r#""([^"\\]|\\.)*""#, |lex| lex.slice())]
    #[regex(r#"'([^'\\]|\\.)*'"#, |lex| lex.slice())]
    StringLit(&'a str),

    #[token(":")]
    Colon,
    #[token("{")]
    LBrace,
    #[token("}")]
    RBrace,
    #[token("<")]
    LAngle,
    #[token(">")]
    RAngle,
    #[token("[")]
    LBracket,
    #[token("]")]
    RBracket,
    #[token(",")]
    Comma,
    #[token(";")]
    Semicolon,
    #[token(".")]
    Dot,
    #[token("/")]
    Slash,
}

struct Parser<'a> {
    src: &'a str,
    lex: Lexer<'a, Token<'a>>,
    peeked: Option<Token<'a>>,
}

impl<'a> Parser<'a> {
    fn new(src: &'a str) -> Self {
        Self {
            src,
            lex: Token::lexer(src),
            peeked: None,
        }
    }

    fn line_col(&self) -> (u32, u32) {
        // Compute current 1-based line/col from byte offset.
        let span = self.lex.span();
        let pos = span.start.min(self.src.len());
        let mut line = 1u32;
        let mut col = 1u32;
        for ch in self.src[..pos].chars() {
            if ch == '\n' {
                line += 1;
                col = 1;
            } else {
                col += 1;
            }
        }
        (line, col)
    }

    fn err(&self, kind: ParseErrorKind) -> ParseError {
        let (line, col) = self.line_col();
        ParseError { line, col, kind }
    }

    fn peek(&mut self) -> Result<Option<&Token<'a>>, ParseError> {
        if self.peeked.is_some() {
            return Ok(self.peeked.as_ref());
        }
        match self.lex.next() {
            None => Ok(None),
            Some(Ok(tok)) => {
                self.peeked = Some(tok);
                Ok(self.peeked.as_ref())
            }
            Some(Err(_)) => Err(self.err(ParseErrorKind::InvalidToken)),
        }
    }

    fn next(&mut self) -> Result<Option<Token<'a>>, ParseError> {
        if self.peeked.is_some() {
            return Ok(self.peeked.take());
        }
        match self.lex.next() {
            None => Ok(None),
            Some(Ok(tok)) => Ok(Some(tok)),
            Some(Err(_)) => Err(self.err(ParseErrorKind::InvalidToken)),
        }
    }

    fn require(&mut self) -> Result<Token<'a>, ParseError> {
        self.next()?
            .ok_or_else(|| self.err(ParseErrorKind::UnexpectedEof))
    }

    fn expect(&mut self, expected: Token<'a>) -> Result<(), ParseError> {
        let actual = self.require()?;
        if actual == expected {
            Ok(())
        } else {
            Err(self.err(ParseErrorKind::UnexpectedToken(format!(
                "expected {expected:?}, got {actual:?}"
            ))))
        }
    }
}

/// Parser entry point.
pub(super) fn parse(
    descriptor: MessageDescriptor,
    input: &str,
) -> Result<DynamicMessage, ParseError> {
    let mut parser = Parser::new(input);
    let mut msg = DynamicMessage::new(descriptor.clone());
    parse_message_body(
        &mut parser,
        &descriptor,
        &mut msg,
        /* until_brace */ false,
    )?;
    Ok(msg)
}

fn parse_message_body(
    parser: &mut Parser<'_>,
    descriptor: &MessageDescriptor,
    msg: &mut DynamicMessage,
    until_brace: bool,
) -> Result<(), ParseError> {
    loop {
        let tok = match parser.peek()? {
            Some(t) => t.clone(),
            None => {
                if until_brace {
                    return Err(parser.err(ParseErrorKind::UnexpectedEof));
                }
                return Ok(());
            }
        };
        if until_brace && matches!(tok, Token::RBrace) {
            parser.next()?;
            return Ok(());
        }
        match tok {
            Token::Ident(name) => {
                parser.next()?;
                let field = descriptor
                    .get_field_by_name(name)
                    .or_else(|| descriptor.get_field_by_json_name(name))
                    .ok_or_else(|| parser.err(ParseErrorKind::UnknownField(name.to_string())))?;
                parse_field_value(parser, &field, msg)?;
            }
            Token::Semicolon => {
                parser.next()?;
            }
            other => {
                return Err(parser.err(ParseErrorKind::UnexpectedToken(format!(
                    "expected field name, got {other:?}"
                ))));
            }
        }
    }
}

fn parse_field_value(
    parser: &mut Parser<'_>,
    field: &FieldDescriptor,
    msg: &mut DynamicMessage,
) -> Result<(), ParseError> {
    // Optional `:` (required for scalars; optional before `{`).
    let next = parser.peek()?.cloned();
    let is_brace = matches!(next, Some(Token::LBrace) | Some(Token::LAngle));
    if !is_brace {
        parser.expect(Token::Colon)?;
    }

    if field.is_map() {
        return parse_map_entry(parser, field, msg);
    }
    if field.is_list() {
        // Repeated: either `[a, b, c]` short form or single value.
        let next = parser.peek()?;
        if let Some(Token::LBracket) = next {
            parser.next()?;
            let kind = field.kind();
            loop {
                let v = parse_singular_value(parser, &kind, &field.kind())?;
                push_list(msg, field, v);
                let sep = parser.next()?;
                match sep {
                    Some(Token::Comma) => continue,
                    Some(Token::RBracket) => break,
                    other => {
                        return Err(parser.err(ParseErrorKind::UnexpectedToken(format!(
                            "expected `,` or `]`, got {other:?}"
                        ))));
                    }
                }
            }
            return Ok(());
        }
        let kind = field.kind();
        let v = parse_singular_value(parser, &kind, &field.kind())?;
        push_list(msg, field, v);
        return Ok(());
    }

    let kind = field.kind();
    let v = parse_singular_value(parser, &kind, &field.kind())?;
    msg.try_set_field(field, v)
        .map_err(|e| parser.err(ParseErrorKind::Schema(e.to_string())))?;
    Ok(())
}

fn parse_map_entry(
    parser: &mut Parser<'_>,
    field: &FieldDescriptor,
    msg: &mut DynamicMessage,
) -> Result<(), ParseError> {
    let opener = parser.require()?;
    let closer = match opener {
        Token::LBrace => Token::RBrace,
        Token::LAngle => Token::RAngle,
        other => {
            return Err(parser.err(ParseErrorKind::UnexpectedToken(format!(
                "expected `{{` or `<`, got {other:?}"
            ))));
        }
    };
    let (key_kind, value_kind) = crate::dynamic::value::map_entry_kinds(field)
        .ok_or_else(|| parser.err(ParseErrorKind::Schema("not a map field".into())))?;
    let mut key: Option<Value> = None;
    let mut value: Option<Value> = None;
    loop {
        let next = parser.require()?;
        if next == closer {
            break;
        }
        let name = match next {
            Token::Ident(n) => n,
            other => {
                return Err(parser.err(ParseErrorKind::UnexpectedToken(format!(
                    "expected `key`/`value`, got {other:?}"
                ))));
            }
        };
        parser.expect(Token::Colon)?;
        match name {
            "key" => key = Some(parse_singular_value(parser, &key_kind, &key_kind)?),
            "value" => value = Some(parse_singular_value(parser, &value_kind, &value_kind)?),
            other => {
                return Err(parser.err(ParseErrorKind::UnknownField(other.to_string())));
            }
        }
    }
    let key = key.unwrap_or_else(|| Value::default_value(&key_kind));
    let value = value.unwrap_or_else(|| Value::default_value(&value_kind));
    let mk = value_to_map_key(&key)
        .ok_or_else(|| parser.err(ParseErrorKind::Schema("invalid map key".into())))?;

    if !msg.has_field(field) {
        msg.set_field(field, Value::Map(HashMap::new()));
    }
    if let Some(Value::Map(m)) = msg.get_field_by_name_mut(field.name()) {
        m.insert(mk, value);
    }
    Ok(())
}

fn value_to_map_key(value: &Value) -> Option<MapKey> {
    Some(match value {
        Value::Bool(b) => MapKey::Bool(*b),
        Value::I32(v) => MapKey::I32(*v),
        Value::I64(v) => MapKey::I64(*v),
        Value::U32(v) => MapKey::U32(*v),
        Value::U64(v) => MapKey::U64(*v),
        Value::String(s) => MapKey::String(s.clone()),
        _ => return None,
    })
}

fn push_list(msg: &mut DynamicMessage, field: &FieldDescriptor, v: Value) {
    if !msg.has_field(field) {
        msg.set_field(field, Value::List(Vec::new()));
    }
    if let Some(Value::List(l)) = msg.get_field_by_name_mut(field.name()) {
        l.push(v);
    }
}

fn parse_singular_value(
    parser: &mut Parser<'_>,
    kind: &Kind,
    _field_kind: &Kind,
) -> Result<Value, ParseError> {
    if let Kind::Message(d) = kind {
        let opener = parser.require()?;
        let closer = match opener {
            Token::LBrace => Token::RBrace,
            Token::LAngle => Token::RAngle,
            other => {
                return Err(parser.err(ParseErrorKind::UnexpectedToken(format!(
                    "expected `{{` or `<` for sub-message, got {other:?}"
                ))));
            }
        };
        let mut inner = DynamicMessage::new(d.clone());
        parse_message_until(parser, d, &mut inner, &closer)?;
        return Ok(Value::Message(inner));
    }
    let tok = parser.require()?;
    match (kind, tok) {
        (Kind::Bool, Token::Ident("true")) => Ok(Value::Bool(true)),
        (Kind::Bool, Token::Ident("false")) => Ok(Value::Bool(false)),
        (Kind::Int32 | Kind::Sint32 | Kind::Sfixed32, Token::Int(s)) => {
            Ok(Value::I32(parse_signed_int(s)? as i32))
        }
        (Kind::Int64 | Kind::Sint64 | Kind::Sfixed64, Token::Int(s)) => {
            Ok(Value::I64(parse_signed_int(s)?))
        }
        (Kind::Uint32 | Kind::Fixed32, Token::Int(s)) => {
            Ok(Value::U32(parse_unsigned_int(s)? as u32))
        }
        (Kind::Uint64 | Kind::Fixed64, Token::Int(s)) => Ok(Value::U64(parse_unsigned_int(s)?)),
        (Kind::Float | Kind::Double, Token::Int(s)) => {
            let v: f64 = s
                .parse()
                .map_err(|_| parser.err(ParseErrorKind::Schema(format!("invalid number `{s}`"))))?;
            if matches!(kind, Kind::Float) {
                Ok(Value::F32(v as f32))
            } else {
                Ok(Value::F64(v))
            }
        }
        (Kind::Float | Kind::Double, Token::Float(s)) => {
            let v: f64 = s
                .parse()
                .map_err(|_| parser.err(ParseErrorKind::Schema(format!("invalid float `{s}`"))))?;
            if matches!(kind, Kind::Float) {
                Ok(Value::F32(v as f32))
            } else {
                Ok(Value::F64(v))
            }
        }
        (Kind::Float | Kind::Double, Token::Ident(name)) => {
            let v: f64 = match name {
                "nan" | "NaN" => f64::NAN,
                "inf" | "infinity" | "Infinity" => f64::INFINITY,
                _ => {
                    return Err(parser.err(ParseErrorKind::Schema(format!(
                        "invalid float ident `{name}`"
                    ))));
                }
            };
            if matches!(kind, Kind::Float) {
                Ok(Value::F32(v as f32))
            } else {
                Ok(Value::F64(v))
            }
        }
        (Kind::String, Token::StringLit(raw)) => {
            let bytes = decode_string(raw).map_err(|e| parser.err(ParseErrorKind::Schema(e)))?;
            String::from_utf8(bytes)
                .map(Value::String)
                .map_err(|_| parser.err(ParseErrorKind::Schema("invalid UTF-8 in string".into())))
        }
        (Kind::Bytes, Token::StringLit(raw)) => {
            let bytes = decode_string(raw).map_err(|e| parser.err(ParseErrorKind::Schema(e)))?;
            Ok(Value::Bytes(Bytes::from(bytes)))
        }
        (Kind::Enum(d), Token::Ident(name)) => match d.values().find(|v| v.name() == name) {
            Some(v) => Ok(Value::EnumNumber(v.number())),
            None => Err(parser.err(ParseErrorKind::Schema(format!(
                "unknown enum variant `{name}`"
            )))),
        },
        (Kind::Enum(_), Token::Int(s)) => Ok(Value::EnumNumber(parse_signed_int(s)? as i32)),
        (kind, tok) => Err(parser.err(ParseErrorKind::UnexpectedToken(format!(
            "kind={kind:?} got {tok:?}"
        )))),
    }
}

fn parse_message_until(
    parser: &mut Parser<'_>,
    descriptor: &MessageDescriptor,
    msg: &mut DynamicMessage,
    closer: &Token<'_>,
) -> Result<(), ParseError> {
    loop {
        let tok = parser.peek()?.cloned();
        match tok {
            Some(t) if &t == closer => {
                parser.next()?;
                return Ok(());
            }
            Some(Token::Ident(name)) => {
                parser.next()?;
                let field = descriptor
                    .get_field_by_name(name)
                    .or_else(|| descriptor.get_field_by_json_name(name))
                    .ok_or_else(|| parser.err(ParseErrorKind::UnknownField(name.to_string())))?;
                parse_field_value(parser, &field, msg)?;
            }
            Some(Token::Semicolon) => {
                parser.next()?;
            }
            Some(other) => {
                return Err(parser.err(ParseErrorKind::UnexpectedToken(format!(
                    "expected field name, got {other:?}"
                ))));
            }
            None => return Err(parser.err(ParseErrorKind::UnexpectedEof)),
        }
    }
}

fn parse_signed_int(raw: &str) -> Result<i64, ParseError> {
    let (negative, body) = match raw.strip_prefix('-') {
        Some(rest) => (true, rest),
        None => (false, raw),
    };
    let v = parse_unsigned_int(body)? as i128;
    let v = if negative { -v } else { v };
    if v < i64::MIN as i128 || v > i64::MAX as i128 {
        return Err(ParseError {
            line: 0,
            col: 0,
            kind: ParseErrorKind::Schema(format!("integer out of range: {raw}")),
        });
    }
    Ok(v as i64)
}

fn parse_unsigned_int(raw: &str) -> Result<u64, ParseError> {
    if let Some(hex) = raw.strip_prefix("0x").or_else(|| raw.strip_prefix("0X")) {
        return u64::from_str_radix(hex, 16).map_err(|e| ParseError {
            line: 0,
            col: 0,
            kind: ParseErrorKind::Schema(format!("invalid hex `{raw}`: {e}")),
        });
    }
    if raw.starts_with('0') && raw.len() > 1 && raw.chars().all(|c| c.is_ascii_digit()) {
        return u64::from_str_radix(&raw[1..], 8).map_err(|e| ParseError {
            line: 0,
            col: 0,
            kind: ParseErrorKind::Schema(format!("invalid octal `{raw}`: {e}")),
        });
    }
    raw.parse::<u64>().map_err(|e| ParseError {
        line: 0,
        col: 0,
        kind: ParseErrorKind::Schema(format!("invalid integer `{raw}`: {e}")),
    })
}

fn decode_string(raw: &str) -> Result<Vec<u8>, String> {
    // Strip leading/trailing quote (single or double).
    let bytes = raw.as_bytes();
    if bytes.len() < 2 {
        return Err("string literal too short".into());
    }
    let inner = &raw[1..raw.len() - 1];
    crate::dynamic::defaults::parse_default_value(inner, &crate::pool::KindRef::Bytes, None)
        .and_then(|v| match v {
            Value::Bytes(b) => Ok(b.to_vec()),
            _ => Err("internal: expected Bytes".into()),
        })
}