qql-convert 0.4.2

Qdrant REST JSON to QQL statement converter
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
//! Paste shapes: HTTP snippets (`METHOD /path` + JSON) and `curl` commands.
//!
//! Docs and blogs show requests as `METHOD /path` plus a JSON body, or as full
//! `curl` invocations — never the wrapped `{"method","path","body"}` envelope
//! the recorder writes. This module parses those paste shapes back into
//! `(method, path, body)` triples that reuse the shared
//! [`crate::convert::convert_method_path_body`] lowering, so envelope,
//! snippet, and curl inputs cannot drift.
//!
//! Fail-closed rules: `$VAR`/`${VAR}`/`$(…)`/backticks cannot resolve offline,
//! `--data @file` points at bytes the pure [`crate::convert`] function cannot
//! read, and multiple `--data` flags join with `&` in curl (never valid JSON).
//! Full `curl` flag coverage is intentionally bounded — unknown boolean flags
//! are skipped, but anything that carries a value the parse cannot see fails
//! rather than silently misreading the URL or body.

use qql_core::ast::Stmt;
use serde_json::Value;

use crate::ConvertError;
use crate::shell::{self, Item, Word};

pub(crate) struct PastedRequest {
    pub method: String,
    pub path: String,
    pub body: Option<Value>,
    pub line: usize,
}

const METHODS: [&str; 5] = ["GET", "POST", "PUT", "PATCH", "DELETE"];

fn is_method(token: &str) -> bool {
    METHODS.iter().any(|m| m.eq_ignore_ascii_case(token))
}

/// Entry point for [`crate::convert::convert_stmts`]: `None` when the input is
/// not a paste shape (plain JSON / JSONL fall through untouched), otherwise
/// the lowered statements. Errors on a single pasted request surface directly;
/// with several requests the failing one is wrapped in `InvalidLine`.
pub(crate) fn convert_pasted(
    input: &str,
    collection: Option<&str>,
) -> Option<Result<Vec<Stmt>, ConvertError>> {
    let docs = match parse_pasted(input)? {
        Ok(docs) => docs,
        Err(e) => return Some(Err(e)),
    };
    let multi = docs.len() > 1;
    let mut out = Vec::new();
    for doc in &docs {
        match crate::convert::convert_method_path_body(
            &doc.method,
            &doc.path,
            None,
            doc.body.as_ref(),
            collection,
        ) {
            Ok(stmts) => out.extend(stmts),
            Err(e) => {
                return Some(Err(if multi {
                    ConvertError::InvalidLine {
                        line: doc.line,
                        source: Box::new(e),
                    }
                } else {
                    e
                }));
            }
        }
    }
    Some(Ok(out))
}

fn parse_pasted(input: &str) -> Option<Result<Vec<PastedRequest>, ConvertError>> {
    let input = strip_prompt(input);
    let first = input.split_whitespace().next()?;
    if first == "curl" || first == "curl.exe" {
        return Some(parse_curl_docs(input));
    }
    if is_method(first) && header_shape(input.lines().next().unwrap_or("")) {
        return Some(parse_snippet_docs(input));
    }
    None
}

/// A leading `$ ` shell prompt is paste noise, not input.
fn strip_prompt(input: &str) -> &str {
    let trimmed = input.trim_start();
    match trimmed.strip_prefix('$') {
        Some(rest) if rest.starts_with(char::is_whitespace) => rest.trim_start(),
        _ => trimmed,
    }
}

/// Whether a line opens a snippet doc: `METHOD <path-or-URL> [HTTP/x]`.
fn header_shape(line: &str) -> bool {
    let mut parts = line.split_whitespace();
    matches!(parts.next(), Some(m) if is_method(m))
        && matches!(parts.next(), Some(p) if p.starts_with('/') || p.contains("://"))
}

/// Strip `scheme://authority` from a URL, keeping `path?query`. Bare paths
/// pass through; endpoint resolution validates them downstream.
fn url_to_path(url: &str) -> &str {
    match url.find("://") {
        Some(pos) => {
            let after = &url[pos + 3..];
            match after.find('/') {
                Some(i) => &after[i..],
                None => "/",
            }
        }
        None => url,
    }
}

// ── HTTP snippets ────────────────────────────────────────────────────────────

fn parse_snippet_docs(input: &str) -> Result<Vec<PastedRequest>, ConvertError> {
    let mut docs = Vec::new();
    let mut rest = input;
    let mut line = 1usize;
    loop {
        (line, rest) = skip_blank_lines(rest, line);
        if rest.is_empty() {
            break;
        }
        let (header, after) = split_first_line(rest);
        let header_line = line;
        line += 1;
        let (method, path) = parse_header(header)?;
        rest = after;
        let (body, r, l) = take_json_body(rest, line)?;
        rest = r;
        line = l;
        docs.push(PastedRequest {
            method,
            path,
            body,
            line: header_line,
        });
    }
    if docs.is_empty() {
        return Err(ConvertError::undecodable("no request found"));
    }
    Ok(docs)
}

fn parse_header(header: &str) -> Result<(String, String), ConvertError> {
    let bad = || {
        ConvertError::undecodable(format!(
            "expected `METHOD /path` (e.g. `POST /collections/docs/points/query`), got `{header}`"
        ))
    };
    let mut parts = header.split_whitespace();
    let (Some(method), Some(raw_path)) = (parts.next(), parts.next()) else {
        return Err(bad());
    };
    if !is_method(method) {
        return Err(bad());
    }
    match parts.next() {
        None => {}
        Some(v) if v.to_ascii_uppercase().starts_with("HTTP/") && parts.next().is_none() => {}
        _ => return Err(bad()),
    }
    let path = url_to_path(raw_path);
    reject_dollar(path)?;
    Ok((method.to_string(), path.to_string()))
}

/// Read one JSON body after a snippet header: `None` when the next doc (or
/// EOF) follows immediately, so bodyless routes (`GET /collections`) paste
/// cleanly. Bodies may span lines; the first complete JSON value wins and any
/// trailing non-blank text on its lines fails rather than silently dropping.
fn take_json_body(rest: &str, line: usize) -> Result<(Option<Value>, &str, usize), ConvertError> {
    let mut buf = String::new();
    let mut cur_rest = rest;
    let mut cur_line = line;
    loop {
        if buf.trim().is_empty() {
            (cur_line, cur_rest) = skip_blank_lines(cur_rest, cur_line);
            if cur_rest.is_empty() {
                return Ok((None, cur_rest, cur_line));
            }
            if header_shape(split_first_line(cur_rest).0) {
                return Ok((None, cur_rest, cur_line));
            }
        }
        if cur_rest.is_empty() {
            let msg = match first_json_value(&buf) {
                Err(m) => m,
                _ => "unexpected end of input in JSON body".to_string(),
            };
            return Err(ConvertError::InvalidJson(msg));
        }
        let (this, after) = split_first_line(cur_rest);
        buf.push_str(this);
        buf.push('\n');
        cur_rest = after;
        cur_line += 1;
        match first_json_value(&buf) {
            Ok(Some((value, used))) => {
                if !buf[used..].trim().is_empty() {
                    return Err(ConvertError::InvalidJson(
                        "unexpected trailing characters after JSON body".to_string(),
                    ));
                }
                return Ok((Some(value), cur_rest, cur_line));
            }
            Ok(None) => {}
            Err(msg) => return Err(ConvertError::InvalidJson(msg)),
        }
    }
}

/// First complete JSON value in `buf`: `None` when empty or truncated mid-value
/// (callers feed more lines), `Err` on a real syntax error.
fn first_json_value(buf: &str) -> Result<Option<(Value, usize)>, String> {
    let mut stream = serde_json::Deserializer::from_str(buf).into_iter::<Value>();
    match stream.next() {
        None => Ok(None),
        Some(Ok(value)) => Ok(Some((value, stream.byte_offset()))),
        Some(Err(e)) if e.is_eof() => Ok(None),
        Some(Err(e)) => Err(e.to_string()),
    }
}

fn skip_blank_lines(s: &str, line: usize) -> (usize, &str) {
    let (mut l, mut rest) = (line, s);
    loop {
        let (first, after) = split_first_line(rest);
        if first.trim().is_empty() && !rest.is_empty() {
            rest = after;
            l += 1;
        } else {
            return (l, rest);
        }
    }
}

fn split_first_line(s: &str) -> (&str, &str) {
    match s.find('\n') {
        Some(i) => (&s[..i], &s[i + 1..]),
        None => (s, ""),
    }
}

// ── curl ─────────────────────────────────────────────────────────────────────

fn parse_curl_docs(input: &str) -> Result<Vec<PastedRequest>, ConvertError> {
    let mut commands: Vec<Vec<Word>> = Vec::new();
    let mut cur: Vec<Word> = Vec::new();
    for item in shell::tokenize(input)? {
        match item {
            Item::Word(w) => {
                if cur.is_empty() && w.text == "$" {
                    continue; // per-command shell prompt
                }
                cur.push(w);
            }
            Item::Newline(_) | Item::Semi | Item::And | Item::Or => {
                if !cur.is_empty() {
                    commands.push(std::mem::take(&mut cur));
                }
            }
        }
    }
    if !cur.is_empty() {
        commands.push(cur);
    }
    if commands.is_empty() {
        return Err(ConvertError::undecodable("curl: no command found"));
    }
    commands.iter().map(|cmd| parse_curl_command(cmd)).collect()
}

/// Short flags that consume the next token (or an attached value).
const SHORT_VALUE_FLAGS: &str = "XduHAemow";
/// Short boolean flags (also valid combined, e.g. `-sSf`).
const SHORT_BOOL_FLAGS: &str = "sSfkvqNLlipO#012346gG";
/// Long flags that consume the next token (or `=value`).
const LONG_VALUE_FLAGS: [&str; 22] = [
    "request",
    "url",
    "header",
    "user",
    "user-agent",
    "referer",
    "max-time",
    "connect-timeout",
    "retry",
    "retry-delay",
    "output",
    "write-out",
    "proxy",
    "proxy-user",
    "cacert",
    "cert",
    "key",
    "data",
    "data-raw",
    "data-ascii",
    "data-binary",
    "json",
];

fn parse_curl_command(words: &[Word]) -> Result<PastedRequest, ConvertError> {
    let line = words[0].line;
    if words[0].text != "curl" && words[0].text != "curl.exe" {
        return Err(ConvertError::undecodable(format!(
            "expected a `curl` command, got `{}`",
            words[0].text
        )));
    }
    let mut method: Option<String> = None;
    let mut url: Option<String> = None;
    let mut datas: Vec<String> = Vec::new();
    let mut get_flag = false;
    let mut head_flag = false;

    let mut i = 1;
    while i < words.len() {
        let w = &words[i].text;
        if w == "-X" || w == "--request" {
            i += 1;
            method = Some(next_value(words, i, w)?.to_string());
        } else if w.starts_with("-X") && w.len() > 2 {
            method = Some(w[2..].to_string());
        } else if is_data_flag(w) {
            i += 1;
            datas.push(check_data_value(next_value(words, i, w)?)?);
        } else if w.starts_with("-d") && w.len() > 2 {
            datas.push(check_data_value(&w[2..])?);
        } else if w == "-T" || w == "--upload-file" || w == "-K" || w == "--config" {
            return Err(ConvertError::undecodable(format!(
                "curl: `{w}` reads a file — paste literal values instead"
            )));
        } else if w == "--data-urlencode" {
            return Err(ConvertError::undecodable(
                "curl: `--data-urlencode` has no QQL mapping — pass a JSON body with `-d`",
            ));
        } else if w.starts_with("--") {
            i = parse_long_flag(
                words,
                i,
                &mut method,
                &mut url,
                &mut datas,
                &mut get_flag,
                &mut head_flag,
            )?;
        } else if w.starts_with('-') && w.len() > 1 {
            i = parse_shorts(
                words,
                i,
                &mut method,
                &mut datas,
                &mut get_flag,
                &mut head_flag,
            )?;
        } else if url.is_none() {
            url = Some(w.clone());
        } else {
            return Err(ConvertError::undecodable(format!(
                "curl: unexpected extra argument `{w}`"
            )));
        }
        i += 1;
    }

    let Some(url) = url else {
        return Err(ConvertError::undecodable(
            "curl: no URL — expected `curl [flags] <url>`",
        ));
    };
    let body = match datas.len() {
        0 => None,
        1 => Some(
            serde_json::from_str(&datas[0])
                .map_err(|e| ConvertError::InvalidJson(e.to_string()))?,
        ),
        _ => {
            return Err(ConvertError::undecodable(
                "curl: multiple `--data` flags join with `&` — pass a single JSON body",
            ));
        }
    };
    let method = match method {
        Some(m) => m,
        None if head_flag => "HEAD".to_string(),
        None if get_flag || body.is_none() => "GET".to_string(),
        None => "POST".to_string(),
    };
    if body.is_some() && (method.eq_ignore_ascii_case("GET") || method.eq_ignore_ascii_case("HEAD"))
    {
        return Err(ConvertError::undecodable(format!(
            "curl: {method} with `--data` has no QQL mapping — drop `--data` or pass `-X POST`"
        )));
    }
    let path = url_to_path(&url);
    reject_dollar(path)?;
    Ok(PastedRequest {
        method,
        path: path.to_string(),
        body,
        line,
    })
}

fn is_data_flag(w: &str) -> bool {
    matches!(
        w,
        "-d" | "--data" | "--data-raw" | "--data-ascii" | "--data-binary" | "--json"
    )
}

fn next_value<'a>(words: &'a [Word], i: usize, flag: &str) -> Result<&'a str, ConvertError> {
    words
        .get(i)
        .map(|w| w.text.as_str())
        .ok_or_else(|| ConvertError::undecodable(format!("curl: `{flag}` needs a value")))
}

/// `--data @file` (and `@-` stdin) point at bytes an offline converter cannot
/// read. Shell process substitution (`<(…)`) is not a file either.
fn check_data_value(value: &str) -> Result<String, ConvertError> {
    if value.starts_with('@') {
        return Err(ConvertError::undecodable(
            "curl: `--data @file` reads a file — paste the JSON body inline instead",
        ));
    }
    Ok(value.to_string())
}

fn parse_long_flag(
    words: &[Word],
    i: usize,
    method: &mut Option<String>,
    url: &mut Option<String>,
    datas: &mut Vec<String>,
    get_flag: &mut bool,
    head_flag: &mut bool,
) -> Result<usize, ConvertError> {
    let w = &words[i].text;
    let (name, attached) = match w[2..].split_once('=') {
        Some((n, v)) => (n, Some(v)),
        None => (w[2..].as_ref(), None),
    };
    let mut i = i;
    let value = match attached {
        Some(v) => v.to_string(),
        None if LONG_VALUE_FLAGS.contains(&name) => {
            i += 1;
            next_value(words, i, w)?.to_string()
        }
        None => String::new(),
    };
    match name {
        "request" => *method = Some(value),
        "data" | "data-raw" | "data-ascii" | "data-binary" | "json" => {
            datas.push(check_data_value(&value)?);
        }
        "url" => *url = Some(value),
        "get" => *get_flag = true,
        "head" => *head_flag = true,
        "data-urlencode" | "config" | "upload-file" => {
            return Err(ConvertError::undecodable(format!(
                "curl: `--{name}` has no QQL mapping — paste literal values instead"
            )));
        }
        _ => {} // unknown boolean flags (e.g. `--fail`, `--compressed`) are inert
    }
    Ok(i)
}

fn parse_shorts(
    words: &[Word],
    i: usize,
    method: &mut Option<String>,
    datas: &mut Vec<String>,
    get_flag: &mut bool,
    head_flag: &mut bool,
) -> Result<usize, ConvertError> {
    let w = &words[i].text;
    let mut i = i;
    let mut chars = w[1..].chars();
    while let Some(c) = chars.next() {
        if SHORT_VALUE_FLAGS.contains(c) {
            let rest: String = chars.collect();
            let value = if rest.is_empty() {
                i += 1;
                next_value(words, i, w)?.to_string()
            } else {
                rest
            };
            match c {
                'X' => *method = Some(value),
                'd' => datas.push(check_data_value(&value)?),
                'u' | 'H' | 'A' | 'e' | 'm' | 'o' | 'w' => {} // credentials / tuning: inert
                // Drift guard: `SHORT_VALUE_FLAGS` membership was checked
                // above, so every table member is covered. Fail closed (never
                // panic) if the flag table and this match ever drift apart.
                _ => {
                    return Err(ConvertError::undecodable(format!(
                        "curl: unsupported flag `-{c}` in `{w}`"
                    )));
                }
            }
            break; // the remainder was the value
        } else if SHORT_BOOL_FLAGS.contains(c) {
            match c {
                'G' => *get_flag = true,
                'I' => *head_flag = true,
                _ => {}
            }
        } else if c == 'T' || c == 'K' {
            return Err(ConvertError::undecodable(format!(
                "curl: `-{c}` reads a file — paste literal values instead"
            )));
        } else {
            return Err(ConvertError::undecodable(format!(
                "curl: unsupported flag `-{c}` in `{w}`"
            )));
        }
    }
    Ok(i)
}

/// `$` survives only inside single quotes (no shell expansion there), so a `$`
/// in the final path is always a variable or placeholder the paste left
/// behind — emitting it would produce QQL that cannot re-parse.
fn reject_dollar(path: &str) -> Result<(), ConvertError> {
    if path.contains('$') {
        return Err(ConvertError::UndecodableBody {
            detail: "path contains `$` — replace variables/placeholders with literal values"
                .to_string(),
        });
    }
    Ok(())
}