dx-serializer 0.1.0

A token-efficient serialization format for LLM context windows with high-performance binary encoding
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
795
796
797
798
799
800
801
802
//! DX-Compress: Integrated LZ4 Streaming
//!
//! rkyv has no built-in compression.
//! DX-Compress streams LZ4 with zero-copy.
//!
//! Result: 70% smaller wire size with negligible overhead

use super::types::{DxMachineError, Result};

/// Compression level for LZ4
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CompressionLevel {
    /// Fastest compression, larger output (default)
    #[default]
    Fast,
    /// Balanced compression and speed
    Default,
    /// Maximum compression, slower
    High,
}

impl CompressionLevel {
    /// Convert to Zstd compression level (1-22)
    pub fn to_zstd_level(self) -> i32 {
        match self {
            CompressionLevel::Fast => 1,    // Fastest
            CompressionLevel::Default => 3, // Balanced (default)
            CompressionLevel::High => 19,   // Maximum compression
        }
    }
}

/// Compressed DX-Machine buffer
///
/// Wraps compressed data with lazy decompression.
/// The first access triggers decompression, subsequent accesses use cache.
#[derive(Debug)]
pub struct DxCompressed {
    /// Compressed data
    compressed: Vec<u8>,
    /// Original uncompressed size (for allocation)
    original_size: u32,
    /// Decompression cache (lazy)
    decompressed: Option<Vec<u8>>,
}

impl DxCompressed {
    /// Create empty compressed buffer
    pub fn new() -> Self {
        Self {
            compressed: Vec::new(),
            original_size: 0,
            decompressed: None,
        }
    }

    /// Compress data using LZ4 (fast, pure Rust)
    ///
    /// LZ4 provides good compression with excellent speed (pure Rust, no C dependencies).
    pub fn compress(data: &[u8]) -> Self {
        // Use LZ4 compression (pure Rust implementation)
        let compressed = lz4_compress_fast(data);
        let original_size = data.len() as u32;

        Self {
            compressed,
            original_size,
            decompressed: None,
        }
    }

    /// Compress with level hint
    pub fn compress_level(data: &[u8], _level: CompressionLevel) -> Self {
        // LZ4 doesn't have compression levels in lz4_flex
        Self::compress(data)
    }

    /// Get compressed size
    #[inline(always)]
    pub fn compressed_size(&self) -> usize {
        self.compressed.len()
    }

    /// Get original (uncompressed) size
    #[inline(always)]
    pub fn original_size(&self) -> usize {
        self.original_size as usize
    }

    /// Get compression ratio (compressed / original)
    #[inline(always)]
    pub fn ratio(&self) -> f64 {
        if self.original_size == 0 {
            return 1.0;
        }
        self.compressed.len() as f64 / self.original_size as f64
    }

    /// Get space savings (1.0 - ratio)
    #[inline(always)]
    pub fn savings(&self) -> f64 {
        1.0 - self.ratio()
    }

    /// Get compressed bytes
    #[inline(always)]
    pub fn as_compressed(&self) -> &[u8] {
        &self.compressed
    }

    /// Decompress and get data
    ///
    /// First call triggers decompression, subsequent calls use cache.
    pub fn decompress(&mut self) -> Result<&[u8]> {
        if self.decompressed.is_none() {
            let data = lz4_decompress_fast(&self.compressed)?;
            self.decompressed = Some(data);
        }

        // SAFETY: We just checked and set decompressed above
        Ok(self.decompressed.as_ref().unwrap_or_else(|| unreachable!()))
    }

    /// Force decompress and return owned data
    pub fn decompress_owned(&self) -> Result<Vec<u8>> {
        lz4_decompress_fast(&self.compressed)
    }

    /// Check if already decompressed (cached)
    #[inline(always)]
    pub fn is_cached(&self) -> bool {
        self.decompressed.is_some()
    }

    /// Clear the decompression cache
    pub fn clear_cache(&mut self) {
        self.decompressed = None;
    }

    /// Create from pre-compressed data
    pub fn from_compressed(compressed: Vec<u8>, original_size: u32) -> Self {
        Self {
            compressed,
            original_size,
            decompressed: None,
        }
    }

    /// Serialize to wire format: `[compressed_data_with_prepended_size...]`
    pub fn to_wire(&self) -> Vec<u8> {
        // LZ4 data already has size prepended by lz4_flex
        self.compressed.clone()
    }

    /// Parse from wire format
    pub fn from_wire(data: &[u8]) -> Result<Self> {
        // LZ4 data has size prepended, extract it
        if data.len() < 4 {
            return Err(DxMachineError::BufferTooSmall {
                required: 4,
                actual: data.len(),
            });
        }

        let original_size = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);
        let compressed = data.to_vec();

        Ok(Self {
            compressed,
            original_size,
            decompressed: None,
        })
    }
}

impl Default for DxCompressed {
    fn default() -> Self {
        Self::new()
    }
}

/// Simple LZ4-like compression (pure Rust)
/// LZ4 compression helper using lz4_flex (pure Rust, fast)
#[cfg(feature = "compression-lz4")]
fn lz4_compress_fast(input: &[u8]) -> Vec<u8> {
    lz4_flex::compress_prepend_size(input)
}

#[cfg(not(feature = "compression-lz4"))]
fn lz4_compress_fast(input: &[u8]) -> Vec<u8> {
    // Fallback to simple RLE compression
    lz4_compress(input)
}

/// LZ4 decompression helper using lz4_flex (pure Rust, fast)
#[cfg(feature = "compression-lz4")]
pub(crate) fn lz4_decompress_fast(input: &[u8]) -> Result<Vec<u8>> {
    lz4_flex::decompress_size_prepended(input)
        .map_err(|e| DxMachineError::DecompressionFailed(e.to_string()))
}

#[cfg(not(feature = "compression-lz4"))]
fn lz4_decompress_fast(input: &[u8]) -> Result<Vec<u8>> {
    // Fallback: assume first 4 bytes are size
    if input.len() < 4 {
        return Err(DxMachineError::DecompressionFailed(
            "Input too short".into(),
        ));
    }
    let size = u32::from_le_bytes([input[0], input[1], input[2], input[3]]) as usize;
    lz4_decompress(&input[4..], size)
}

/// Zstd compression helper
#[allow(dead_code)]
#[cfg(feature = "compression")]
fn zstd_compress(input: &[u8], level: i32) -> Vec<u8> {
    zstd::encode_all(input, level).unwrap_or_else(|_| input.to_vec())
}

#[allow(dead_code)]
#[cfg(not(feature = "compression"))]
fn zstd_compress(input: &[u8], _level: i32) -> Vec<u8> {
    // Fallback to LZ4 if zstd not enabled
    lz4_compress(input)
}

/// Zstd decompression helper
#[allow(dead_code)]
#[cfg(feature = "compression")]
fn zstd_decompress(input: &[u8]) -> Result<Vec<u8>> {
    zstd::decode_all(input).map_err(|e| DxMachineError::DecompressionFailed(e.to_string()))
}

#[allow(dead_code)]
#[cfg(not(feature = "compression"))]
fn zstd_decompress(input: &[u8]) -> Result<Vec<u8>> {
    // Fallback to LZ4 if zstd not enabled
    // Assume original size is stored in first 4 bytes
    if input.len() < 4 {
        return Err(DxMachineError::DecompressionFailed(
            "Input too short".into(),
        ));
    }
    let size = u32::from_le_bytes([input[0], input[1], input[2], input[3]]) as usize;
    lz4_decompress(&input[4..], size)
}

///
/// This is a simplified LZ4-compatible implementation.
/// For production, consider using the `lz4_flex` crate.
#[allow(dead_code)]
fn lz4_compress(input: &[u8]) -> Vec<u8> {
    if input.is_empty() {
        return Vec::new();
    }

    // Simple RLE + literal encoding for now
    // This is not full LZ4, but provides good compression for structured data
    let mut output = Vec::with_capacity(input.len());
    let mut i = 0;

    while i < input.len() {
        // Try to find a run of identical bytes
        let byte = input[i];
        let mut run_len = 1;

        while i + run_len < input.len() && input[i + run_len] == byte && run_len < 255 {
            run_len += 1;
        }

        if run_len >= 4 {
            // Encode as run: 0xFF marker + length + byte
            output.push(0xFF);
            output.push(run_len as u8);
            output.push(byte);
            i += run_len;
        } else {
            // Find literal sequence (until we hit a run or end)
            let lit_start = i;
            while i < input.len() {
                // Check for upcoming run
                if i + 4 <= input.len() {
                    let b = input[i];
                    if input[i + 1] == b && input[i + 2] == b && input[i + 3] == b {
                        break;
                    }
                }
                i += 1;
                if i - lit_start >= 254 {
                    break;
                }
            }

            let lit_len = i - lit_start;
            // Encode as literal: length (if < 0xFF) + bytes
            if lit_len > 0 {
                output.push(lit_len as u8);
                output.extend_from_slice(&input[lit_start..i]);
            }
        }
    }

    output
}

/// Simple LZ4-like decompression
#[allow(dead_code)] // Reserved for future decompression feature
fn lz4_decompress(input: &[u8], expected_size: usize) -> Result<Vec<u8>> {
    if input.is_empty() {
        return Ok(Vec::new());
    }

    let mut output = Vec::with_capacity(expected_size);
    let mut i = 0;

    while i < input.len() {
        let marker = input[i];
        i += 1;

        if marker == 0xFF {
            // Run-length encoded
            if i + 2 > input.len() {
                return Err(DxMachineError::InvalidData("Truncated RLE sequence".into()));
            }
            let run_len = input[i] as usize;
            let byte = input[i + 1];
            i += 2;

            output.extend(std::iter::repeat_n(byte, run_len));
        } else {
            // Literal sequence
            let lit_len = marker as usize;
            if i + lit_len > input.len() {
                return Err(DxMachineError::InvalidData(
                    "Truncated literal sequence".into(),
                ));
            }
            output.extend_from_slice(&input[i..i + lit_len]);
            i += lit_len;
        }
    }

    Ok(output)
}

/// Streaming compressor for large data
pub struct StreamCompressor {
    /// Chunk size for streaming
    chunk_size: usize,
    /// Accumulated chunks
    chunks: Vec<DxCompressed>,
    /// Current buffer
    buffer: Vec<u8>,
}

impl StreamCompressor {
    /// Create a new streaming compressor
    ///
    /// # Arguments
    /// * `chunk_size` - Size of each chunk (default 64KB)
    pub fn new(chunk_size: usize) -> Self {
        Self {
            chunk_size,
            chunks: Vec::new(),
            buffer: Vec::with_capacity(chunk_size),
        }
    }

    /// Default chunk size (64KB)
    pub fn default_chunk() -> Self {
        Self::new(64 * 1024)
    }

    /// Write data to the stream
    pub fn write(&mut self, data: &[u8]) {
        let mut remaining = data;

        while !remaining.is_empty() {
            let space = self.chunk_size - self.buffer.len();
            let take = remaining.len().min(space);

            self.buffer.extend_from_slice(&remaining[..take]);
            remaining = &remaining[take..];

            if self.buffer.len() >= self.chunk_size {
                self.flush_chunk();
            }
        }
    }

    /// Flush current buffer as a chunk
    fn flush_chunk(&mut self) {
        if !self.buffer.is_empty() {
            let chunk = DxCompressed::compress(&self.buffer);
            self.chunks.push(chunk);
            self.buffer.clear();
        }
    }

    /// Finish compression and get all chunks
    pub fn finish(mut self) -> Vec<DxCompressed> {
        self.flush_chunk();
        self.chunks
    }

    /// Get current number of chunks
    pub fn chunk_count(&self) -> usize {
        self.chunks.len()
    }

    /// Get total compressed size
    pub fn total_compressed_size(&self) -> usize {
        self.chunks
            .iter()
            .map(|c| c.compressed_size())
            .sum::<usize>()
            + self.buffer.len()
        // Current uncompressed buffer
    }
}

/// Streaming decompressor
pub struct StreamDecompressor {
    chunks: Vec<DxCompressed>,
    current_chunk: usize,
    current_offset: usize,
}

impl StreamDecompressor {
    /// Create from compressed chunks
    pub fn new(chunks: Vec<DxCompressed>) -> Self {
        Self {
            chunks,
            current_chunk: 0,
            current_offset: 0,
        }
    }

    /// Read decompressed data
    pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        if self.current_chunk >= self.chunks.len() {
            return Ok(0);
        }

        let mut written = 0;

        while written < buf.len() && self.current_chunk < self.chunks.len() {
            let chunk = &mut self.chunks[self.current_chunk];
            let data = chunk.decompress()?;

            let remaining_in_chunk = data.len() - self.current_offset;
            let to_copy = (buf.len() - written).min(remaining_in_chunk);

            buf[written..written + to_copy]
                .copy_from_slice(&data[self.current_offset..self.current_offset + to_copy]);

            written += to_copy;
            self.current_offset += to_copy;

            if self.current_offset >= data.len() {
                self.current_chunk += 1;
                self.current_offset = 0;
            }
        }

        Ok(written)
    }

    /// Decompress all chunks to a single buffer
    pub fn decompress_all(&mut self) -> Result<Vec<u8>> {
        let total_size: usize = self.chunks.iter().map(|c| c.original_size()).sum();
        let mut output = Vec::with_capacity(total_size);

        for chunk in &mut self.chunks {
            let data = chunk.decompress()?;
            output.extend_from_slice(data);
        }

        Ok(output)
    }
}

// ============================================================================
// LZ4 Compression
// ============================================================================

/// Compress data using LZ4 (fastest compression)
///
/// LZ4 provides excellent speed with good compression ratios.
/// Typical compression: 40-60% size reduction.
#[cfg(feature = "compression-lz4")]
pub fn compress_lz4(data: &[u8]) -> Result<Vec<u8>> {
    if data.is_empty() {
        return Ok(Vec::new());
    }

    Ok(lz4_flex::compress_prepend_size(data))
}

/// Compress data using LZ4 (stub for non-lz4 builds)
#[cfg(not(feature = "compression-lz4"))]
pub fn compress_lz4(_data: &[u8]) -> Result<Vec<u8>> {
    Err(DxMachineError::InvalidData(
        "LZ4 compression not available (enable 'compression-lz4' feature)".into(),
    ))
}

/// Decompress LZ4-compressed data
///
/// Automatically detects size and decompresses.
#[cfg(feature = "compression-lz4")]
pub fn decompress_lz4(data: &[u8]) -> Result<Vec<u8>> {
    if data.is_empty() {
        return Ok(Vec::new());
    }

    lz4_flex::decompress_size_prepended(data).map_err(|e| {
        DxMachineError::DecompressionFailed(format!("LZ4 decompression failed: {}", e))
    })
}

/// Decompress LZ4 data (stub for non-lz4 builds)
#[cfg(not(feature = "compression-lz4"))]
pub fn decompress_lz4(_data: &[u8]) -> Result<Vec<u8>> {
    Err(DxMachineError::InvalidData(
        "LZ4 decompression not available (enable 'compression-lz4' feature)".into(),
    ))
}

// ============================================================================
// Zstd Compression
// ============================================================================

/// Compress data using Zstd with default level (3)
///
/// Zstd provides better compression ratios than LZ4 at the cost of speed.
/// Typical compression: 75-85% size reduction.
#[cfg(feature = "compression")]
pub fn compress_zstd(data: &[u8]) -> Result<Vec<u8>> {
    compress_zstd_level(data, CompressionLevel::Default)
}

/// Compress data using Zstd (stub for non-zstd builds)
#[cfg(not(feature = "compression"))]
pub fn compress_zstd(_data: &[u8]) -> Result<Vec<u8>> {
    Err(DxMachineError::InvalidData(
        "Zstd compression not available (enable 'compression' feature)".into(),
    ))
}

/// Compress data using Zstd with specified compression level
///
/// # Arguments
/// * `data` - Data to compress
/// * `level` - Compression level (Fast=1, Default=3, High=19)
///
/// # Performance
/// - Level 1: ~500 MB/s compression, 60-70% reduction
/// - Level 3: ~200 MB/s compression, 70-80% reduction (default)
/// - Level 19: ~20 MB/s compression, 80-85% reduction
#[cfg(feature = "compression")]
pub fn compress_zstd_level(data: &[u8], level: CompressionLevel) -> Result<Vec<u8>> {
    if data.is_empty() {
        return Ok(Vec::new());
    }

    let zstd_level = level.to_zstd_level();
    zstd::encode_all(data, zstd_level)
        .map_err(|e| DxMachineError::InvalidData(format!("Zstd compression failed: {}", e)))
}

/// Compress data using Zstd with level (stub for non-zstd builds)
#[cfg(not(feature = "compression"))]
pub fn compress_zstd_level(_data: &[u8], _level: CompressionLevel) -> Result<Vec<u8>> {
    Err(DxMachineError::InvalidData(
        "Zstd compression not available (enable 'compression' feature)".into(),
    ))
}

/// Decompress Zstd-compressed data
///
/// Automatically detects compression level and decompresses.
/// Decompression is typically 2-3x faster than compression.
#[cfg(feature = "compression")]
pub fn decompress_zstd(data: &[u8]) -> Result<Vec<u8>> {
    if data.is_empty() {
        return Ok(Vec::new());
    }

    zstd::decode_all(data)
        .map_err(|e| DxMachineError::InvalidData(format!("Zstd decompression failed: {}", e)))
}

/// Decompress Zstd data (stub for non-zstd builds)
#[cfg(not(feature = "compression"))]
pub fn decompress_zstd(_data: &[u8]) -> Result<Vec<u8>> {
    Err(DxMachineError::InvalidData(
        "Zstd decompression not available (enable 'compression' feature)".into(),
    ))
}

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

    #[test]
    #[cfg(feature = "compression-lz4")]
    fn test_compress_decompress() {
        let original = b"Hello, World! This is a test of the compression system.";
        let mut compressed = DxCompressed::compress(original);

        // Verify compression happened
        println!(
            "Original: {} bytes, Compressed: {} bytes, Ratio: {:.2}",
            original.len(),
            compressed.compressed_size(),
            compressed.ratio()
        );

        // Decompress and verify
        let decompressed = compressed.decompress().unwrap();
        assert_eq!(decompressed, original);
    }

    #[test]
    #[cfg(feature = "compression-lz4")]
    fn test_compress_repetitive_data() {
        // Highly compressible data
        let original: Vec<u8> = std::iter::repeat_n(b'A', 1000).collect();
        let compressed = DxCompressed::compress(&original);

        println!(
            "Repetitive: {} bytes -> {} bytes ({:.1}% savings)",
            original.len(),
            compressed.compressed_size(),
            compressed.savings() * 100.0
        );

        // Should achieve significant compression
        assert!(compressed.ratio() < 0.1); // Less than 10% of original
    }

    #[test]
    #[cfg(feature = "compression-lz4")]
    fn test_compress_random_data() {
        // Less compressible data
        let original: Vec<u8> = (0..1000).map(|i| (i % 256) as u8).collect();
        let compressed = DxCompressed::compress(&original);

        println!(
            "Sequential: {} bytes -> {} bytes ({:.1}% savings)",
            original.len(),
            compressed.compressed_size(),
            compressed.savings() * 100.0
        );
    }

    #[test]
    #[cfg(feature = "compression-lz4")]
    fn test_wire_format() {
        let original = b"Test data for wire format";
        let compressed = DxCompressed::compress(original);

        let wire = compressed.to_wire();
        let restored = DxCompressed::from_wire(&wire).unwrap();

        assert_eq!(restored.original_size(), original.len());
        assert_eq!(restored.compressed_size(), compressed.compressed_size());
    }

    #[test]
    #[cfg(feature = "compression-lz4")]
    fn test_streaming_compressor() {
        let mut compressor = StreamCompressor::new(32);

        // Write data in multiple chunks
        for i in 0..10 {
            let data: Vec<u8> = (0..20).map(|j| ((i * 20 + j) % 256) as u8).collect();
            compressor.write(&data);
        }

        let chunks = compressor.finish();
        println!("Produced {} chunks", chunks.len());

        // Decompress all
        let mut decompressor = StreamDecompressor::new(chunks);
        let output = decompressor.decompress_all().unwrap();

        // Verify
        let expected: Vec<u8> = (0..200).map(|i| (i % 256) as u8).collect();
        assert_eq!(output, expected);
    }

    #[test]
    #[cfg(feature = "compression-lz4")]
    fn test_cache() {
        let original = b"Cache test data";
        let mut compressed = DxCompressed::compress(original);

        assert!(!compressed.is_cached());

        compressed.decompress().unwrap();
        assert!(compressed.is_cached());

        compressed.clear_cache();
        assert!(!compressed.is_cached());
    }

    #[test]
    #[cfg(feature = "compression-lz4")]
    fn test_empty_data() {
        let original: &[u8] = &[];
        let mut compressed = DxCompressed::compress(original);

        assert_eq!(compressed.original_size(), 0);
        let decompressed = compressed.decompress().unwrap();
        assert!(decompressed.is_empty());
    }

    #[test]
    #[cfg(feature = "compression")]
    fn test_zstd_compress_decompress() {
        let original = b"Hello, World! This is a test of Zstd compression.";
        let compressed = compress_zstd(original).unwrap();

        println!(
            "Zstd: Original {} bytes -> Compressed {} bytes ({:.1}% of original)",
            original.len(),
            compressed.len(),
            (compressed.len() as f64 / original.len() as f64) * 100.0
        );

        let decompressed = decompress_zstd(&compressed).unwrap();
        assert_eq!(decompressed, original);
    }

    #[test]
    #[cfg(feature = "compression")]
    fn test_zstd_compression_levels() {
        let original: Vec<u8> = std::iter::repeat_n(b'A', 1000).collect();

        // Test all three levels
        let fast = compress_zstd_level(&original, CompressionLevel::Fast).unwrap();
        let default = compress_zstd_level(&original, CompressionLevel::Default).unwrap();
        let high = compress_zstd_level(&original, CompressionLevel::High).unwrap();

        println!("Zstd compression levels on 1000 bytes of 'A':");
        println!("  Fast (level 1): {} bytes", fast.len());
        println!("  Default (level 3): {} bytes", default.len());
        println!("  High (level 19): {} bytes", high.len());

        // Higher compression should produce smaller or equal output
        assert!(high.len() <= default.len());
        assert!(default.len() <= fast.len());

        // All should decompress correctly
        assert_eq!(decompress_zstd(&fast).unwrap(), original);
        assert_eq!(decompress_zstd(&default).unwrap(), original);
        assert_eq!(decompress_zstd(&high).unwrap(), original);
    }

    #[test]
    #[cfg(feature = "compression")]
    fn test_zstd_empty_data() {
        let original: &[u8] = &[];
        let compressed = compress_zstd(original).unwrap();
        assert!(compressed.is_empty());

        let decompressed = decompress_zstd(&compressed).unwrap();
        assert!(decompressed.is_empty());
    }

    #[test]
    #[cfg(feature = "compression")]
    fn test_zstd_vs_lz4() {
        // Compare Zstd and LZ4 on repetitive data
        let original: Vec<u8> = std::iter::repeat_n(b'X', 10000).collect();

        let lz4_compressed = lz4_compress(&original);
        let zstd_compressed = compress_zstd(&original).unwrap();

        println!("Compression comparison on 10KB repetitive data:");
        println!(
            "  LZ4: {} bytes ({:.1}% of original)",
            lz4_compressed.len(),
            (lz4_compressed.len() as f64 / original.len() as f64) * 100.0
        );
        println!(
            "  Zstd: {} bytes ({:.1}% of original)",
            zstd_compressed.len(),
            (zstd_compressed.len() as f64 / original.len() as f64) * 100.0
        );

        // Both should decompress correctly
        let lz4_decompressed = lz4_decompress(&lz4_compressed, original.len()).unwrap();
        let zstd_decompressed = decompress_zstd(&zstd_compressed).unwrap();

        assert_eq!(lz4_decompressed, original);
        assert_eq!(zstd_decompressed, original);
    }
}