barbacane 0.6.2

Barbacane data plane — spec-driven API gateway
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use std::collections::HashMap;

/// The routing trie. Maps HTTP paths + methods to route matches.
#[derive(Debug, Default)]
pub struct Router {
    root: Node,
}

/// A single node in the prefix trie.
#[derive(Debug, Default)]
struct Node {
    /// Static children keyed by segment name.
    static_children: HashMap<String, Node>,
    /// Parameter child (at most one per node).
    param_child: Option<Box<ParamNode>>,
    /// Wildcard child — matches all remaining path segments joined by `/`.
    /// Only valid at a terminal position (no further trie nodes after it).
    wildcard_child: Option<Box<WildcardNode>>,
    /// Method-to-route mapping at this terminal node.
    methods: HashMap<String, RouteEntry>,
}

/// A parameter segment node.
#[derive(Debug)]
struct ParamNode {
    /// Parameter name (e.g. "id").
    name: String,
    /// The subtree below this parameter.
    node: Node,
}

/// A wildcard segment node (e.g. `{path+}`).
///
/// Matches all remaining path segments joined by `/` and captures the result
/// as a single parameter value.
#[derive(Debug)]
struct WildcardNode {
    /// Parameter name (e.g. "path" from `{path+}`).
    name: String,
    /// Terminal node — holds the method map.
    node: Node,
}

/// A matched route entry.
#[derive(Debug, Clone)]
pub struct RouteEntry {
    /// Index into the compiled operations list.
    pub operation_index: usize,
}

/// The result of a route lookup.
#[derive(Debug)]
pub enum RouteMatch {
    /// Matched a path and method.
    Found {
        entry: RouteEntry,
        params: Vec<(String, String)>,
    },
    /// Path matched but method is not allowed.
    MethodNotAllowed { allowed: Vec<String> },
    /// No path matched.
    NotFound,
}

/// A parsed path segment.
#[derive(Debug, Clone)]
enum Segment {
    Static(String),
    Param(String),
    /// Matches all remaining segments joined by `/`. Must be the last segment.
    Wildcard(String),
}

impl Router {
    /// Create a new empty router.
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert a route into the trie.
    ///
    /// Path should be a template like "/users/{id}/orders".
    /// Method should be uppercase (e.g. "GET", "POST").
    pub fn insert(&mut self, path: &str, method: &str, entry: RouteEntry) {
        let segments = parse_path_template(path);
        let node = self.traverse_or_create(&segments);
        node.methods.insert(method.to_uppercase(), entry);
    }

    /// Look up a request path and method.
    ///
    /// Path should be an actual request path (not a template).
    /// Method should be uppercase.
    pub fn lookup(&self, path: &str, method: &str) -> RouteMatch {
        let normalized = normalize_path(path);
        let segments: Vec<&str> = normalized.split('/').filter(|s| !s.is_empty()).collect();

        let mut params = Vec::new();
        match self.traverse_and_match(&self.root, &segments, &mut params) {
            Some(node) => {
                if let Some(entry) = node.methods.get(&method.to_uppercase()) {
                    RouteMatch::Found {
                        entry: entry.clone(),
                        params,
                    }
                } else if node.methods.is_empty() {
                    RouteMatch::NotFound
                } else {
                    let mut allowed: Vec<String> = node.methods.keys().cloned().collect();
                    allowed.sort();
                    RouteMatch::MethodNotAllowed { allowed }
                }
            }
            None => RouteMatch::NotFound,
        }
    }

    /// Traverse or create nodes for a path template.
    fn traverse_or_create(&mut self, segments: &[Segment]) -> &mut Node {
        let mut current = &mut self.root;

        for segment in segments {
            current = match segment {
                Segment::Static(name) => current.static_children.entry(name.clone()).or_default(),
                Segment::Param(name) => {
                    if current.param_child.is_none() {
                        current.param_child = Some(Box::new(ParamNode {
                            name: name.clone(),
                            node: Node::default(),
                        }));
                    }
                    &mut current.param_child.as_mut().expect("just set above").node
                }
                Segment::Wildcard(name) => {
                    if current.wildcard_child.is_none() {
                        current.wildcard_child = Some(Box::new(WildcardNode {
                            name: name.clone(),
                            node: Node::default(),
                        }));
                    }
                    &mut current
                        .wildcard_child
                        .as_mut()
                        .expect("just set above")
                        .node
                }
            };
        }

        current
    }

    /// Traverse the trie matching actual path segments, capturing parameters.
    /// Returns the terminal node if the path matches, None otherwise.
    fn traverse_and_match<'a>(
        &'a self,
        node: &'a Node,
        segments: &[&str],
        params: &mut Vec<(String, String)>,
    ) -> Option<&'a Node> {
        if segments.is_empty() {
            return Some(node);
        }

        let segment = segments[0];
        let remaining = &segments[1..];

        // Static children take precedence (most specific match).
        if let Some(child) = node.static_children.get(segment) {
            if let Some(result) = self.traverse_and_match(child, remaining, params) {
                return Some(result);
            }
        }

        // Try single-segment parameter child.
        if let Some(param_child) = &node.param_child {
            let param_len = params.len();
            params.push((param_child.name.clone(), segment.to_string()));

            if let Some(result) = self.traverse_and_match(&param_child.node, remaining, params) {
                return Some(result);
            }

            // Backtrack if this path didn't work.
            params.truncate(param_len);
        }

        // Try wildcard child — consumes all remaining segments (including the current one).
        if let Some(wildcard_child) = &node.wildcard_child {
            let joined = std::iter::once(segment)
                .chain(remaining.iter().copied())
                .collect::<Vec<_>>()
                .join("/");
            let param_len = params.len();
            params.push((wildcard_child.name.clone(), joined));

            if let Some(result) = self.traverse_and_match(&wildcard_child.node, &[], params) {
                return Some(result);
            }

            params.truncate(param_len);
        }

        None
    }
}

/// Parse a path template into segments.
fn parse_path_template(path: &str) -> Vec<Segment> {
    path.split('/')
        .filter(|s| !s.is_empty())
        .map(|s| {
            if s.starts_with('{') && s.ends_with('}') {
                let inner = &s[1..s.len() - 1];
                if let Some(base) = inner.strip_suffix('+') {
                    Segment::Wildcard(base.to_string())
                } else {
                    Segment::Param(inner.to_string())
                }
            } else {
                Segment::Static(s.to_string())
            }
        })
        .collect()
}

/// Normalize a request path: strip trailing slashes, collapse double slashes.
pub fn normalize_path(path: &str) -> String {
    let mut normalized = String::with_capacity(path.len());
    let mut prev_slash = false;

    for ch in path.chars() {
        if ch == '/' {
            if !prev_slash {
                normalized.push('/');
            }
            prev_slash = true;
        } else {
            normalized.push(ch);
            prev_slash = false;
        }
    }

    // Strip trailing slash (but keep root "/")
    if normalized.len() > 1 && normalized.ends_with('/') {
        normalized.pop();
    }

    if normalized.is_empty() {
        "/".to_string()
    } else {
        normalized
    }
}

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

    // === Normalization tests ===

    #[test]
    fn normalize_strips_trailing_slash() {
        assert_eq!(normalize_path("/users/"), "/users");
    }

    #[test]
    fn normalize_collapses_double_slashes() {
        assert_eq!(normalize_path("/users//123"), "/users/123");
    }

    #[test]
    fn normalize_preserves_root() {
        assert_eq!(normalize_path("/"), "/");
    }

    #[test]
    fn normalize_combined() {
        assert_eq!(normalize_path("/users//123//orders/"), "/users/123/orders");
    }

    // === Routing tests ===

    #[test]
    fn route_static_path() {
        let mut router = Router::new();
        router.insert("/health", "GET", RouteEntry { operation_index: 0 });

        match router.lookup("/health", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert!(params.is_empty());
            }
            _ => panic!("expected Found"),
        }
    }

    #[test]
    fn route_with_parameter() {
        let mut router = Router::new();
        router.insert("/users/{id}", "GET", RouteEntry { operation_index: 0 });

        match router.lookup("/users/123", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert_eq!(params, vec![("id".to_string(), "123".to_string())]);
            }
            _ => panic!("expected Found"),
        }
    }

    #[test]
    fn route_with_multiple_parameters() {
        let mut router = Router::new();
        router.insert(
            "/users/{userId}/orders/{orderId}",
            "GET",
            RouteEntry { operation_index: 0 },
        );

        match router.lookup("/users/42/orders/99", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert_eq!(
                    params,
                    vec![
                        ("userId".to_string(), "42".to_string()),
                        ("orderId".to_string(), "99".to_string()),
                    ]
                );
            }
            _ => panic!("expected Found"),
        }
    }

    #[test]
    fn route_not_found() {
        let mut router = Router::new();
        router.insert("/users", "GET", RouteEntry { operation_index: 0 });

        match router.lookup("/posts", "GET") {
            RouteMatch::NotFound => {}
            _ => panic!("expected NotFound"),
        }
    }

    #[test]
    fn route_method_not_allowed() {
        let mut router = Router::new();
        router.insert("/users", "GET", RouteEntry { operation_index: 0 });
        router.insert("/users", "POST", RouteEntry { operation_index: 1 });

        match router.lookup("/users", "DELETE") {
            RouteMatch::MethodNotAllowed { allowed } => {
                assert!(allowed.contains(&"GET".to_string()));
                assert!(allowed.contains(&"POST".to_string()));
            }
            _ => panic!("expected MethodNotAllowed"),
        }
    }

    #[test]
    fn static_takes_precedence_over_param() {
        let mut router = Router::new();
        router.insert("/users/me", "GET", RouteEntry { operation_index: 0 });
        router.insert("/users/{id}", "GET", RouteEntry { operation_index: 1 });

        // "/users/me" should match the static route
        match router.lookup("/users/me", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert!(params.is_empty());
            }
            _ => panic!("expected Found for static"),
        }

        // "/users/123" should match the param route
        match router.lookup("/users/123", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 1);
                assert_eq!(params, vec![("id".to_string(), "123".to_string())]);
            }
            _ => panic!("expected Found for param"),
        }
    }

    #[test]
    fn route_root_path() {
        let mut router = Router::new();
        router.insert("/", "GET", RouteEntry { operation_index: 0 });

        match router.lookup("/", "GET") {
            RouteMatch::Found { entry, .. } => {
                assert_eq!(entry.operation_index, 0);
            }
            _ => panic!("expected Found for root"),
        }
    }

    #[test]
    fn route_normalizes_request_path() {
        let mut router = Router::new();
        router.insert("/users/{id}", "GET", RouteEntry { operation_index: 0 });

        // Trailing slash should still match
        match router.lookup("/users/123/", "GET") {
            RouteMatch::Found { params, .. } => {
                assert_eq!(params, vec![("id".to_string(), "123".to_string())]);
            }
            _ => panic!("expected Found"),
        }

        // Double slashes should still match
        match router.lookup("/users//456", "GET") {
            RouteMatch::Found { params, .. } => {
                assert_eq!(params, vec![("id".to_string(), "456".to_string())]);
            }
            _ => panic!("expected Found"),
        }
    }

    #[test]
    fn multiple_methods_same_path() {
        let mut router = Router::new();
        router.insert("/users", "GET", RouteEntry { operation_index: 0 });
        router.insert("/users", "POST", RouteEntry { operation_index: 1 });
        router.insert("/users", "DELETE", RouteEntry { operation_index: 2 });

        match router.lookup("/users", "GET") {
            RouteMatch::Found { entry, .. } => assert_eq!(entry.operation_index, 0),
            _ => panic!("expected Found for GET"),
        }

        match router.lookup("/users", "POST") {
            RouteMatch::Found { entry, .. } => assert_eq!(entry.operation_index, 1),
            _ => panic!("expected Found for POST"),
        }

        match router.lookup("/users", "DELETE") {
            RouteMatch::Found { entry, .. } => assert_eq!(entry.operation_index, 2),
            _ => panic!("expected Found for DELETE"),
        }
    }

    // === Wildcard parameter tests ===

    #[test]
    fn wildcard_matches_single_segment() {
        let mut router = Router::new();
        router.insert("/files/{name+}", "GET", RouteEntry { operation_index: 0 });

        match router.lookup("/files/readme.txt", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert_eq!(params, vec![("name".to_string(), "readme.txt".to_string())]);
            }
            _ => panic!("expected Found"),
        }
    }

    #[test]
    fn wildcard_matches_multiple_segments() {
        let mut router = Router::new();
        router.insert("/files/{path+}", "GET", RouteEntry { operation_index: 0 });

        match router.lookup("/files/a/b/c/file.txt", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert_eq!(
                    params,
                    vec![("path".to_string(), "a/b/c/file.txt".to_string())]
                );
            }
            _ => panic!("expected Found"),
        }
    }

    #[test]
    fn wildcard_with_prefix_param() {
        let mut router = Router::new();
        router.insert(
            "/files/{bucket}/{key+}",
            "GET",
            RouteEntry { operation_index: 0 },
        );

        // Multi-segment wildcard value
        match router.lookup("/files/my-bucket/folder/sub/file.txt", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert_eq!(
                    params,
                    vec![
                        ("bucket".to_string(), "my-bucket".to_string()),
                        ("key".to_string(), "folder/sub/file.txt".to_string()),
                    ]
                );
            }
            _ => panic!("expected Found for multi-segment"),
        }

        // Single-segment wildcard value — this is the S3 proxy case
        match router.lookup("/files/my-bucket/file.txt", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert_eq!(
                    params,
                    vec![
                        ("bucket".to_string(), "my-bucket".to_string()),
                        ("key".to_string(), "file.txt".to_string()),
                    ]
                );
            }
            _ => panic!("expected Found for single-segment"),
        }
    }

    #[test]
    fn static_takes_precedence_over_wildcard() {
        let mut router = Router::new();
        router.insert("/files/special", "GET", RouteEntry { operation_index: 0 });
        router.insert("/files/{path+}", "GET", RouteEntry { operation_index: 1 });

        // Static wins for exact match
        match router.lookup("/files/special", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert!(params.is_empty());
            }
            _ => panic!("expected Found for static"),
        }

        // Wildcard wins for multi-segment
        match router.lookup("/files/other/file.txt", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 1);
                assert_eq!(
                    params,
                    vec![("path".to_string(), "other/file.txt".to_string())]
                );
            }
            _ => panic!("expected Found for wildcard"),
        }
    }

    #[test]
    fn param_takes_precedence_over_wildcard() {
        let mut router = Router::new();
        router.insert("/files/{name}", "GET", RouteEntry { operation_index: 0 });
        router.insert("/files/{path+}", "GET", RouteEntry { operation_index: 1 });

        // Single segment: param wins (more specific)
        match router.lookup("/files/readme.txt", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 0);
                assert_eq!(params, vec![("name".to_string(), "readme.txt".to_string())]);
            }
            _ => panic!("expected Found for param"),
        }

        // Multi-segment: only wildcard can match
        match router.lookup("/files/a/b.txt", "GET") {
            RouteMatch::Found { entry, params } => {
                assert_eq!(entry.operation_index, 1);
                assert_eq!(params, vec![("path".to_string(), "a/b.txt".to_string())]);
            }
            _ => panic!("expected Found for wildcard"),
        }
    }
}