reddb-io-server 1.0.8

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/// Command router: resolves domain/resource/verb from positional tokens.
///
/// Builds a RouteTree from registry data and resolves a token stream into
/// one of several outcomes: fully resolved command, global command,
/// partial match, or unknown with suggestions.
use std::collections::{HashMap, HashSet};

use super::error::suggest;
use super::token::Token;
use super::types::CommandPath;

/// Global commands that bypass domain/resource/verb routing.
const GLOBAL_COMMANDS: &[&str] = &["help", "version", "commands"];

/// Result of route resolution.
#[derive(Debug)]
pub enum RouteResolution {
    /// Fully resolved: domain + resource + verb.
    Resolved {
        path: CommandPath,
        remaining_tokens: Vec<Token>,
    },
    /// Global command: help, version, commands.
    GlobalCommand {
        name: String,
        remaining_tokens: Vec<Token>,
    },
    /// Partial: only domain found (no resource given).
    PartialDomain {
        domain: String,
        remaining_tokens: Vec<Token>,
    },
    /// Partial: domain + resource found but no verb.
    PartialResource {
        domain: String,
        resource: String,
        remaining_tokens: Vec<Token>,
    },
    /// Nothing matched.
    Unknown {
        tokens: Vec<String>,
        suggestions: Vec<String>,
    },
}

// ------------------------------------------------------------------
// Internal tree structures
// ------------------------------------------------------------------

struct ResourceEntry {
    verbs: HashSet<String>,
    verb_aliases: HashMap<String, String>,
}

struct DomainEntry {
    resources: HashMap<String, ResourceEntry>,
    aliases: HashMap<String, String>,
}

/// Domain/resource/verb tree built from Command trait implementations.
pub struct RouteTree {
    domains: HashMap<String, DomainEntry>,
    aliases: HashMap<String, String>,
}

impl RouteTree {
    /// Build from command registry data.
    ///
    /// * `domains`   - `(domain_name, domain_aliases)`
    /// * `resources` - `(domain, resource_name, resource_aliases)`
    /// * `verbs`     - `(domain, resource, verb_name, verb_aliases)`
    pub fn build(
        domains: &[(String, Vec<String>)],
        resources: &[(String, String, Vec<String>)],
        verbs: &[(String, String, String, Vec<String>)],
    ) -> Self {
        let mut tree_domains: HashMap<String, DomainEntry> = HashMap::new();
        let mut tree_aliases: HashMap<String, String> = HashMap::new();

        // Register domains and their aliases.
        for (name, aliases) in domains {
            for alias in aliases {
                tree_aliases.insert(alias.clone(), name.clone());
            }
            tree_domains
                .entry(name.clone())
                .or_insert_with(|| DomainEntry {
                    resources: HashMap::new(),
                    aliases: HashMap::new(),
                });
        }

        // Register resources and their aliases within their domain.
        for (domain, resource, aliases) in resources {
            let domain_entry = tree_domains
                .entry(domain.clone())
                .or_insert_with(|| DomainEntry {
                    resources: HashMap::new(),
                    aliases: HashMap::new(),
                });
            for alias in aliases {
                domain_entry.aliases.insert(alias.clone(), resource.clone());
            }
            domain_entry
                .resources
                .entry(resource.clone())
                .or_insert_with(|| ResourceEntry {
                    verbs: HashSet::new(),
                    verb_aliases: HashMap::new(),
                });
        }

        // Register verbs and their aliases within their domain/resource.
        for (domain, resource, verb, aliases) in verbs {
            let domain_entry = tree_domains
                .entry(domain.clone())
                .or_insert_with(|| DomainEntry {
                    resources: HashMap::new(),
                    aliases: HashMap::new(),
                });
            let resource_entry = domain_entry
                .resources
                .entry(resource.clone())
                .or_insert_with(|| ResourceEntry {
                    verbs: HashSet::new(),
                    verb_aliases: HashMap::new(),
                });
            resource_entry.verbs.insert(verb.clone());
            for alias in aliases {
                resource_entry
                    .verb_aliases
                    .insert(alias.clone(), verb.clone());
            }
        }

        Self {
            domains: tree_domains,
            aliases: tree_aliases,
        }
    }

    /// Resolve tokens into a route.
    pub fn resolve(&self, tokens: &[Token]) -> RouteResolution {
        // 1. Collect leading positionals (stop at first flag token).
        let mut positionals: Vec<&str> = Vec::new();
        let mut first_non_pos_idx: Option<usize> = None;

        for (i, token) in tokens.iter().enumerate() {
            match token {
                Token::Positional(val) => positionals.push(val.as_str()),
                _ => {
                    first_non_pos_idx = Some(i);
                    break;
                }
            }
        }

        // Remaining = everything after positionals consumed for routing.
        let build_remaining = |consumed: usize| -> Vec<Token> {
            let start = if consumed < positionals.len() {
                // Unconsumed positionals + all remaining tokens.
                consumed
            } else {
                positionals.len()
            };
            let mut remaining = Vec::new();
            // Re-emit unconsumed positionals.
            for &p in &positionals[start..] {
                remaining.push(Token::Positional(p.to_string()));
            }
            // Append everything from the first non-positional onward.
            let tail_start = first_non_pos_idx.unwrap_or(tokens.len());
            remaining.extend_from_slice(&tokens[tail_start..]);
            remaining
        };

        // 2. If empty: nothing to route.
        if positionals.is_empty() {
            return RouteResolution::Unknown {
                tokens: vec![],
                suggestions: self.domain_names(),
            };
        }

        let first = positionals[0];

        // 3. Check global commands.
        if GLOBAL_COMMANDS.contains(&first) {
            return RouteResolution::GlobalCommand {
                name: first.to_string(),
                remaining_tokens: build_remaining(1),
            };
        }

        // 4. Resolve domain (canonical or alias).
        let domain = if self.domains.contains_key(first) {
            first.to_string()
        } else if let Some(canonical) = self.aliases.get(first) {
            canonical.clone()
        } else {
            // Unknown domain -- generate suggestions.
            let candidates = self.domain_names();
            let candidate_refs: Vec<&str> = candidates.iter().map(|s| s.as_str()).collect();
            let suggestions = suggest(first, &candidate_refs, 3);
            return RouteResolution::Unknown {
                tokens: positionals.iter().map(|s| s.to_string()).collect(),
                suggestions,
            };
        };

        let domain_entry = self.domains.get(&domain).expect("domain just resolved");

        // 5. Only domain given.
        if positionals.len() == 1 {
            return RouteResolution::PartialDomain {
                domain,
                remaining_tokens: build_remaining(1),
            };
        }

        let second = positionals[1];

        // 6. Resolve resource (canonical or alias within domain).
        let resource = if domain_entry.resources.contains_key(second) {
            second.to_string()
        } else if let Some(canonical) = domain_entry.aliases.get(second) {
            canonical.clone()
        } else {
            // Before giving up, check compat translation (legacy: domain verb resource).
            if positionals.len() >= 3 {
                if let Some(resolution) = self.try_compat_swap(
                    &domain,
                    domain_entry,
                    positionals[1],
                    positionals[2],
                    &build_remaining(3),
                ) {
                    return resolution;
                }
            }
            // Resource not found.
            return RouteResolution::PartialDomain {
                domain,
                remaining_tokens: build_remaining(1),
            };
        };

        // 7. Only domain + resource given.
        if positionals.len() == 2 {
            return RouteResolution::PartialResource {
                domain,
                resource,
                remaining_tokens: build_remaining(2),
            };
        }

        let third = positionals[2];
        let resource_entry = domain_entry
            .resources
            .get(&resource)
            .expect("resource just resolved");

        // 8. Resolve verb (canonical or alias within resource).
        if resource_entry.verbs.contains(third) {
            return RouteResolution::Resolved {
                path: CommandPath {
                    domain,
                    resource: Some(resource),
                    verb: Some(third.to_string()),
                },
                remaining_tokens: build_remaining(3),
            };
        }

        if let Some(canonical_verb) = resource_entry.verb_aliases.get(third) {
            return RouteResolution::Resolved {
                path: CommandPath {
                    domain,
                    resource: Some(resource),
                    verb: Some(canonical_verb.clone()),
                },
                remaining_tokens: build_remaining(3),
            };
        }

        // 9. Compat translation: try swapping positional[1] and positional[2].
        if let Some(resolution) = self.try_compat_swap(
            &domain,
            domain_entry,
            positionals[1],
            positionals[2],
            &build_remaining(3),
        ) {
            return resolution;
        }

        // Verb not found but domain+resource valid.
        RouteResolution::PartialResource {
            domain,
            resource,
            remaining_tokens: build_remaining(2),
        }
    }

    /// Get all canonical domain names.
    pub fn domains(&self) -> Vec<&str> {
        let mut names: Vec<&str> = self.domains.keys().map(|s| s.as_str()).collect();
        names.sort();
        names
    }

    /// Get canonical resource names for a domain.
    pub fn resources(&self, domain: &str) -> Vec<&str> {
        self.domains
            .get(domain)
            .map(|entry| {
                let mut names: Vec<&str> = entry.resources.keys().map(|s| s.as_str()).collect();
                names.sort();
                names
            })
            .unwrap_or_default()
    }

    /// Get canonical verb names for a domain/resource pair.
    pub fn verbs(&self, domain: &str, resource: &str) -> Vec<&str> {
        self.domains
            .get(domain)
            .and_then(|d| d.resources.get(resource))
            .map(|r| {
                let mut names: Vec<&str> = r.verbs.iter().map(|s| s.as_str()).collect();
                names.sort();
                names
            })
            .unwrap_or_default()
    }

    // ------------------------------------------------------------------
    // Private helpers
    // ------------------------------------------------------------------

    fn domain_names(&self) -> Vec<String> {
        let mut names: Vec<String> = self.domains.keys().cloned().collect();
        names.sort();
        names
    }

    /// Try the legacy order swap: `domain verb resource` -> `domain resource verb`.
    /// Returns `Some(Resolved)` if the swap produces a valid route.
    fn try_compat_swap(
        &self,
        domain: &str,
        domain_entry: &DomainEntry,
        first_token: &str,
        second_token: &str,
        remaining: &[Token],
    ) -> Option<RouteResolution> {
        // Interpret first_token as verb, second_token as resource.
        let swapped_resource = if domain_entry.resources.contains_key(second_token) {
            second_token.to_string()
        } else {
            domain_entry.aliases.get(second_token)?.clone()
        };

        let resource_entry = domain_entry.resources.get(&swapped_resource)?;

        let swapped_verb = if resource_entry.verbs.contains(first_token) {
            first_token.to_string()
        } else {
            resource_entry.verb_aliases.get(first_token)?.clone()
        };

        Some(RouteResolution::Resolved {
            path: CommandPath {
                domain: domain.to_string(),
                resource: Some(swapped_resource),
                verb: Some(swapped_verb),
            },
            remaining_tokens: remaining.to_vec(),
        })
    }
}

// ==================================================================
// Tests
// ==================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::token::Token;

    /// Build a small tree for testing.
    fn test_tree() -> RouteTree {
        let domains = vec![
            ("data".into(), vec![]),
            ("server".into(), vec!["s".into(), "srv".into()]),
            ("index".into(), vec![]),
            ("graph".into(), vec!["g".into()]),
        ];
        let resources = vec![
            ("data".into(), "collection".into(), vec!["col".into()]),
            ("server".into(), "grpc".into(), vec!["rpc".into()]),
            ("server".into(), "http".into(), vec![]),
            ("index".into(), "vector".into(), vec![]),
            ("graph".into(), "node".into(), vec!["n".into()]),
        ];
        let verbs = vec![
            ("data".into(), "collection".into(), "list".into(), vec![]),
            ("data".into(), "collection".into(), "create".into(), vec![]),
            (
                "server".into(),
                "grpc".into(),
                "start".into(),
                vec!["s".into()],
            ),
            ("server".into(), "http".into(), "start".into(), vec![]),
            ("index".into(), "vector".into(), "build".into(), vec![]),
            (
                "graph".into(),
                "node".into(),
                "query".into(),
                vec!["q".into()],
            ),
            ("graph".into(), "node".into(), "traverse".into(), vec![]),
        ];
        RouteTree::build(&domains, &resources, &verbs)
    }

    fn pos(s: &str) -> Token {
        Token::Positional(s.to_string())
    }

    fn long_flag(name: &str) -> Token {
        Token::LongFlag {
            name: name.to_string(),
            value: None,
        }
    }

    // ----------------------------------------------------------------
    // 1. Full command resolution
    // ----------------------------------------------------------------
    #[test]
    fn test_resolve_full_command() {
        let tree = test_tree();
        let tokens = vec![pos("data"), pos("collection"), pos("list")];
        match tree.resolve(&tokens) {
            RouteResolution::Resolved {
                path,
                remaining_tokens,
            } => {
                assert_eq!(path.domain, "data");
                assert_eq!(path.resource.as_deref(), Some("collection"));
                assert_eq!(path.verb.as_deref(), Some("list"));
                assert!(remaining_tokens.is_empty());
            }
            other => panic!("expected Resolved, got {:?}", other),
        }
    }

    // ----------------------------------------------------------------
    // 2. Alias resolution
    // ----------------------------------------------------------------
    #[test]
    fn test_resolve_with_aliases() {
        let tree = test_tree();
        let tokens = vec![pos("s"), pos("grpc"), pos("s")];
        match tree.resolve(&tokens) {
            RouteResolution::Resolved {
                path,
                remaining_tokens,
            } => {
                assert_eq!(path.domain, "server");
                assert_eq!(path.resource.as_deref(), Some("grpc"));
                assert_eq!(path.verb.as_deref(), Some("start"));
                assert!(remaining_tokens.is_empty());
            }
            other => panic!("expected Resolved, got {:?}", other),
        }
    }

    // ----------------------------------------------------------------
    // 3. Global help
    // ----------------------------------------------------------------
    #[test]
    fn test_resolve_global_help() {
        let tree = test_tree();
        let tokens = vec![pos("help")];
        match tree.resolve(&tokens) {
            RouteResolution::GlobalCommand {
                name,
                remaining_tokens,
            } => {
                assert_eq!(name, "help");
                assert!(remaining_tokens.is_empty());
            }
            other => panic!("expected GlobalCommand, got {:?}", other),
        }
    }

    // ----------------------------------------------------------------
    // 4. Global version
    // ----------------------------------------------------------------
    #[test]
    fn test_resolve_global_version() {
        let tree = test_tree();
        let tokens = vec![pos("version")];
        match tree.resolve(&tokens) {
            RouteResolution::GlobalCommand {
                name,
                remaining_tokens,
            } => {
                assert_eq!(name, "version");
                assert!(remaining_tokens.is_empty());
            }
            other => panic!("expected GlobalCommand, got {:?}", other),
        }
    }

    // ----------------------------------------------------------------
    // 5. Partial domain
    // ----------------------------------------------------------------
    #[test]
    fn test_resolve_partial_domain() {
        let tree = test_tree();
        let tokens = vec![pos("data")];
        match tree.resolve(&tokens) {
            RouteResolution::PartialDomain {
                domain,
                remaining_tokens,
            } => {
                assert_eq!(domain, "data");
                assert!(remaining_tokens.is_empty());
            }
            other => panic!("expected PartialDomain, got {:?}", other),
        }
    }

    // ----------------------------------------------------------------
    // 6. Partial resource
    // ----------------------------------------------------------------
    #[test]
    fn test_resolve_partial_resource() {
        let tree = test_tree();
        let tokens = vec![pos("data"), pos("collection")];
        match tree.resolve(&tokens) {
            RouteResolution::PartialResource {
                domain,
                resource,
                remaining_tokens,
            } => {
                assert_eq!(domain, "data");
                assert_eq!(resource, "collection");
                assert!(remaining_tokens.is_empty());
            }
            other => panic!("expected PartialResource, got {:?}", other),
        }
    }

    // ----------------------------------------------------------------
    // 7. Unknown domain
    // ----------------------------------------------------------------
    #[test]
    fn test_resolve_unknown_domain() {
        let tree = test_tree();
        let tokens = vec![pos("daat"), pos("collection"), pos("list")];
        match tree.resolve(&tokens) {
            RouteResolution::Unknown {
                tokens: toks,
                suggestions,
            } => {
                assert_eq!(toks, vec!["daat", "collection", "list"]);
                assert!(suggestions.contains(&"data".to_string()));
            }
            other => panic!("expected Unknown, got {:?}", other),
        }
    }

    // ----------------------------------------------------------------
    // 8. Compat translation (legacy order: domain verb resource)
    // ----------------------------------------------------------------
    #[test]
    fn test_resolve_compat_translation() {
        let tree = test_tree();
        // Legacy: red data list collection -> canonical: red data collection list
        let tokens = vec![pos("data"), pos("list"), pos("collection")];
        match tree.resolve(&tokens) {
            RouteResolution::Resolved { path, .. } => {
                assert_eq!(path.domain, "data");
                assert_eq!(path.resource.as_deref(), Some("collection"));
                assert_eq!(path.verb.as_deref(), Some("list"));
            }
            other => panic!("expected Resolved via compat swap, got {:?}", other),
        }
    }

    // ----------------------------------------------------------------
    // 9. Remaining tokens pass-through
    // ----------------------------------------------------------------
    #[test]
    fn test_resolve_with_remaining_tokens() {
        let tree = test_tree();
        let tokens = vec![
            pos("server"),
            pos("grpc"),
            pos("start"),
            pos("--path"),
            long_flag("bind"),
            pos("0.0.0.0:6380"),
        ];
        match tree.resolve(&tokens) {
            RouteResolution::Resolved {
                path,
                remaining_tokens,
            } => {
                assert_eq!(path.domain, "server");
                assert_eq!(path.resource.as_deref(), Some("grpc"));
                assert_eq!(path.verb.as_deref(), Some("start"));
                // remaining: "--path" positional, --bind flag, "0.0.0.0:6380" positional
                assert_eq!(remaining_tokens.len(), 3);
                assert_eq!(remaining_tokens[0], pos("--path"));
            }
            other => panic!("expected Resolved, got {:?}", other),
        }
    }

    // ----------------------------------------------------------------
    // 10. domains() and resources() listing
    // ----------------------------------------------------------------
    #[test]
    fn test_domains_and_resources_listing() {
        let tree = test_tree();
        let domains = tree.domains();
        assert!(domains.contains(&"data"));
        assert!(domains.contains(&"server"));
        assert!(domains.contains(&"index"));
        assert!(domains.contains(&"graph"));

        let resources = tree.resources("server");
        assert!(resources.contains(&"grpc"));
        assert!(resources.contains(&"http"));

        let verbs = tree.verbs("data", "collection");
        assert!(verbs.contains(&"list"));
        assert!(verbs.contains(&"create"));

        // Non-existent domain returns empty.
        assert!(tree.resources("fake").is_empty());
        assert!(tree.verbs("fake", "foo").is_empty());
    }
}