Skip to main content

dsl_knowledge/tables/
mod.rs

1//! Knowledge-base tables, lazily initialised on first use.
2//!
3//! Each sub-module owns one table:
4//!   - [`keywords`] -- SQL keywords (SELECT, FROM, ...).
5//!   - [`types`]    -- data types (UUID, TIMESTAMPTZ, ...).
6//!   - [`functions`] -- built-in functions (count, now, gen_random_uuid, ...).
7//!
8//! Each table is keyed by canonical case: keywords + types use uppercase,
9//! functions use lowercase. The [`lookup`] helper tries all three.
10
11pub mod functions;
12pub mod keywords;
13pub mod types;
14
15use crate::entry::Entry;
16use once_cell::sync::Lazy;
17use std::collections::HashMap;
18
19pub fn keywords() -> &'static HashMap<&'static str, Entry> {
20  static MAP: Lazy<HashMap<&'static str, Entry>> = Lazy::new(keywords::build);
21  &MAP
22}
23
24pub fn types() -> &'static HashMap<&'static str, Entry> {
25  static MAP: Lazy<HashMap<&'static str, Entry>> = Lazy::new(types::build);
26  &MAP
27}
28
29pub fn functions() -> &'static HashMap<&'static str, Entry> {
30  static MAP: Lazy<HashMap<&'static str, Entry>> = Lazy::new(functions::build);
31  &MAP
32}
33
34/// Try keyword (uppercase), then type (uppercase), then function (lowercase).
35/// Returns the first match.
36pub fn lookup(token: &str) -> Option<&'static Entry> {
37  let upper = token.to_ascii_uppercase();
38  if let Some(e) = keywords().get(upper.as_str()) {
39    return Some(e);
40  }
41  if let Some(e) = types().get(upper.as_str()) {
42    return Some(e);
43  }
44  let lower = token.to_ascii_lowercase();
45  functions().get(lower.as_str())
46}