Skip to main content

rust_web_server/proxy_config/
parser.rs

1//! Hand-rolled TOML parser that extracts `[[upstream]]`, `[[route]]`,
2//! `[[tcp_proxy]]`, `[[udp_proxy]]`, `[[ws_proxy]]` sections from
3//! `rws.config.toml` into a flat `SectionMap`.
4//!
5//! The output is a `HashMap<String, Vec<(String, String)>>` where each key is
6//! a section path (e.g. `"route[0].action.proxy"`) and each value is the list
7//! of key-value pairs found in that section.
8
9use std::collections::HashMap;
10use crate::entry_point::config_file::toml_str::{unquote, Quotes};
11
12/// Section path → list of (key, value) pairs.
13pub type SectionMap = HashMap<String, Vec<(String, String)>>;
14
15/// Parse a TOML string into a `SectionMap`.
16///
17/// Supports the subset of TOML used in `rws.config.toml`:
18/// - Array-of-tables `[[X]]`
19/// - Standard tables `[X]`
20/// - Key-value pairs (string, bool, integer, inline tables, arrays)
21pub fn parse(toml: &str) -> SectionMap {
22    let mut map: SectionMap = HashMap::new();
23
24    // Outer array table tracking
25    let mut outer_name: Option<String> = None; // e.g. "route"
26    let mut outer_idx: usize = 0;
27    let mut outer_counters: HashMap<String, usize> = HashMap::new(); // "route" → 2
28    let mut inner_counters: HashMap<String, usize> = HashMap::new(); // "route[0].middleware.rewrite.request" → 1
29
30    // Current section path (where key-value pairs are written)
31    let mut current_section: String = String::new();
32
33    for line in logical_lines(toml) {
34
35        if line.is_empty() {
36            continue;
37        }
38
39        if line.starts_with("[[") && line.ends_with("]]") {
40            // Array-of-tables header
41            let name = line[2..line.len() - 2].trim().to_string();
42
43            // Is this a nested array inside the current outer table?
44            if let Some(ref on) = outer_name.clone() {
45                let prefix = format!("{}.", on);
46                if name.starts_with(&prefix) {
47                    // Nested array: "route[N].middleware.rewrite.request"
48                    let base = format!("{}{}", outer_section_base(on, outer_idx), &name[on.len()..]);
49                    let cnt = inner_counters.entry(base.clone()).or_insert(0);
50                    let section_path = format!("{}[{}]", base, cnt);
51                    *cnt += 1;
52                    current_section = section_path.clone();
53                    map.entry(current_section.clone()).or_default();
54                    continue;
55                }
56            }
57
58            // Top-level array-of-tables
59            let idx = outer_counters.entry(name.clone()).or_insert(0);
60            outer_idx = *idx;
61            *idx += 1;
62            outer_name = Some(name.clone());
63            inner_counters.clear();
64            current_section = outer_section_base(&name, outer_idx);
65            map.entry(current_section.clone()).or_default();
66        } else if line.starts_with('[') && line.ends_with(']') {
67            // Standard table header
68            let name = line[1..line.len() - 1].trim().to_string();
69
70            if let Some(ref on) = outer_name.clone() {
71                let prefix = format!("{}.", on);
72                if name.starts_with(&prefix) {
73                    // Sub-table of current outer: e.g. "route.match" inside "route"
74                    current_section = format!(
75                        "{}{}",
76                        outer_section_base(on, outer_idx),
77                        &name[on.len()..]
78                    );
79                    map.entry(current_section.clone()).or_default();
80                    continue;
81                }
82            }
83
84            // Standalone table (reset outer tracking)
85            outer_name = None;
86            inner_counters.clear();
87            current_section = name.clone();
88            map.entry(current_section.clone()).or_default();
89        } else if let Some(eq) = line.find('=') {
90            // Key = value pair
91            let key = line[..eq].trim().to_string();
92            let raw_val = line[eq + 1..].trim().to_string();
93
94            if raw_val.starts_with('{') {
95                // Inline table: `rate_limit = { max_requests = 5 }` is the
96                // sub-table `[<section>.rate_limit]`, exactly as in TOML. (It
97                // used to become a `rate_limit.max_requests` key in the parent
98                // section, which nothing reads — inline auth, rate limits and
99                // IP filters were silently ignored.)
100                let inner = &raw_val[1..raw_val.rfind('}').unwrap_or(raw_val.len())];
101                let sub_section = if current_section.is_empty() { key.clone() } else { format!("{}.{}", current_section, key) };
102                let entries = map.entry(sub_section).or_default();
103                for part in split_inline_table(inner) {
104                    if let Some(ieq) = part.find('=') {
105                        let ik = part[..ieq].trim().to_string();
106                        let iv = parse_value(part[ieq + 1..].trim());
107                        entries.push((ik, iv));
108                    }
109                }
110            } else {
111                let value = parse_value(&raw_val);
112                map.entry(current_section.clone())
113                    .or_default()
114                    .push((key, value));
115            }
116        }
117    }
118
119    map
120}
121
122/// Build the base path for an outer array entry, e.g. `"route[0]"`.
123fn outer_section_base(name: &str, idx: usize) -> String {
124    format!("{}[{}]", name, idx)
125}
126
127/// Strip an inline TOML comment (first `#` not inside a quoted string).
128/// The lines of `toml` with comments removed and trimmed, a multi-line
129/// array (`key = [` … `]`) joined into one line — the rest of the parser
130/// reads one `key = value` per line. A bracket inside a string doesn't
131/// count; an array left open at the end is returned as it is.
132pub(crate) fn logical_lines(toml: &str) -> Vec<String> {
133    let mut out = Vec::new();
134    let mut pending: Option<(String, i64)> = None;
135    for raw in toml.lines() {
136        let line = strip_comment(raw).trim();
137        if let Some((joined, depth)) = pending.as_mut() {
138            if !line.is_empty() {
139                joined.push(' ');
140                joined.push_str(line);
141                *depth += bracket_depth(line);
142            }
143            if *depth <= 0 {
144                out.push(pending.take().expect("pending").0);
145            }
146            continue;
147        }
148        let opens_array = line.split_once('=').is_some_and(|(key, value)| !key.trim().starts_with('[') && value.trim_start().starts_with('['));
149        let depth = if opens_array { bracket_depth(line) } else { 0 };
150        if depth > 0 {
151            pending = Some((line.to_string(), depth));
152        } else {
153            out.push(line.to_string());
154        }
155    }
156    if let Some((joined, _)) = pending {
157        out.push(joined);
158    }
159    out
160}
161
162/// `[` minus `]` outside quoted strings.
163fn bracket_depth(line: &str) -> i64 {
164    let mut depth = 0;
165    let mut quotes = Quotes::default();
166    for c in line.chars() {
167        if quotes.step(c) {
168            continue;
169        }
170        match c {
171            '[' => depth += 1,
172            ']' => depth -= 1,
173            _ => {}
174        }
175    }
176    depth
177}
178
179pub(crate) fn strip_comment(line: &str) -> &str {
180    let mut quotes = Quotes::default();
181    for (i, c) in line.char_indices() {
182        if !quotes.step(c) && c == '#' {
183            return &line[..i];
184        }
185    }
186    line
187}
188
189/// Parse a single TOML value into its string representation.
190///
191/// - Quoted strings → bare string (quotes stripped, a basic string's escapes
192///   decoded: [`unquote`])
193/// - Arrays `["a","b"]` → `"a,b"` (joined with comma)
194/// - Booleans / numbers → as-is
195pub(crate) fn parse_value(raw: &str) -> String {
196    let raw = raw.trim();
197    if raw.starts_with('[') && raw.ends_with(']') {
198        // Array of scalars
199        let inner = &raw[1..raw.len() - 1];
200        let items: Vec<String> = split_array_items(inner)
201            .into_iter()
202            .map(|s| unquote(s.trim()))
203            .collect();
204        return items.join(",");
205    }
206    unquote(raw)
207}
208
209/// Split a TOML array body (without brackets) into individual items.
210/// Handles quoted strings that may contain commas.
211fn split_array_items(inner: &str) -> Vec<&str> {
212    let mut items = Vec::new();
213    let mut depth = 0i32;
214    let mut quotes = Quotes::default();
215    let mut start = 0;
216    for (i, c) in inner.char_indices() {
217        if quotes.step(c) {
218            continue;
219        }
220        match c {
221            '[' | '{' => depth += 1,
222            ']' | '}' => depth -= 1,
223            ',' if depth == 0 => {
224                items.push(inner[start..i].trim());
225                start = i + 1;
226            }
227            _ => {}
228        }
229    }
230    let last = inner[start..].trim();
231    if !last.is_empty() {
232        items.push(last);
233    }
234    items
235}
236
237/// Split inline table body `"key = val, key2 = val2"` into parts at top-level commas.
238fn split_inline_table(inner: &str) -> Vec<String> {
239    let mut parts = Vec::new();
240    let mut depth = 0i32;
241    let mut quotes = Quotes::default();
242    let mut start = 0;
243    for (i, c) in inner.char_indices() {
244        if quotes.step(c) {
245            continue;
246        }
247        match c {
248            '[' | '{' => depth += 1,
249            ']' | '}' => depth -= 1,
250            ',' if depth == 0 => {
251                let part = inner[start..i].trim();
252                if !part.is_empty() {
253                    parts.push(part.to_string());
254                }
255                start = i + 1;
256            }
257            _ => {}
258        }
259    }
260    let part = inner[start..].trim();
261    if !part.is_empty() {
262        parts.push(part.to_string());
263    }
264    parts
265}
266
267// ── Helper accessors ───────────────────────────────────────────────────────────
268
269/// Get the first value for `key` in `section`, or `None`.
270pub(crate) fn get(map: &SectionMap, section: &str, key: &str) -> Option<String> {
271    map.get(section)?.iter().find(|(k, _)| k == key).map(|(_, v)| v.clone())
272}
273
274/// Get the first value for `key` in `section`, or empty string.
275pub(crate) fn get_str(map: &SectionMap, section: &str, key: &str) -> String {
276    get(map, section, key).unwrap_or_default()
277}
278
279/// Get the first value for `key` in `section` as `u64`, or `default`.
280pub(crate) fn get_u64(map: &SectionMap, section: &str, key: &str, default: u64) -> u64 {
281    get(map, section, key)
282        .and_then(|v| v.parse().ok())
283        .unwrap_or(default)
284}
285
286/// Get the first value for `key` in `section` as `u32`, or `default`.
287pub(crate) fn get_u32(map: &SectionMap, section: &str, key: &str, default: u32) -> u32 {
288    get(map, section, key)
289        .and_then(|v| v.parse().ok())
290        .unwrap_or(default)
291}
292
293/// Get the first value for `key` in `section` as a list (split on `,`).
294pub(crate) fn get_array(map: &SectionMap, section: &str, key: &str) -> Vec<String> {
295    match get(map, section, key) {
296        Some(v) if !v.is_empty() => v.split(',').map(|s| s.trim().to_string()).collect(),
297        _ => vec![],
298    }
299}
300
301/// Returns `true` if the section key exists in the map.
302pub(crate) fn section_exists(map: &SectionMap, section: &str) -> bool {
303    map.contains_key(section)
304}
305
306#[cfg(test)]
307mod tests {
308    use super::*;
309
310    const SAMPLE: &str = r#"
311[server]
312ip = "0.0.0.0"
313port = 8080
314
315[[upstream]]
316name = "api"
317backends = ["backend1:8080", "backend2:8080"]
318strategy = "round_robin"
319
320[upstream.health_check]
321path = "/health"
322interval_secs = 10
323timeout_ms = 2000
324healthy_threshold = 2
325unhealthy_threshold = 3
326
327[[route]]
328name = "api-route"
329
330[route.match]
331path = "/api/*"
332method = "GET"
333
334[route.action]
335type = "proxy"
336
337[route.action.proxy]
338upstream = "api"
339connect_timeout_ms = 5000
340read_timeout_ms = 30000
341
342[route.middleware]
343
344[route.middleware.rate_limit]
345max_requests = 100
346window_secs = 60
347
348[[route.middleware.rewrite.request]]
349type = "header_set"
350name = "X-Env"
351value = "production"
352
353[[tcp_proxy]]
354name = "db-proxy"
355listen = "0.0.0.0:5432"
356backends = ["db1:5432", "db2:5432"]
357connect_timeout_ms = 3000
358"#;
359
360    #[test]
361    fn parse_server_section() {
362        let map = parse(SAMPLE);
363        assert_eq!(get(&map, "server", "ip").as_deref(), Some("0.0.0.0"));
364        assert_eq!(get_u64(&map, "server", "port", 0), 8080);
365    }
366
367    #[test]
368    fn parse_upstream() {
369        let map = parse(SAMPLE);
370        assert!(section_exists(&map, "upstream[0]"));
371        assert_eq!(get_str(&map, "upstream[0]", "name"), "api");
372        assert_eq!(
373            get_array(&map, "upstream[0]", "backends"),
374            vec!["backend1:8080", "backend2:8080"]
375        );
376    }
377
378    #[test]
379    fn parse_upstream_health_check() {
380        let map = parse(SAMPLE);
381        assert!(section_exists(&map, "upstream[0].health_check"));
382        assert_eq!(get_str(&map, "upstream[0].health_check", "path"), "/health");
383        assert_eq!(get_u64(&map, "upstream[0].health_check", "interval_secs", 0), 10);
384    }
385
386    #[test]
387    fn parse_route() {
388        let map = parse(SAMPLE);
389        assert!(section_exists(&map, "route[0]"));
390        assert_eq!(get_str(&map, "route[0]", "name"), "api-route");
391        assert_eq!(get_str(&map, "route[0].match", "path"), "/api/*");
392        assert_eq!(get_str(&map, "route[0].match", "method"), "GET");
393        assert_eq!(get_str(&map, "route[0].action.proxy", "upstream"), "api");
394        assert_eq!(get_u64(&map, "route[0].action.proxy", "connect_timeout_ms", 0), 5000);
395    }
396
397    #[test]
398    fn parse_route_middleware() {
399        let map = parse(SAMPLE);
400        assert_eq!(get_u32(&map, "route[0].middleware.rate_limit", "max_requests", 0), 100);
401        assert_eq!(get_u64(&map, "route[0].middleware.rate_limit", "window_secs", 0), 60);
402    }
403
404    #[test]
405    fn parse_nested_rewrite_array() {
406        let map = parse(SAMPLE);
407        assert!(section_exists(&map, "route[0].middleware.rewrite.request[0]"));
408        assert_eq!(get_str(&map, "route[0].middleware.rewrite.request[0]", "type"), "header_set");
409        assert_eq!(get_str(&map, "route[0].middleware.rewrite.request[0]", "name"), "X-Env");
410    }
411
412    #[test]
413    fn parse_tcp_proxy() {
414        let map = parse(SAMPLE);
415        assert!(section_exists(&map, "tcp_proxy[0]"));
416        assert_eq!(get_str(&map, "tcp_proxy[0]", "name"), "db-proxy");
417        assert_eq!(get_str(&map, "tcp_proxy[0]", "listen"), "0.0.0.0:5432");
418        assert_eq!(
419            get_array(&map, "tcp_proxy[0]", "backends"),
420            vec!["db1:5432", "db2:5432"]
421        );
422    }
423
424    #[test]
425    fn multi_line_arrays_are_joined() {
426        let toml = "[authz]\ntuples = [\n  \"doc:1#owner@user:a\",  # first\n  \"doc:2#owner@user:[b]\",\n]\nstore = \"memory\"\n";
427        let map = parse(toml);
428        assert_eq!(get_array(&map, "authz", "tuples"), ["doc:1#owner@user:a", "doc:2#owner@user:[b]"]);
429        assert_eq!(get_str(&map, "authz", "store"), "memory");
430        assert_eq!(logical_lines("a = [1,\n2]\n[t]\nb = []"), ["a = [1, 2]", "[t]", "b = []"]);
431        assert_eq!(logical_lines("a = [1,\n2"), ["a = [1, 2"], "an unclosed array ends with the file");
432    }
433
434    #[test]
435    fn strip_comments_test() {
436        assert_eq!(strip_comment("key = \"value\" # a comment"), "key = \"value\" ");
437        assert_eq!(strip_comment("# full comment"), "");
438        assert_eq!(strip_comment("url = \"http://example.com#fragment\""), "url = \"http://example.com#fragment\"");
439    }
440
441    #[test]
442    fn parse_value_array() {
443        assert_eq!(parse_value(r#"["a", "b", "c"]"#), "a,b,c");
444    }
445
446    #[test]
447    fn parse_value_string() {
448        assert_eq!(parse_value(r#""hello""#), "hello");
449    }
450}