darkwing_diff/
compress.rs

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
use rkyv::Archive;
use std::io::{Read, Write};

use crate::Error;

const ZSTD_COMPRESSION_LEVEL: i32 = 21;

/// Compression algorithms available for patch data.
///
/// Determines how patch data is compressed before storage or transmission.
/// Different algorithms offer tradeoffs between compression ratio and speed.
///
/// # Example
/// ```rust
/// use darkwing_diff::{diff, DiffAlgorithm, CompressAlgorithm};
///
/// let before = b"original content";
/// let after = b"modified content";
///
/// // Use no compression for debugging or when speed is critical
/// let uncompressed = diff(
///     before,
///     after,
///     DiffAlgorithm::Rsync020,
///     CompressAlgorithm::None
/// )?;
///
/// // Use Zstd for maximum compression
/// let compressed = diff(
///     before,
///     after,
///     DiffAlgorithm::Rsync020,
///     CompressAlgorithm::Zstd
/// )?;
/// # Ok::<(), darkwing_diff::Error>(())
/// ```
#[derive(
  Archive,
  rkyv::Deserialize,
  rkyv::Serialize,
  Debug,
  PartialEq,
  Copy,
  Clone,
  Eq,
  Hash,
)]
#[rkyv(derive(Debug, PartialEq, Copy, Clone))]
pub enum CompressAlgorithm {
  /// No compression. Patch data is stored as-is.
  /// Use this when:
  /// - Debugging patches
  /// - Working with already compressed data
  /// - Speed is more important than size
  None,

  /// Zstandard compression with level 21 (maximum compression).
  /// Use this when:
  /// - Minimizing patch size is critical
  /// - Network bandwidth or storage is limited
  /// - Compression time is not a concern
  Zstd,
}

impl std::fmt::Display for CompressAlgorithm {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(f, "{:?}", self)
  }
}

impl CompressAlgorithm {
  /// Compresses the input data using the selected algorithm.
  pub fn compress(self, input: &[u8]) -> Result<Vec<u8>, Error> {
    match self {
      Self::None => Ok(input.to_vec()),
      Self::Zstd => {
        let mut encoder =
          zstd::Encoder::new(Vec::new(), ZSTD_COMPRESSION_LEVEL).map_err(
            |e| {
              Error::ZipError(format!("failed to create zstd encoder: {}", e))
            },
          )?;
        encoder
          .write_all(input)
          .map_err(|e| Error::ZipError(format!("failed to write: {}", e)))?;
        Ok(
          encoder
            .finish()
            .map_err(|e| Error::ZipError(format!("failed to finish: {}", e)))?,
        )
      }
    }
  }

  /// Decompresses the input data using the selected algorithm.
  pub(crate) fn decompress(self, input: &[u8]) -> Result<Vec<u8>, Error> {
    match self {
      Self::None => Ok(input.to_vec()),
      Self::Zstd => {
        let mut output = Vec::new();
        let mut decoder = zstd::Decoder::new(input).map_err(|e| {
          Error::ZipError(format!("failed to create zstd decoder: {}", e))
        })?;
        decoder
          .read_to_end(&mut output)
          .map_err(|e| Error::ZipError(format!("failed to read: {}", e)))?;
        Ok(output)
      }
    }
  }
}