udb 0.4.27

Universal Data Broker — a Rust gRPC broker over multiple databases (Postgres, MySQL, SQLite, MongoDB, ClickHouse, Cassandra, MSSQL, Redis, Qdrant, S3, Neo4j, …) with per-tenant RLS, 2PC, sagas, and CDC.
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
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenKind {
    Unknown,
    Ident,
    String,
    Number,
    LBrace,
    RBrace,
    LBracket,
    RBracket,
    LParen,
    RParen,
    Equal,
    Colon,
    Semicolon,
    Comma,
    Dot,
    Slash,
    Minus,
    Eof,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token {
    pub kind: TokenKind,
    pub value: String,
    pub line: usize,
    pub column: usize,
}

impl Token {
    pub fn is_ident(&self, value: &str) -> bool {
        self.kind == TokenKind::Ident && self.value == value
    }
}

#[derive(Debug, Clone)]
pub struct LexError {
    pub file: String,
    pub line: usize,
    pub column: usize,
    pub message: String,
}

impl fmt::Display for LexError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}:{}:{}: {}",
            self.file, self.line, self.column, self.message
        )
    }
}

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

pub struct Lexer<'a> {
    src: &'a [u8],
    pos: usize,
    line: usize,
    col: usize,
    file: String,
}

impl<'a> Lexer<'a> {
    pub fn new(src: &'a [u8], file: impl Into<String>) -> Self {
        Self {
            src,
            pos: 0,
            line: 1,
            col: 1,
            file: file.into(),
        }
    }

    pub fn tokenize(mut self) -> Result<Vec<Token>, LexError> {
        let mut tokens = Vec::new();
        loop {
            let token = self.next_token()?;
            let done = token.kind == TokenKind::Eof;
            tokens.push(token);
            if done {
                return Ok(tokens);
            }
        }
    }

    fn next_token(&mut self) -> Result<Token, LexError> {
        while self.pos < self.src.len() {
            let ch = self.src[self.pos];
            if is_ws(ch) {
                self.advance();
                continue;
            }
            if ch == b'/' && self.peek(1) == b'/' {
                self.skip_line_comment();
                continue;
            }
            if ch == b'/' && self.peek(1) == b'*' {
                self.skip_block_comment()?;
                continue;
            }
            if ch == b'"' || ch == b'\'' {
                return self.read_string(ch);
            }
            if is_digit(ch) || (ch == b'-' && is_digit(self.peek(1))) {
                return Ok(self.read_number());
            }
            if is_ident_start(ch) {
                return Ok(self.read_ident());
            }

            let line = self.line;
            let column = self.col;
            self.advance();
            let kind = match ch {
                b'{' => TokenKind::LBrace,
                b'}' => TokenKind::RBrace,
                b'[' => TokenKind::LBracket,
                b']' => TokenKind::RBracket,
                b'(' => TokenKind::LParen,
                b')' => TokenKind::RParen,
                b'=' => TokenKind::Equal,
                b':' => TokenKind::Colon,
                b';' => TokenKind::Semicolon,
                b',' => TokenKind::Comma,
                b'.' => TokenKind::Dot,
                b'/' => TokenKind::Slash,
                b'-' => TokenKind::Minus,
                b'<' => {
                    self.skip_angle_block();
                    continue;
                }
                b'>' => continue,
                _ => TokenKind::Unknown,
            };
            return Ok(Token {
                kind,
                value: (ch as char).to_string(),
                line,
                column,
            });
        }

        Ok(Token {
            kind: TokenKind::Eof,
            value: String::new(),
            line: self.line,
            column: self.col,
        })
    }

    fn read_ident(&mut self) -> Token {
        let line = self.line;
        let column = self.col;
        let start = self.pos;
        while self.pos < self.src.len() && is_ident_continue(self.src[self.pos]) {
            self.advance();
        }
        let mut value = String::from_utf8_lossy(&self.src[start..self.pos]).to_string();
        // Capture a proto `map<K, V>` type as a single token (e.g.
        // "map<string,int32>") so the parser can record the key/value types.
        // Without this, the generic `<...>` skip in next_token() would discard
        // the type arguments. Scoped to the `map` keyword followed by `<` so the
        // option-aggregate `<...>` syntax (preceded by `=`) is unaffected.
        if value == "map" {
            let saved = (self.pos, self.line, self.col);
            while self.pos < self.src.len() && is_ws(self.src[self.pos]) {
                self.advance();
            }
            if self.pos < self.src.len() && self.src[self.pos] == b'<' {
                let angle_start = self.pos;
                self.consume_angle_block();
                let angle = String::from_utf8_lossy(&self.src[angle_start..self.pos]);
                // Normalise out interior whitespace: "< string, int32 >" → "<string,int32>".
                value.push_str(&angle.split_whitespace().collect::<String>());
            } else {
                // A bare `map` (e.g. a field literally named `map`) — restore.
                self.pos = saved.0;
                self.line = saved.1;
                self.col = saved.2;
            }
        }
        Token {
            kind: TokenKind::Ident,
            value,
            line,
            column,
        }
    }

    /// Consume a balanced `<...>` block starting at the current `<`, leaving
    /// `pos` just past the matching `>`. Used to capture `map<K, V>` types.
    fn consume_angle_block(&mut self) {
        let mut depth = 0usize;
        while self.pos < self.src.len() {
            let ch = self.src[self.pos];
            self.advance();
            match ch {
                b'<' => depth += 1,
                b'>' => {
                    depth -= 1;
                    if depth == 0 {
                        break;
                    }
                }
                _ => {}
            }
        }
    }

    fn read_number(&mut self) -> Token {
        let line = self.line;
        let column = self.col;
        let start = self.pos;
        if self.src[self.pos] == b'-' {
            self.advance();
        }
        while self.pos < self.src.len() {
            let ch = self.src[self.pos];
            if is_digit(ch) || ch == b'.' || ch == b'e' || ch == b'E' {
                self.advance();
            } else {
                break;
            }
        }
        Token {
            kind: TokenKind::Number,
            value: String::from_utf8_lossy(&self.src[start..self.pos]).to_string(),
            line,
            column,
        }
    }

    fn read_string(&mut self, quote: u8) -> Result<Token, LexError> {
        let line = self.line;
        let column = self.col;
        self.advance();
        let mut out = Vec::new();
        while self.pos < self.src.len() {
            let ch = self.src[self.pos];
            if ch == b'\\' && self.pos + 1 < self.src.len() {
                let escape_line = self.line;
                let escape_column = self.col;
                self.advance(); // consume backslash
                let escaped = self.src[self.pos];
                self.advance(); // consume the escape selector itself
                match escaped {
                    b'n' => out.push(b'\n'),
                    b't' => out.push(b'\t'),
                    b'r' => out.push(b'\r'),
                    b'a' => out.push(0x07),
                    b'b' => out.push(0x08),
                    b'f' => out.push(0x0C),
                    b'v' => out.push(0x0B),
                    b'"' => out.push(b'"'),
                    b'\'' => out.push(b'\''),
                    b'\\' => out.push(b'\\'),
                    // `\xHH` — hex byte escape (1–2 hex digits), proto/C style.
                    b'x' | b'X' => {
                        let mut val: u32 = 0;
                        let mut n = 0;
                        while n < 2
                            && self.pos < self.src.len()
                            && let Some(h) = hex_digit(self.src[self.pos])
                        {
                            val = val * 16 + h as u32;
                            self.advance();
                            n += 1;
                        }
                        if n == 0 {
                            out.push(b'\\');
                            out.push(escaped);
                        } else {
                            out.push(val as u8);
                        }
                    }
                    // `\0`–`\377` — octal byte escape (selector is the 1st digit).
                    b'0'..=b'7' => {
                        let mut val: u32 = (escaped - b'0') as u32;
                        let mut n = 1;
                        while n < 3
                            && self.pos < self.src.len()
                            && (b'0'..=b'7').contains(&self.src[self.pos])
                        {
                            val = val * 8 + (self.src[self.pos] - b'0') as u32;
                            self.advance();
                            n += 1;
                        }
                        out.push(val as u8);
                    }
                    // `\uHHHH` / `\UHHHHHHHH` — Unicode code point, encoded UTF-8.
                    b'u' | b'U' => {
                        let width = if escaped == b'u' { 4 } else { 8 };
                        let mut val: u32 = 0;
                        let mut n = 0;
                        while n < width
                            && self.pos < self.src.len()
                            && let Some(h) = hex_digit(self.src[self.pos])
                        {
                            val = val * 16 + h as u32;
                            self.advance();
                            n += 1;
                        }
                        if n == width
                            && let Some(c) = char::from_u32(val)
                        {
                            let mut buf = [0u8; 4];
                            out.extend_from_slice(c.encode_utf8(&mut buf).as_bytes());
                        } else {
                            return Err(self.error_at(
                                escape_line,
                                escape_column,
                                &format!(
                                    "malformed unicode escape: expected \\{}{}",
                                    escaped as char,
                                    "H".repeat(width)
                                ),
                            ));
                        }
                    }
                    other => {
                        out.push(b'\\');
                        out.push(other);
                    }
                }
                continue;
            }
            if ch == quote {
                self.advance();
                return Ok(Token {
                    kind: TokenKind::String,
                    value: String::from_utf8_lossy(&out).to_string(),
                    line,
                    column,
                });
            }
            out.push(ch);
            self.advance();
        }
        Err(self.error_at(line, column, "unterminated string literal"))
    }

    fn skip_line_comment(&mut self) {
        while self.pos < self.src.len() && self.src[self.pos] != b'\n' {
            self.advance();
        }
    }

    fn skip_block_comment(&mut self) -> Result<(), LexError> {
        let line = self.line;
        let column = self.col;
        self.advance();
        self.advance();
        while self.pos + 1 < self.src.len() {
            if self.src[self.pos] == b'*' && self.src[self.pos + 1] == b'/' {
                self.advance();
                self.advance();
                return Ok(());
            }
            self.advance();
        }
        Err(self.error_at(line, column, "unterminated block comment"))
    }

    fn skip_angle_block(&mut self) {
        let mut depth = 1usize;
        while self.pos < self.src.len() && depth > 0 {
            match self.src[self.pos] {
                b'<' => depth += 1,
                b'>' => depth -= 1,
                _ => {}
            }
            self.advance();
        }
    }

    fn advance(&mut self) {
        if self.pos >= self.src.len() {
            return;
        }
        if self.src[self.pos] == b'\n' {
            self.line += 1;
            self.col = 1;
        } else {
            self.col += 1;
        }
        self.pos += 1;
    }

    fn peek(&self, offset: usize) -> u8 {
        self.src.get(self.pos + offset).copied().unwrap_or_default()
    }

    fn error_at(&self, line: usize, column: usize, message: &str) -> LexError {
        LexError {
            file: self.file.clone(),
            line,
            column,
            message: message.to_string(),
        }
    }
}

fn is_ws(ch: u8) -> bool {
    matches!(ch, b' ' | b'\t' | b'\r' | b'\n')
}

fn is_digit(ch: u8) -> bool {
    ch.is_ascii_digit()
}

/// Hex digit value for `\x` / `\u` / `\U` string-escape decoding.
fn hex_digit(ch: u8) -> Option<u8> {
    match ch {
        b'0'..=b'9' => Some(ch - b'0'),
        b'a'..=b'f' => Some(ch - b'a' + 10),
        b'A'..=b'F' => Some(ch - b'A' + 10),
        _ => None,
    }
}

fn is_ident_start(ch: u8) -> bool {
    ch == b'_' || ch.is_ascii_alphabetic()
}

fn is_ident_continue(ch: u8) -> bool {
    is_ident_start(ch) || is_digit(ch)
}