MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / parse_object_literal_array

Function parse_object_literal_array

nodedb-sql/src/parser/object_literal.rs:31–73  ·  view source on GitHub ↗

Parse `[{ ... }, { ... }]` — an array of object literals for batch insert. Returns `None` if the input doesn't start with `[` (not an array literal). Returns `Some(Err(msg))` on parse errors. Returns `Some(Ok(vec))` on success — each element must be an object.

(
    s: &str,
)

Source from the content-addressed store, hash-verified

29/// Returns `Some(Err(msg))` on parse errors.
30/// Returns `Some(Ok(vec))` on success — each element must be an object.
31pub fn parse_object_literal_array(
32 s: &str,
33) -> Option<Result<Vec<HashMap<String, Value>>, SqlError>> {
34 let trimmed = s.trim();
35 if !trimmed.starts_with('[') {
36 return None;
37 }
38 let chars: Vec<char> = trimmed.chars().collect();
39 let mut pos = 0;
40
41 // Consume '['
42 pos += 1;
43 let mut objects = Vec::new();
44 loop {
45 skip_ws(&chars, &mut pos);
46 if pos >= chars.len() {
47 return Some(Err(SqlError::Parse {
48 detail: "unterminated array of objects".to_string(),
49 }));
50 }
51 if chars[pos] == ']' {
52 break;
53 }
54 if chars[pos] == ',' {
55 pos += 1;
56 continue;
57 }
58 if chars[pos] != '{' {
59 return Some(Err(SqlError::Parse {
60 detail: format!("expected '{{' at position {pos}, found '{}'", chars[pos]),
61 }));
62 }
63 match parse_object(&chars, &mut pos) {
64 Ok(obj) => objects.push(obj),
65 Err(e) => return Some(Err(e)),
66 }
67 skip_ws(&chars, &mut pos);
68 if pos < chars.len() && chars[pos] == ',' {
69 pos += 1;
70 }
71 }
72 Some(Ok(objects))
73}
74
75fn skip_ws(chars: &[char], pos: &mut usize) {
76 while *pos < chars.len() && chars[*pos].is_ascii_whitespace() {

Callers 5

parse_array_of_objectsFunction · 0.85
parse_array_emptyFunction · 0.85
rewrite_array_formFunction · 0.85

Calls 6

parse_objectFunction · 0.85
collectMethod · 0.80
to_stringMethod · 0.80
skip_wsFunction · 0.70
lenMethod · 0.45
pushMethod · 0.45

Tested by 4

parse_array_of_objectsFunction · 0.68
parse_array_emptyFunction · 0.68