memcache-proto 0.0.2

Memcache ASCII and binary protocol parser and encoder
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! Streaming parser for large memcache binary SET commands.
//!
//! This module provides streaming support for SET commands with large values,
//! allowing zero-copy receive directly into cache segment memory.
//!
//! # Overview
//!
//! For SET/SetQ/Add/AddQ/Replace/ReplaceQ commands with values >= `STREAMING_THRESHOLD`,
//! the parser returns `NeedValue` after parsing the header and extras, allowing the
//! caller to receive the value directly into a target buffer.
//!
//! For all other commands (including small SETs), it behaves identically to
//! `BinaryCommand::parse()`.
//!
//! # Example
//!
//! ```ignore
//! use protocol_memcache::binary::{parse_streaming, BinaryParseProgress, BINARY_STREAMING_THRESHOLD};
//!
//! match parse_streaming(data, BINARY_STREAMING_THRESHOLD)? {
//!     BinaryParseProgress::NeedValue { header, value_len, value_prefix, header_consumed } => {
//!         // Allocate buffer and receive value directly
//!         let mut reservation = cache.begin_segment_set(&header.key, value_len, ttl)?;
//!         reservation.value_mut()[..value_prefix.len()].copy_from_slice(value_prefix);
//!         // ... receive remaining bytes ...
//!     }
//!     BinaryParseProgress::Complete(cmd, consumed) => {
//!         // Small SET or other command - handle normally
//!     }
//!     BinaryParseProgress::Incomplete => {
//!         // Need more data
//!     }
//! }
//! ```

use super::command::BinaryCommand;
use super::header::{HEADER_SIZE, Opcode, RequestHeader};
use crate::error::ParseError;

/// Threshold for streaming large values (64KB).
///
/// SET commands with values >= this size will use the streaming path,
/// returning `NeedValue` to allow zero-copy receive.
pub const BINARY_STREAMING_THRESHOLD: usize = 64 * 1024;

/// Parsed binary SET command header (before value data).
///
/// Contains all metadata needed to complete the SET after receiving the value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinarySetHeader<'a> {
    /// The key to set.
    pub key: &'a [u8],
    /// Client-defined flags.
    pub flags: u32,
    /// Expiration time in seconds.
    pub expiration: u32,
    /// CAS value for compare-and-swap.
    pub cas: u64,
    /// Opaque value (echoed in response).
    pub opaque: u32,
    /// The original opcode (Set, SetQ, Add, AddQ, Replace, ReplaceQ, Append, Prepend).
    pub opcode: Opcode,
}

/// Result of incremental parsing for binary protocol.
#[derive(Debug)]
pub enum BinaryParseProgress<'a> {
    /// Need more data to continue parsing.
    Incomplete,

    /// SET header parsed, waiting for value data.
    ///
    /// The caller should:
    /// 1. Allocate a buffer for the value (e.g., in cache segment)
    /// 2. Copy `value_prefix` to the start of that buffer
    /// 3. Receive remaining `value_len - value_prefix.len()` bytes into the buffer
    /// 4. Consume `total_consumed` bytes from the input buffer when complete
    NeedValue {
        /// Parsed command header with metadata.
        header: BinarySetHeader<'a>,
        /// Total size of the value in bytes.
        value_len: usize,
        /// Bytes of value already in the parse buffer (may be empty).
        /// These must be copied to the target buffer before receiving more.
        value_prefix: &'a [u8],
        /// Total bytes that will be consumed when value is complete.
        /// This is HEADER_SIZE + total_body_length.
        total_consumed: usize,
    },

    /// Fully parsed command.
    ///
    /// The tuple contains the parsed command and the number of bytes consumed.
    Complete(BinaryCommand<'a>, usize),
}

/// Parse a memcache binary command with streaming support for large values.
///
/// For SET/SetQ/Add/AddQ/Replace/ReplaceQ commands with values >= `streaming_threshold`,
/// this returns `NeedValue` after parsing the header, allowing the caller to receive
/// the value directly into a target buffer.
///
/// For all other commands (including small SETs), it behaves identically to
/// `BinaryCommand::parse()`.
///
/// # Arguments
///
/// * `buffer` - The input buffer containing memcache binary protocol data
/// * `streaming_threshold` - Minimum value size for streaming (use `BINARY_STREAMING_THRESHOLD`)
///
/// # Returns
///
/// * `Ok(BinaryParseProgress::Incomplete)` - Need more data
/// * `Ok(BinaryParseProgress::NeedValue { .. })` - SET header parsed, value pending
/// * `Ok(BinaryParseProgress::Complete(cmd, consumed))` - Fully parsed command
/// * `Err(ParseError)` - Parse error
pub fn parse_streaming(
    buffer: &[u8],
    streaming_threshold: usize,
) -> Result<BinaryParseProgress<'_>, ParseError> {
    // Need at least the header
    if buffer.len() < HEADER_SIZE {
        return Ok(BinaryParseProgress::Incomplete);
    }

    // Parse the request header
    let header = RequestHeader::parse(buffer)?;

    let total_len = HEADER_SIZE + header.total_body_length as usize;

    // Check if this is a storage command with a value
    let is_storage_command = matches!(
        header.opcode,
        Opcode::Set
            | Opcode::SetQ
            | Opcode::Add
            | Opcode::AddQ
            | Opcode::Replace
            | Opcode::ReplaceQ
            | Opcode::Append
            | Opcode::AppendQ
            | Opcode::Prepend
            | Opcode::PrependQ
    );

    if !is_storage_command {
        // For non-storage commands, use standard parser
        if buffer.len() < total_len {
            return Ok(BinaryParseProgress::Incomplete);
        }
        return match BinaryCommand::parse(buffer) {
            Ok((cmd, consumed)) => Ok(BinaryParseProgress::Complete(cmd, consumed)),
            Err(ParseError::Incomplete) => Ok(BinaryParseProgress::Incomplete),
            Err(e) => Err(e),
        };
    }

    // For storage commands, we need extras and key to determine if streaming
    let extras_len = header.extras_length as usize;
    let key_len = header.key_length as usize;

    // Validate header lengths
    if extras_len + key_len > header.total_body_length as usize {
        return Err(ParseError::Protocol("header lengths exceed body length"));
    }

    // Calculate value length
    let value_len = header.total_body_length as usize - extras_len - key_len;

    // Check if we should use streaming
    if value_len < streaming_threshold {
        // Small value - use standard parser
        if buffer.len() < total_len {
            return Ok(BinaryParseProgress::Incomplete);
        }
        return match BinaryCommand::parse(buffer) {
            Ok((cmd, consumed)) => Ok(BinaryParseProgress::Complete(cmd, consumed)),
            Err(ParseError::Incomplete) => Ok(BinaryParseProgress::Incomplete),
            Err(e) => Err(e),
        };
    }

    // Large value - use streaming path
    // We need at least the header, extras, and key
    let header_and_key_len = HEADER_SIZE + extras_len + key_len;
    if buffer.len() < header_and_key_len {
        return Ok(BinaryParseProgress::Incomplete);
    }

    // Parse extras (flags and expiration for SET/Add/Replace)
    let body = &buffer[HEADER_SIZE..];
    let extras = &body[..extras_len];

    let (flags, expiration) = if matches!(
        header.opcode,
        Opcode::Set
            | Opcode::SetQ
            | Opcode::Add
            | Opcode::AddQ
            | Opcode::Replace
            | Opcode::ReplaceQ
    ) {
        if extras.len() < 8 {
            return Err(ParseError::Protocol(
                "storage command requires 8 bytes of extras",
            ));
        }
        let flags = u32::from_be_bytes([extras[0], extras[1], extras[2], extras[3]]);
        let expiration = u32::from_be_bytes([extras[4], extras[5], extras[6], extras[7]]);
        (flags, expiration)
    } else {
        // Append/Prepend don't have extras
        (0, 0)
    };

    // Extract key
    let key_start = extras_len;
    let key_end = key_start + key_len;
    let key = &body[key_start..key_end];

    // Calculate how much of the value is already in the buffer
    let value_start = header_and_key_len;
    let available = buffer.len().saturating_sub(value_start);
    let prefix_len = std::cmp::min(available, value_len);
    let value_prefix = &buffer[value_start..value_start + prefix_len];

    Ok(BinaryParseProgress::NeedValue {
        header: BinarySetHeader {
            key,
            flags,
            expiration,
            cas: header.cas,
            opaque: header.opaque,
            opcode: header.opcode,
        },
        value_len,
        value_prefix,
        total_consumed: total_len,
    })
}

/// Complete a binary SET command after receiving the full value.
///
/// This is a helper for constructing the final BinaryCommand after streaming receive.
/// The caller is responsible for ensuring the value data is correct.
///
/// # Arguments
///
/// * `header` - The parsed SET header from `NeedValue`
/// * `value` - The complete value data (must match the expected length)
///
/// # Returns
///
/// A `BinaryCommand` appropriate for the opcode with the provided value.
pub fn complete_set<'a>(header: &BinarySetHeader<'_>, value: &'a [u8]) -> BinaryCommand<'a> {
    // Safety: The key reference is valid for the lifetime of the original buffer.
    // We transmute the lifetime to match the value's lifetime since the caller
    // is responsible for ensuring both are valid.
    let key: &'a [u8] = unsafe { std::mem::transmute::<&[u8], &'a [u8]>(header.key) };

    match header.opcode {
        Opcode::Set => BinaryCommand::Set {
            key,
            value,
            flags: header.flags,
            expiration: header.expiration,
            cas: header.cas,
            opaque: header.opaque,
        },
        Opcode::SetQ => BinaryCommand::SetQ {
            key,
            value,
            flags: header.flags,
            expiration: header.expiration,
            cas: header.cas,
            opaque: header.opaque,
        },
        Opcode::Add | Opcode::AddQ => BinaryCommand::Add {
            key,
            value,
            flags: header.flags,
            expiration: header.expiration,
            opaque: header.opaque,
        },
        Opcode::Replace | Opcode::ReplaceQ => BinaryCommand::Replace {
            key,
            value,
            flags: header.flags,
            expiration: header.expiration,
            cas: header.cas,
            opaque: header.opaque,
        },
        Opcode::Append | Opcode::AppendQ => BinaryCommand::Append {
            key,
            value,
            cas: header.cas,
            opaque: header.opaque,
        },
        Opcode::Prepend | Opcode::PrependQ => BinaryCommand::Prepend {
            key,
            value,
            cas: header.cas,
            opaque: header.opaque,
        },
        // Should not happen if used correctly, but provide a fallback
        _ => BinaryCommand::Set {
            key,
            value,
            flags: header.flags,
            expiration: header.expiration,
            cas: header.cas,
            opaque: header.opaque,
        },
    }
}

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

    fn make_set_request(key: &[u8], value: &[u8], flags: u32, expiration: u32) -> Vec<u8> {
        let extras = [
            (flags >> 24) as u8,
            (flags >> 16) as u8,
            (flags >> 8) as u8,
            flags as u8,
            (expiration >> 24) as u8,
            (expiration >> 16) as u8,
            (expiration >> 8) as u8,
            expiration as u8,
        ];
        let body_len = extras.len() + key.len() + value.len();

        let mut buf = vec![0u8; HEADER_SIZE + body_len];
        let mut header = RequestHeader::new(Opcode::Set);
        header.key_length = key.len() as u16;
        header.extras_length = extras.len() as u8;
        header.total_body_length = body_len as u32;
        header.encode(&mut buf);

        let body_start = HEADER_SIZE;
        buf[body_start..body_start + extras.len()].copy_from_slice(&extras);
        buf[body_start + extras.len()..body_start + extras.len() + key.len()].copy_from_slice(key);
        buf[body_start + extras.len() + key.len()..].copy_from_slice(value);
        buf
    }

    fn make_get_request(key: &[u8]) -> Vec<u8> {
        let mut buf = vec![0u8; HEADER_SIZE + key.len()];
        let mut header = RequestHeader::new(Opcode::Get);
        header.key_length = key.len() as u16;
        header.total_body_length = key.len() as u32;
        header.encode(&mut buf);

        buf[HEADER_SIZE..].copy_from_slice(key);
        buf
    }

    #[test]
    fn test_small_set_complete() {
        let data = make_set_request(b"mykey", b"myvalue", 0, 3600);
        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::Complete(cmd, consumed) => {
                if let BinaryCommand::Set {
                    key,
                    value,
                    flags,
                    expiration,
                    ..
                } = cmd
                {
                    assert_eq!(key, b"mykey");
                    assert_eq!(value, b"myvalue");
                    assert_eq!(flags, 0);
                    assert_eq!(expiration, 3600);
                } else {
                    panic!("Expected Set command");
                }
                assert_eq!(consumed, data.len());
            }
            _ => panic!("expected Complete"),
        }
    }

    #[test]
    fn test_large_set_needs_value() {
        let value_len = 100 * 1024; // 100KB
        let key = b"mykey";
        let flags = 42u32;
        let expiration = 3600u32;

        // Create header + extras + key, but only partial value
        let extras = [
            (flags >> 24) as u8,
            (flags >> 16) as u8,
            (flags >> 8) as u8,
            flags as u8,
            (expiration >> 24) as u8,
            (expiration >> 16) as u8,
            (expiration >> 8) as u8,
            expiration as u8,
        ];
        let body_len = extras.len() + key.len() + value_len;

        let mut data = vec![0u8; HEADER_SIZE + extras.len() + key.len() + 1000]; // Only 1000 bytes of value
        let mut header = RequestHeader::new(Opcode::Set);
        header.key_length = key.len() as u16;
        header.extras_length = extras.len() as u8;
        header.total_body_length = body_len as u32;
        header.opaque = 123;
        header.cas = 456;
        header.encode(&mut data);

        let body_start = HEADER_SIZE;
        data[body_start..body_start + extras.len()].copy_from_slice(&extras);
        data[body_start + extras.len()..body_start + extras.len() + key.len()].copy_from_slice(key);
        // Fill in partial value
        for i in 0..1000 {
            data[body_start + extras.len() + key.len() + i] = b'x';
        }

        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::NeedValue {
                header: set_header,
                value_len: vl,
                value_prefix,
                total_consumed,
            } => {
                assert_eq!(set_header.key, b"mykey");
                assert_eq!(set_header.flags, 42);
                assert_eq!(set_header.expiration, 3600);
                assert_eq!(set_header.opaque, 123);
                assert_eq!(set_header.cas, 456);
                assert_eq!(set_header.opcode, Opcode::Set);
                assert_eq!(vl, 100 * 1024);
                assert_eq!(value_prefix.len(), 1000);
                assert!(value_prefix.iter().all(|&b| b == b'x'));
                assert_eq!(total_consumed, HEADER_SIZE + body_len);
            }
            _ => panic!("expected NeedValue, got {:?}", result),
        }
    }

    #[test]
    fn test_get_uses_normal_path() {
        let data = make_get_request(b"mykey");
        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::Complete(cmd, consumed) => {
                assert!(matches!(cmd, BinaryCommand::Get { key: b"mykey", .. }));
                assert_eq!(consumed, data.len());
            }
            _ => panic!("expected Complete"),
        }
    }

    #[test]
    fn test_incomplete_header() {
        let data = [0x80, 0x01]; // Only 2 bytes
        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::Incomplete => {}
            _ => panic!("expected Incomplete"),
        }
    }

    #[test]
    fn test_incomplete_small_value() {
        // Create a small SET but don't include all the data
        let key = b"mykey";
        let value_len = 100; // Small value
        let extras = [0u8; 8];
        let body_len = extras.len() + key.len() + value_len;

        let mut data = vec![0u8; HEADER_SIZE + extras.len() + key.len() + 10]; // Only 10 bytes of value
        let mut header = RequestHeader::new(Opcode::Set);
        header.key_length = key.len() as u16;
        header.extras_length = extras.len() as u8;
        header.total_body_length = body_len as u32;
        header.encode(&mut data);

        data[HEADER_SIZE..HEADER_SIZE + extras.len()].copy_from_slice(&extras);
        data[HEADER_SIZE + extras.len()..HEADER_SIZE + extras.len() + key.len()]
            .copy_from_slice(key);

        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::Incomplete => {}
            _ => panic!("expected Incomplete"),
        }
    }

    #[test]
    fn test_threshold_boundary() {
        // At threshold - should use streaming
        let value_len = BINARY_STREAMING_THRESHOLD;
        let key = b"mykey";
        let extras = [0u8; 8];
        let body_len = extras.len() + key.len() + value_len;

        let mut data = vec![0u8; HEADER_SIZE + extras.len() + key.len()]; // No value yet
        let mut header = RequestHeader::new(Opcode::Set);
        header.key_length = key.len() as u16;
        header.extras_length = extras.len() as u8;
        header.total_body_length = body_len as u32;
        header.encode(&mut data);

        data[HEADER_SIZE..HEADER_SIZE + extras.len()].copy_from_slice(&extras);
        data[HEADER_SIZE + extras.len()..].copy_from_slice(key);

        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::NeedValue { value_len: vl, .. } => {
                assert_eq!(vl, BINARY_STREAMING_THRESHOLD);
            }
            _ => panic!("expected NeedValue at threshold"),
        }

        // Just below threshold - should use normal path (but incomplete)
        let value_len = BINARY_STREAMING_THRESHOLD - 1;
        let body_len = extras.len() + key.len() + value_len;

        let mut data = vec![0u8; HEADER_SIZE + extras.len() + key.len()]; // No value yet
        let mut header = RequestHeader::new(Opcode::Set);
        header.key_length = key.len() as u16;
        header.extras_length = extras.len() as u8;
        header.total_body_length = body_len as u32;
        header.encode(&mut data);

        data[HEADER_SIZE..HEADER_SIZE + extras.len()].copy_from_slice(&extras);
        data[HEADER_SIZE + extras.len()..].copy_from_slice(key);

        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::Incomplete => {}
            _ => panic!("expected Incomplete for sub-threshold without data"),
        }
    }

    #[test]
    fn test_setq_streaming() {
        let value_len = 100 * 1024;
        let key = b"key";
        let extras = [0u8; 8];
        let body_len = extras.len() + key.len() + value_len;

        let mut data = vec![0u8; HEADER_SIZE + extras.len() + key.len()];
        let mut header = RequestHeader::new(Opcode::SetQ);
        header.key_length = key.len() as u16;
        header.extras_length = extras.len() as u8;
        header.total_body_length = body_len as u32;
        header.encode(&mut data);

        data[HEADER_SIZE..HEADER_SIZE + extras.len()].copy_from_slice(&extras);
        data[HEADER_SIZE + extras.len()..].copy_from_slice(key);

        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::NeedValue { header, .. } => {
                assert_eq!(header.opcode, Opcode::SetQ);
            }
            _ => panic!("expected NeedValue"),
        }
    }

    #[test]
    fn test_add_streaming() {
        let value_len = 100 * 1024;
        let key = b"key";
        let extras = [0u8; 8];
        let body_len = extras.len() + key.len() + value_len;

        let mut data = vec![0u8; HEADER_SIZE + extras.len() + key.len()];
        let mut header = RequestHeader::new(Opcode::Add);
        header.key_length = key.len() as u16;
        header.extras_length = extras.len() as u8;
        header.total_body_length = body_len as u32;
        header.encode(&mut data);

        data[HEADER_SIZE..HEADER_SIZE + extras.len()].copy_from_slice(&extras);
        data[HEADER_SIZE + extras.len()..].copy_from_slice(key);

        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::NeedValue { header, .. } => {
                assert_eq!(header.opcode, Opcode::Add);
            }
            _ => panic!("expected NeedValue"),
        }
    }

    #[test]
    fn test_append_streaming() {
        // Append has no extras
        let value_len = 100 * 1024;
        let key = b"key";
        let body_len = key.len() + value_len;

        let mut data = vec![0u8; HEADER_SIZE + key.len()];
        let mut header = RequestHeader::new(Opcode::Append);
        header.key_length = key.len() as u16;
        header.extras_length = 0;
        header.total_body_length = body_len as u32;
        header.encode(&mut data);

        data[HEADER_SIZE..].copy_from_slice(key);

        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::NeedValue { header, .. } => {
                assert_eq!(header.opcode, Opcode::Append);
                assert_eq!(header.flags, 0);
                assert_eq!(header.expiration, 0);
            }
            _ => panic!("expected NeedValue"),
        }
    }

    #[test]
    fn test_complete_set_helper() {
        let header = BinarySetHeader {
            key: b"mykey",
            flags: 42,
            expiration: 3600,
            cas: 123,
            opaque: 456,
            opcode: Opcode::Set,
        };
        let value = b"myvalue";

        let cmd = complete_set(&header, value);

        match cmd {
            BinaryCommand::Set {
                key,
                value: v,
                flags,
                expiration,
                cas,
                opaque,
            } => {
                assert_eq!(key, b"mykey");
                assert_eq!(v, b"myvalue");
                assert_eq!(flags, 42);
                assert_eq!(expiration, 3600);
                assert_eq!(cas, 123);
                assert_eq!(opaque, 456);
            }
            _ => panic!("expected Set command"),
        }
    }

    #[test]
    fn test_complete_set_helper_setq() {
        let header = BinarySetHeader {
            key: b"key",
            flags: 0,
            expiration: 0,
            cas: 0,
            opaque: 0,
            opcode: Opcode::SetQ,
        };

        let cmd = complete_set(&header, b"val");
        assert!(matches!(cmd, BinaryCommand::SetQ { .. }));
    }

    #[test]
    fn test_complete_set_helper_add() {
        let header = BinarySetHeader {
            key: b"key",
            flags: 0,
            expiration: 0,
            cas: 0,
            opaque: 0,
            opcode: Opcode::Add,
        };

        let cmd = complete_set(&header, b"val");
        assert!(matches!(cmd, BinaryCommand::Add { .. }));
    }

    #[test]
    fn test_complete_set_helper_replace() {
        let header = BinarySetHeader {
            key: b"key",
            flags: 0,
            expiration: 0,
            cas: 0,
            opaque: 0,
            opcode: Opcode::Replace,
        };

        let cmd = complete_set(&header, b"val");
        assert!(matches!(cmd, BinaryCommand::Replace { .. }));
    }

    #[test]
    fn test_complete_set_helper_append() {
        let header = BinarySetHeader {
            key: b"key",
            flags: 0,
            expiration: 0,
            cas: 0,
            opaque: 0,
            opcode: Opcode::Append,
        };

        let cmd = complete_set(&header, b"val");
        assert!(matches!(cmd, BinaryCommand::Append { .. }));
    }

    #[test]
    fn test_complete_set_helper_prepend() {
        let header = BinarySetHeader {
            key: b"key",
            flags: 0,
            expiration: 0,
            cas: 0,
            opaque: 0,
            opcode: Opcode::Prepend,
        };

        let cmd = complete_set(&header, b"val");
        assert!(matches!(cmd, BinaryCommand::Prepend { .. }));
    }

    #[test]
    fn test_noop_command() {
        let mut data = vec![0u8; HEADER_SIZE];
        let header = RequestHeader::new(Opcode::Noop);
        header.encode(&mut data);

        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::Complete(cmd, _) => {
                assert!(matches!(cmd, BinaryCommand::Noop { .. }));
            }
            _ => panic!("expected Complete"),
        }
    }

    #[test]
    fn test_delete_command() {
        let key = b"mykey";
        let mut data = vec![0u8; HEADER_SIZE + key.len()];
        let mut header = RequestHeader::new(Opcode::Delete);
        header.key_length = key.len() as u16;
        header.total_body_length = key.len() as u32;
        header.encode(&mut data);

        data[HEADER_SIZE..].copy_from_slice(key);

        let result = parse_streaming(&data, BINARY_STREAMING_THRESHOLD).unwrap();

        match result {
            BinaryParseProgress::Complete(cmd, consumed) => {
                assert!(matches!(cmd, BinaryCommand::Delete { key: b"mykey", .. }));
                assert_eq!(consumed, data.len());
            }
            _ => panic!("expected Complete"),
        }
    }

    #[test]
    fn test_header_traits() {
        let header = BinarySetHeader {
            key: b"test",
            flags: 0,
            expiration: 0,
            cas: 0,
            opaque: 0,
            opcode: Opcode::Set,
        };
        let header2 = header.clone();
        assert_eq!(header, header2);

        let debug_str = format!("{:?}", header);
        assert!(debug_str.contains("BinarySetHeader"));
    }
}