ocular-protocol 0.12.0

Wire protocol parsers for ocular (Redis, MySQL, PostgreSQL, MongoDB, AMQP, HTTP)
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
652
653
654
655
//! MongoDB wire protocol parser (OP_MSG only, modern MongoDB 3.6+)
//! All integers are little-endian.

const OP_MSG: i32 = 2013;
const OP_COMPRESSED: i32 = 2012;

/// Get the total message length from a MongoDB wire protocol header.
/// Returns None if buffer is too small or length is invalid.
pub fn mongo_msg_len(buf: &[u8]) -> Option<usize> {
    if buf.len() < 4 { return None; }
    let len = i32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) as usize;
    if !(16..=48 * 1024 * 1024).contains(&len) { return None; }
    Some(len)
}

/// Parse a MongoDB request (client→server), returning a command summary.
pub fn parse_mongo_request(buf: &[u8]) -> Option<String> {
    let doc = extract_body_doc(buf)?;
    let cmd = first_key(&doc)?;
    let db = get_string_field(&doc, "$db").unwrap_or_default();
    let detail = match cmd.as_str() {
        "find" => {
            let coll = get_string_field(&doc, "find").unwrap_or_default();
            let filter = get_doc_field_summary(&doc, "filter");
            format!("find {}.{} {}", db, coll, filter)
        }
        "insert" => {
            let coll = get_string_field(&doc, "insert").unwrap_or_default();
            let n = get_array_len(&doc, "documents");
            format!("insert {}.{} ({} docs)", db, coll, n)
        }
        "update" => {
            let coll = get_string_field(&doc, "update").unwrap_or_default();
            let n = get_array_len(&doc, "updates");
            format!("update {}.{} ({} ops)", db, coll, n)
        }
        "delete" => {
            let coll = get_string_field(&doc, "delete").unwrap_or_default();
            let n = get_array_len(&doc, "deletes");
            format!("delete {}.{} ({} ops)", db, coll, n)
        }
        "aggregate" => {
            let coll = get_string_field(&doc, "aggregate").unwrap_or_default();
            format!("aggregate {}.{}", db, coll)
        }
        "getMore" => {
            let coll = get_string_field(&doc, "collection").unwrap_or_default();
            format!("getMore {}.{}", db, coll)
        }
        _ => {
            if db.is_empty() { cmd.clone() } else { format!("{} {}", cmd, db) }
        }
    };
    Some(detail)
}

/// Extract full command detail (for Detail panel) — mongosh-style replayable statements.
pub fn extract_mongo_full_command(buf: &[u8]) -> Option<String> {
    let doc = extract_body_doc(buf)?;
    let cmd = first_key(&doc)?;
    let db = get_string_field(&doc, "$db").unwrap_or_default();
    match cmd.as_str() {
        "find" => {
            let coll = get_string_field(&doc, "find").unwrap_or_default();
            let filter = get_doc_field_summary(&doc, "filter");
            let limit = get_i32_field(&doc, "limit");
            let sort = get_raw_doc_field(&doc, "sort").map(|d| bson_doc_to_json_like(&d));
            let mut s = format!("db.{}.find({})", coll, filter);
            if let Some(sort_str) = sort { s.push_str(&format!(".sort({})", sort_str)); }
            if let Some(l) = limit { s.push_str(&format!(".limit({})", l)); }
            Some(s)
        }
        "insert" => {
            let coll = get_string_field(&doc, "insert").unwrap_or_default();
            let docs = get_array_docs(&doc, "documents");
            if docs.len() == 1 {
                Some(format!("db.{}.insertOne({})", coll, bson_doc_to_json_like(&docs[0])))
            } else {
                let items: Vec<String> = docs.iter().take(10).map(|d| bson_doc_to_json_like(d)).collect();
                let mut s = format!("db.{}.insertMany([{}])", coll, items.join(", "));
                if docs.len() > 10 { s.push_str(&format!(" // +{} more", docs.len() - 10)); }
                Some(s)
            }
        }
        "update" => {
            let coll = get_string_field(&doc, "update").unwrap_or_default();
            let updates = get_array_docs(&doc, "updates");
            if updates.len() == 1 {
                let q = get_doc_field_summary(&updates[0], "q");
                let u = get_doc_field_summary(&updates[0], "u");
                let multi = get_i32_field(&updates[0], "multi").unwrap_or(0) != 0
                    || has_field(&updates[0], "multi") && get_f64_field(&updates[0], "multi") == Some(1.0);
                let method = if multi { "updateMany" } else { "updateOne" };
                Some(format!("db.{}.{}({}, {})", coll, method, q, u))
            } else {
                Some(format!("db.{}.bulkWrite([...{} ops])", coll, updates.len()))
            }
        }
        "delete" => {
            let coll = get_string_field(&doc, "delete").unwrap_or_default();
            let deletes = get_array_docs(&doc, "deletes");
            if deletes.len() == 1 {
                let q = get_doc_field_summary(&deletes[0], "q");
                let limit = get_i32_field(&deletes[0], "limit").unwrap_or(0);
                let method = if limit == 1 { "deleteOne" } else { "deleteMany" };
                Some(format!("db.{}.{}({})", coll, method, q))
            } else {
                Some(format!("db.{}.bulkWrite([...{} ops])", coll, deletes.len()))
            }
        }
        "aggregate" => {
            let coll = get_string_field(&doc, "aggregate").unwrap_or_default();
            Some(format!("db.{}.aggregate([...])", coll))
        }
        "findAndModify" => {
            let coll = get_string_field(&doc, "findAndModify").unwrap_or_default();
            let query = get_doc_field_summary(&doc, "query");
            let update = get_doc_field_summary(&doc, "update");
            Some(format!("db.{}.findOneAndUpdate({}, {})", coll, query, update))
        }
        "count" | "countDocuments" => {
            let coll = get_string_field(&doc, &cmd).unwrap_or_default();
            let query = get_doc_field_summary(&doc, "query");
            Some(format!("db.{}.countDocuments({})", coll, query))
        }
        _ => {
            if db.is_empty() { Some(cmd) } else { Some(format!("{} {}", cmd, db)) }
        }
    }
}

/// Parse a MongoDB response (server→client), returning a summary.
pub fn parse_mongo_response(buf: &[u8]) -> Option<String> {
    let doc = extract_body_doc(buf)?;
    let ok = get_f64_field(&doc, "ok");
    if ok == Some(0.0) {
        let errmsg = get_string_field(&doc, "errmsg").unwrap_or("error".into());
        let code = get_i32_field(&doc, "code").map(|c| format!(" ({})", c)).unwrap_or_default();
        return Some(format!("ERR{} {}", code, errmsg));
    }
    // Check for cursor result
    if let Some(cursor_doc) = get_raw_doc_field(&doc, "cursor") {
        let batch_key = if has_field(&cursor_doc, "firstBatch") { "firstBatch" } else { "nextBatch" };
        let n = get_array_len(&cursor_doc, batch_key);
        return Some(format!("OK ({} docs)", n));
    }
    // Check for n (insert/update/delete result)
    if let Some(n) = get_i32_field(&doc, "n") {
        let modified = get_i32_field(&doc, "nModified");
        if let Some(m) = modified {
            return Some(format!("OK (n={}, modified={})", n, m));
        }
        return Some(format!("OK (n={})", n));
    }
    Some("OK".into())
}

/// Format detailed response for the detail panel.
pub fn format_mongo_response_detail(buf: &[u8]) -> Option<String> {
    let doc = extract_body_doc(buf)?;
    let ok = get_f64_field(&doc, "ok");
    if ok == Some(0.0) {
        let errmsg = get_string_field(&doc, "errmsg").unwrap_or("error".into());
        let code = get_i32_field(&doc, "code").unwrap_or(0);
        let codename = get_string_field(&doc, "codeName").unwrap_or_default();
        return Some(format!("ERROR {} ({}): {}", code, codename, errmsg));
    }
    if let Some(cursor_doc) = get_raw_doc_field(&doc, "cursor") {
        let batch_key = if has_field(&cursor_doc, "firstBatch") { "firstBatch" } else { "nextBatch" };
        let docs = get_array_docs(&cursor_doc, batch_key);
        let mut lines = Vec::new();
        lines.push(format!("{} documents:", docs.len()));
        for (i, d) in docs.iter().enumerate().take(20) {
            lines.push(format!("  [{}] {}", i, bson_doc_to_json_like(d)));
        }
        if docs.len() > 20 {
            lines.push(format!("  ... ({} more)", docs.len() - 20));
        }
        return Some(lines.join("\n"));
    }
    parse_mongo_response(buf)
}

// --- Internal helpers ---

/// Extract the Kind 0 body BSON document from an OP_MSG.
fn extract_body_doc(buf: &[u8]) -> Option<Vec<u8>> {
    if buf.len() < 21 { return None; } // header(16) + flags(4) + kind(1)
    let opcode = i32::from_le_bytes([buf[12], buf[13], buf[14], buf[15]]);
    if opcode != OP_MSG && opcode != OP_COMPRESSED { return None; }
    if opcode == OP_COMPRESSED { return decompress_op_compressed(buf); }
    // flags at offset 16, sections start at offset 20
    let mut pos = 20;
    while pos < buf.len() {
        let kind = buf[pos];
        pos += 1;
        if kind == 0 {
            // Kind 0: single BSON document
            if pos + 4 > buf.len() { return None; }
            let doc_len = i32::from_le_bytes([buf[pos], buf[pos+1], buf[pos+2], buf[pos+3]]) as usize;
            if pos + doc_len > buf.len() { return None; }
            return Some(buf[pos..pos+doc_len].to_vec());
        } else if kind == 1 {
            // Kind 1: document sequence, skip
            if pos + 4 > buf.len() { return None; }
            let sec_len = i32::from_le_bytes([buf[pos], buf[pos+1], buf[pos+2], buf[pos+3]]) as usize;
            pos += sec_len;
        } else {
            break;
        }
    }
    None
}

/// Decompress an OP_COMPRESSED message and extract the body doc from the inner OP_MSG.
fn decompress_op_compressed(buf: &[u8]) -> Option<Vec<u8>> {
    if buf.len() < 25 { return None; }
    let original_opcode = i32::from_le_bytes([buf[16], buf[17], buf[18], buf[19]]);
    if original_opcode != OP_MSG { return None; }
    let uncompressed_size = i32::from_le_bytes([buf[20], buf[21], buf[22], buf[23]]) as usize;
    let compressor_id = buf[24];
    let compressed = &buf[25..];

    let decompressed = match compressor_id {
        0 => compressed.to_vec(),
        1 => snap::raw::Decoder::new().decompress_vec(compressed).ok()?,
        2 => {
            use std::io::Read;
            let mut decoder = flate2::read::ZlibDecoder::new(compressed);
            let mut out = Vec::with_capacity(uncompressed_size);
            decoder.read_to_end(&mut out).ok()?;
            out
        }
        3 => zstd::decode_all(compressed).ok()?,
        _ => return None,
    };

    // decompressed = flags(4) + sections... (OP_MSG body without 16-byte header)
    if decompressed.len() < 5 { return None; }
    let mut pos = 4; // skip flags
    while pos < decompressed.len() {
        let kind = decompressed[pos];
        pos += 1;
        if kind == 0 {
            if pos + 4 > decompressed.len() { return None; }
            let doc_len = i32::from_le_bytes([decompressed[pos], decompressed[pos+1], decompressed[pos+2], decompressed[pos+3]]) as usize;
            if pos + doc_len > decompressed.len() { return None; }
            return Some(decompressed[pos..pos+doc_len].to_vec());
        } else if kind == 1 {
            if pos + 4 > decompressed.len() { return None; }
            let sec_len = i32::from_le_bytes([decompressed[pos], decompressed[pos+1], decompressed[pos+2], decompressed[pos+3]]) as usize;
            pos += sec_len;
        } else {
            break;
        }
    }
    None
}

/// Get the first key name from a BSON document (the command name).
fn first_key(doc: &[u8]) -> Option<String> {
    if doc.len() < 6 { return None; }
    // doc[0..4] = size, doc[4] = element type, doc[5..] = cstring key
    let key = read_cstr(&doc[5..])?;
    Some(key)
}

/// Read a null-terminated C string.
fn read_cstr(buf: &[u8]) -> Option<String> {
    let end = buf.iter().position(|&b| b == 0)?;
    Some(String::from_utf8_lossy(&buf[..end]).to_string())
}

/// Get a string field value from a BSON document.
fn get_string_field(doc: &[u8], name: &str) -> Option<String> {
    let mut pos = 4; // skip doc size
    while pos < doc.len() - 1 {
        let etype = doc[pos];
        if etype == 0 { break; } // end of doc
        pos += 1;
        let key = read_cstr(&doc[pos..])?;
        pos += key.len() + 1;
        match etype {
            0x02 => { // string
                if pos + 4 > doc.len() { return None; }
                let slen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize;
                pos += 4;
                if key == name {
                    let s = String::from_utf8_lossy(&doc[pos..pos+slen.saturating_sub(1)]).to_string();
                    return Some(s);
                }
                pos += slen;
            }
            0x01 => { pos += 8; } // double
            0x03 | 0x04 => { // document or array
                if pos + 4 > doc.len() { return None; }
                let dlen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize;
                pos += dlen;
            }
            0x05 => { // binary
                if pos + 4 > doc.len() { return None; }
                let blen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize;
                pos += 5 + blen;
            }
            0x07 => { pos += 12; } // ObjectId
            0x08 => { pos += 1; } // boolean
            0x09 | 0x11 | 0x12 => { pos += 8; } // datetime, timestamp, int64
            0x0A => {} // null
            0x10 => { pos += 4; } // int32
            0x13 => { pos += 16; } // decimal128
            _ => { return None; } // unknown type, bail
        }
    }
    None
}

fn get_f64_field(doc: &[u8], name: &str) -> Option<f64> {
    let mut pos = 4;
    while pos < doc.len() - 1 {
        let etype = doc[pos];
        if etype == 0 { break; }
        pos += 1;
        let key = read_cstr(&doc[pos..])?;
        pos += key.len() + 1;
        match etype {
            0x01 => {
                if key == name && pos + 8 <= doc.len() {
                    return Some(f64::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3], doc[pos+4], doc[pos+5], doc[pos+6], doc[pos+7]]));
                }
                pos += 8;
            }
            0x10 => {
                if key == name && pos + 4 <= doc.len() {
                    let v = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]);
                    return Some(v as f64);
                }
                pos += 4;
            }
            0x02 => { if pos + 4 > doc.len() { return None; } let slen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += 4 + slen; }
            0x03 | 0x04 => { if pos + 4 > doc.len() { return None; } let dlen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += dlen; }
            0x05 => { if pos + 4 > doc.len() { return None; } let blen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += 5 + blen; }
            0x07 => { pos += 12; }
            0x08 => { pos += 1; }
            0x09 | 0x11 | 0x12 => { pos += 8; }
            0x0A => {}
            0x13 => { pos += 16; }
            _ => { return None; }
        }
    }
    None
}

fn get_i32_field(doc: &[u8], name: &str) -> Option<i32> {
    let mut pos = 4;
    while pos < doc.len() - 1 {
        let etype = doc[pos];
        if etype == 0 { break; }
        pos += 1;
        let key = read_cstr(&doc[pos..])?;
        pos += key.len() + 1;
        match etype {
            0x10 => {
                if key == name && pos + 4 <= doc.len() {
                    return Some(i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]));
                }
                pos += 4;
            }
            0x01 => { pos += 8; }
            0x02 => { if pos + 4 > doc.len() { return None; } let slen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += 4 + slen; }
            0x03 | 0x04 => { if pos + 4 > doc.len() { return None; } let dlen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += dlen; }
            0x05 => { if pos + 4 > doc.len() { return None; } let blen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += 5 + blen; }
            0x07 => { pos += 12; }
            0x08 => { pos += 1; }
            0x09 | 0x11 | 0x12 => { pos += 8; }
            0x0A => {}
            0x13 => { pos += 16; }
            _ => { return None; }
        }
    }
    None
}

fn get_raw_doc_field(doc: &[u8], name: &str) -> Option<Vec<u8>> {
    let mut pos = 4;
    while pos < doc.len() - 1 {
        let etype = doc[pos];
        if etype == 0 { break; }
        pos += 1;
        let key = read_cstr(&doc[pos..])?;
        pos += key.len() + 1;
        match etype {
            0x03 | 0x04 => {
                if pos + 4 > doc.len() { return None; }
                let dlen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize;
                if key == name {
                    return Some(doc[pos..pos+dlen].to_vec());
                }
                pos += dlen;
            }
            0x01 => { pos += 8; }
            0x02 => { if pos + 4 > doc.len() { return None; } let slen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += 4 + slen; }
            0x05 => { if pos + 4 > doc.len() { return None; } let blen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += 5 + blen; }
            0x07 => { pos += 12; }
            0x08 => { pos += 1; }
            0x09 | 0x10 | 0x11 | 0x12 => { pos += if etype == 0x10 { 4 } else { 8 }; }
            0x0A => {}
            0x13 => { pos += 16; }
            _ => { return None; }
        }
    }
    None
}

fn has_field(doc: &[u8], name: &str) -> bool {
    let mut pos = 4;
    while pos < doc.len().saturating_sub(1) {
        let etype = doc[pos];
        if etype == 0 { break; }
        pos += 1;
        let Some(key) = read_cstr(&doc[pos..]) else { break };
        if key == name { return true; }
        pos += key.len() + 1;
        match etype {
            0x01 => { pos += 8; }
            0x02 => { if pos + 4 > doc.len() { break; } let slen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += 4 + slen; }
            0x03 | 0x04 => { if pos + 4 > doc.len() { break; } let dlen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += dlen; }
            0x05 => { if pos + 4 > doc.len() { break; } let blen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += 5 + blen; }
            0x07 => { pos += 12; }
            0x08 => { pos += 1; }
            0x09 | 0x11 | 0x12 => { pos += 8; }
            0x0A => {}
            0x10 => { pos += 4; }
            0x13 => { pos += 16; }
            _ => { break; }
        }
    }
    false
}

fn get_array_len(doc: &[u8], name: &str) -> usize {
    let Some(arr) = get_raw_doc_field(doc, name) else { return 0 };
    // BSON array is a document with "0", "1", ... keys
    let mut count = 0;
    let mut pos = 4;
    while pos < arr.len().saturating_sub(1) {
        if arr[pos] == 0 { break; }
        count += 1;
        pos += 1;
        let Some(key) = read_cstr(&arr[pos..]) else { break };
        pos += key.len() + 1;
        // skip value based on type
        let etype = arr[pos - key.len() - 2];
        match etype {
            0x01 => { pos += 8; }
            0x02 => { if pos + 4 > arr.len() { break; } let slen = i32::from_le_bytes([arr[pos], arr[pos+1], arr[pos+2], arr[pos+3]]) as usize; pos += 4 + slen; }
            0x03 | 0x04 => { if pos + 4 > arr.len() { break; } let dlen = i32::from_le_bytes([arr[pos], arr[pos+1], arr[pos+2], arr[pos+3]]) as usize; pos += dlen; }
            0x05 => { if pos + 4 > arr.len() { break; } let blen = i32::from_le_bytes([arr[pos], arr[pos+1], arr[pos+2], arr[pos+3]]) as usize; pos += 5 + blen; }
            0x07 => { pos += 12; }
            0x08 => { pos += 1; }
            0x09 | 0x11 | 0x12 => { pos += 8; }
            0x0A => {}
            0x10 => { pos += 4; }
            0x13 => { pos += 16; }
            _ => { break; }
        }
    }
    count
}

fn get_array_docs(doc: &[u8], name: &str) -> Vec<Vec<u8>> {
    let Some(arr) = get_raw_doc_field(doc, name) else { return vec![] };
    let mut docs = Vec::new();
    let mut pos = 4;
    while pos < arr.len().saturating_sub(1) {
        let etype = arr[pos];
        if etype == 0 { break; }
        pos += 1;
        let Some(key) = read_cstr(&arr[pos..]) else { break };
        pos += key.len() + 1;
        if etype == 0x03 {
            if pos + 4 > arr.len() { break; }
            let dlen = i32::from_le_bytes([arr[pos], arr[pos+1], arr[pos+2], arr[pos+3]]) as usize;
            if pos + dlen <= arr.len() {
                docs.push(arr[pos..pos+dlen].to_vec());
            }
            pos += dlen;
        } else {
            break; // unexpected type in result array
        }
    }
    docs
}

fn get_doc_field_summary(doc: &[u8], name: &str) -> String {
    let Some(subdoc) = get_raw_doc_field(doc, name) else { return "{}".into() };
    bson_doc_to_json_like(&subdoc)
}

/// Simple BSON doc to JSON-like string (for display, not full fidelity).
fn bson_doc_to_json_like(doc: &[u8]) -> String {
    let mut parts = Vec::new();
    let mut pos = 4;
    while pos < doc.len().saturating_sub(1) {
        let etype = doc[pos];
        if etype == 0 { break; }
        pos += 1;
        let Some(key) = read_cstr(&doc[pos..]) else { break };
        pos += key.len() + 1;
        let val = match etype {
            0x01 => { let v = if pos + 8 <= doc.len() { f64::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3], doc[pos+4], doc[pos+5], doc[pos+6], doc[pos+7]]) } else { 0.0 }; pos += 8; format!("{}", v) }
            0x02 => { if pos + 4 > doc.len() { break; } let slen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += 4; let s = String::from_utf8_lossy(&doc[pos..pos+slen.saturating_sub(1)]).to_string(); pos += slen; format!("\"{}\"", s) }
            0x03 => { if pos + 4 > doc.len() { break; } let dlen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; let s = bson_doc_to_json_like(&doc[pos..pos+dlen]); pos += dlen; s }
            0x04 => { if pos + 4 > doc.len() { break; } let dlen = i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) as usize; pos += dlen; "[...]".into() }
            0x07 => { pos += 12; "ObjectId(...)".into() }
            0x08 => { let v = doc[pos] != 0; pos += 1; format!("{}", v) }
            0x09 => { pos += 8; "Date(...)".into() }
            0x0A => { "null".into() }
            0x10 => { let v = if pos + 4 <= doc.len() { i32::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3]]) } else { 0 }; pos += 4; format!("{}", v) }
            0x12 => { let v = if pos + 8 <= doc.len() { i64::from_le_bytes([doc[pos], doc[pos+1], doc[pos+2], doc[pos+3], doc[pos+4], doc[pos+5], doc[pos+6], doc[pos+7]]) } else { 0 }; pos += 8; format!("{}", v) }
            _ => { break; }
        };
        if key == "_id" || key == "lsid" { continue; }
        parts.push(format!("{}: {}", key, val));
        if parts.len() >= 8 { parts.push("...".into()); break; }
    }
    format!("{{{}}}", parts.join(", "))
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Build a minimal OP_MSG with a Kind 0 BSON body document.
    fn build_op_msg(doc: &[u8]) -> Vec<u8> {
        let msg_len = 16 + 4 + 1 + doc.len(); // header + flags + kind + doc
        let mut buf = Vec::new();
        buf.extend_from_slice(&(msg_len as i32).to_le_bytes()); // messageLength
        buf.extend_from_slice(&1i32.to_le_bytes()); // requestID
        buf.extend_from_slice(&0i32.to_le_bytes()); // responseTo
        buf.extend_from_slice(&OP_MSG.to_le_bytes()); // opCode
        buf.extend_from_slice(&0u32.to_le_bytes()); // flagBits
        buf.push(0); // kind 0
        buf.extend_from_slice(doc);
        buf
    }

    /// Build a simple BSON document: {"cmd": "coll", "$db": "testdb"}
    fn build_simple_cmd(cmd: &str, coll: &str) -> Vec<u8> {
        let mut doc = Vec::new();
        doc.extend_from_slice(&[0; 4]); // placeholder for size
        // cmd: coll (string)
        doc.push(0x02); // string type
        doc.extend_from_slice(cmd.as_bytes());
        doc.push(0);
        let val = format!("{}\0", coll);
        doc.extend_from_slice(&(val.len() as i32).to_le_bytes());
        doc.extend_from_slice(val.as_bytes());
        // $db: "testdb" (string)
        doc.push(0x02);
        doc.extend_from_slice(b"$db\0");
        let db = "testdb\0";
        doc.extend_from_slice(&(db.len() as i32).to_le_bytes());
        doc.extend_from_slice(db.as_bytes());
        // end
        doc.push(0);
        let len = doc.len() as i32;
        doc[0..4].copy_from_slice(&len.to_le_bytes());
        doc
    }

    #[test]
    fn test_parse_find_request() {
        let doc = build_simple_cmd("find", "users");
        let buf = build_op_msg(&doc);
        let result = parse_mongo_request(&buf).unwrap();
        assert!(result.contains("find"));
        assert!(result.contains("testdb"));
        assert!(result.contains("users"));
    }

    #[test]
    fn test_parse_insert_request() {
        let doc = build_simple_cmd("insert", "users");
        let buf = build_op_msg(&doc);
        let result = parse_mongo_request(&buf).unwrap();
        assert!(result.contains("insert"));
        assert!(result.contains("testdb.users"));
    }

    #[test]
    fn test_parse_response_ok() {
        // {"ok": 1.0}
        let mut doc = Vec::new();
        doc.extend_from_slice(&[0; 4]);
        doc.push(0x01); // double
        doc.extend_from_slice(b"ok\0");
        doc.extend_from_slice(&1.0f64.to_le_bytes());
        doc.push(0);
        let len = doc.len() as i32;
        doc[0..4].copy_from_slice(&len.to_le_bytes());

        let buf = build_op_msg(&doc);
        let result = parse_mongo_response(&buf).unwrap();
        assert_eq!(result, "OK");
    }

    #[test]
    fn test_parse_response_error() {
        // {"ok": 0.0, "errmsg": "not found", "code": 26}
        let mut doc = Vec::new();
        doc.extend_from_slice(&[0; 4]);
        // ok: 0.0
        doc.push(0x01);
        doc.extend_from_slice(b"ok\0");
        doc.extend_from_slice(&0.0f64.to_le_bytes());
        // errmsg: "not found"
        doc.push(0x02);
        doc.extend_from_slice(b"errmsg\0");
        let msg = "not found\0";
        doc.extend_from_slice(&(msg.len() as i32).to_le_bytes());
        doc.extend_from_slice(msg.as_bytes());
        // code: 26
        doc.push(0x10);
        doc.extend_from_slice(b"code\0");
        doc.extend_from_slice(&26i32.to_le_bytes());
        doc.push(0);
        let len = doc.len() as i32;
        doc[0..4].copy_from_slice(&len.to_le_bytes());

        let buf = build_op_msg(&doc);
        let result = parse_mongo_response(&buf).unwrap();
        assert!(result.contains("ERR"));
        assert!(result.contains("26"));
        assert!(result.contains("not found"));
    }

    #[test]
    fn test_mongo_msg_len() {
        let buf = build_op_msg(&build_simple_cmd("ping", "admin"));
        assert_eq!(mongo_msg_len(&buf), Some(buf.len()));
    }

    #[test]
    fn test_mongo_msg_len_too_short() {
        assert_eq!(mongo_msg_len(&[1, 2, 3]), None);
    }

    #[test]
    fn test_extract_full_command_find() {
        let doc = build_simple_cmd("find", "users");
        let buf = build_op_msg(&doc);
        let result = extract_mongo_full_command(&buf).unwrap();
        assert!(result.contains("db.users.find"));
    }
}