mopenzl 0.1.0

OpenZL compression and decompression in safe Rust; encoding is byte-identical to Meta's reference library
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
//! Compression configuration: what the reference calls global parameters.

use core::fmt;

use thiserror::Error;

use crate::FormatVersion;

/// A compression level (`ZL_CParam_compressionLevel`).
///
/// The default graph hands the level to its Zstandard backend unchanged, so
/// the valid range is Zstandard's, `-131072..=22`. As in the reference, `0`
/// selects the default, [`Self::DEFAULT`] (6), rather than Zstandard's own
/// default.
///
/// # Examples
///
/// ```
/// use mopenzl::Level;
///
/// let level = Level::try_from(19)?;
/// assert_eq!(i32::from(level), 19);
/// assert_eq!(Level::try_from(0)?, Level::DEFAULT);
/// assert!(Level::try_from(23).is_err());
/// # Ok::<(), mopenzl::ConfigError>(())
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Level(i32);

impl Level {
    /// The fastest level, `-131072`.
    pub const MIN: Self = Self(-131_072);
    /// The strongest level, `22`.
    pub const MAX: Self = Self(22);
    /// The reference's default level, `6` (`ZL_COMPRESSIONLEVEL_DEFAULT`).
    pub const DEFAULT: Self = Self(6);

    /// The level as the reference's integer.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(mopenzl::Level::MAX.get(), 22);
    /// ```
    #[must_use]
    pub const fn get(self) -> i32 {
        self.0
    }
}

impl Default for Level {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl TryFrom<i32> for Level {
    type Error = ConfigError;

    /// Validates a level.
    ///
    /// # Errors
    ///
    /// [`ConfigError::OutOfRange`] outside `-131072..=22`.
    fn try_from(value: i32) -> Result<Self, Self::Error> {
        if value == 0 {
            return Ok(Self::DEFAULT);
        }
        if !(Self::MIN.0..=Self::MAX.0).contains(&value) {
            return Err(ConfigError::OutOfRange {
                parameter: Parameter::Level,
                value: i64::from(value),
            });
        }
        Ok(Self(value))
    }
}

impl From<Level> for i32 {
    fn from(level: Level) -> Self {
        level.0
    }
}

/// The input size of one chunk when a frame is segmented.
///
/// The reference's serial segmenter (`ZL_Compressor_buildSerialSegmenter`)
/// accepts `32 KiB..=i32::MAX` bytes and defaults to 16 MiB.
///
/// # Examples
///
/// ```
/// use mopenzl::ChunkSize;
///
/// let size = ChunkSize::try_from(1usize << 20)?;
/// assert_eq!(usize::from(size), 1 << 20);
/// assert_eq!(usize::from(ChunkSize::DEFAULT), 16 << 20);
/// assert!(ChunkSize::try_from(4096usize).is_err());
/// # Ok::<(), mopenzl::ConfigError>(())
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ChunkSize(u32);

impl ChunkSize {
    /// The smallest chunk, 32 KiB (`ZL_MIN_CHUNK_SIZE`).
    pub const MIN: Self = Self(32 << 10);
    /// The largest chunk, `i32::MAX` bytes.
    pub const MAX: Self = Self(i32::MAX as u32);
    /// The reference's default, 16 MiB (`ZL_DEFAULT_SEGMENTER_CHUNK_BYTE_SIZE`).
    pub const DEFAULT: Self = Self(16 << 20);

    /// The size in bytes.
    pub(crate) const fn bytes(self) -> usize {
        self.0 as usize
    }
}

impl Default for ChunkSize {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl TryFrom<usize> for ChunkSize {
    type Error = ConfigError;

    /// Validates a chunk size.
    ///
    /// # Errors
    ///
    /// [`ConfigError::OutOfRange`] outside `32768..=2147483647`.
    fn try_from(value: usize) -> Result<Self, Self::Error> {
        if value < Self::MIN.bytes() || value > Self::MAX.bytes() {
            return Err(ConfigError::OutOfRange {
                parameter: Parameter::ChunkSize,
                value: i64::try_from(value).unwrap_or(i64::MAX),
            });
        }
        Ok(Self(value as u32))
    }
}

impl From<ChunkSize> for usize {
    fn from(size: ChunkSize) -> Self {
        size.bytes()
    }
}

/// How a frame's input is cut into independently compressed chunks.
///
/// [`Self::Whole`] is what `ZL_CCtx_compress` does with its default graph:
/// the whole input is one chunk, so a session buffers everything until the
/// frame ends. [`Self::Fixed`] is the serial segmenter of the reference CLI's
/// `serial` profile: a session holds at most one chunk of input, and decoders
/// can deliver output chunk by chunk. Both are byte-identical to the
/// reference configured the same way.
///
/// Whole inputs above 2 GiB are still cut every 2 GiB, because the format
/// caps a stored stream below 4 GiB.
///
/// # Examples
///
/// ```
/// use mopenzl::{ChunkSize, Chunking, EncoderConfig};
///
/// assert_eq!(Chunking::default(), Chunking::Whole);
/// let config = EncoderConfig::default().with_chunking(Chunking::Fixed(ChunkSize::DEFAULT));
/// assert_eq!(config.chunking(), Chunking::Fixed(ChunkSize::DEFAULT));
/// ```
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
pub enum Chunking {
    /// The whole input is one chunk.
    #[default]
    Whole,
    /// Chunks of this many input bytes; the last one may be shorter.
    Fixed(ChunkSize),
}

impl From<ChunkSize> for Chunking {
    fn from(size: ChunkSize) -> Self {
        Self::Fixed(size)
    }
}

/// The largest chunk [`Chunking::Whole`] writes.
pub(crate) const WHOLE_CHUNK_LIMIT: usize = 1 << 31;

impl Chunking {
    /// The most input bytes one chunk takes.
    pub(crate) const fn limit(self) -> usize {
        match self {
            Self::Whole => WHOLE_CHUNK_LIMIT,
            Self::Fixed(size) => size.bytes(),
        }
    }
}

/// Everything that shapes the bytes of a frame.
///
/// The defaults are the reference's: level 6, the newest format version,
/// content and compressed checksums on, and the whole input as one chunk.
///
/// # Examples
///
/// ```
/// use mopenzl::{EncoderConfig, FormatVersion, Level};
///
/// let config = EncoderConfig::default()
///     .with_level(Level::try_from(3)?)
///     .with_format_version(FormatVersion::try_from(25)?)
///     .with_content_checksum(false);
/// assert_eq!(config.level().get(), 3);
/// assert_eq!(u32::from(config.format_version()), 25);
/// assert!(!config.content_checksum());
/// assert!(config.compressed_checksum());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct EncoderConfig {
    level: Level,
    format_version: FormatVersion,
    chunking: Chunking,
    content_checksum: bool,
    compressed_checksum: bool,
}

impl EncoderConfig {
    /// The reference's defaults.
    pub const DEFAULT: Self = Self {
        level: Level::DEFAULT,
        format_version: FormatVersion::MAX,
        chunking: Chunking::Whole,
        content_checksum: true,
        compressed_checksum: true,
    };

    /// Sets the compression level.
    #[must_use]
    pub const fn with_level(mut self, level: Level) -> Self {
        self.level = level;
        self
    }

    /// Sets the format version frames are written in.
    #[must_use]
    pub const fn with_format_version(mut self, version: FormatVersion) -> Self {
        self.format_version = version;
        self
    }

    /// Sets how the input is cut into chunks.
    #[must_use]
    pub const fn with_chunking(mut self, chunking: Chunking) -> Self {
        self.chunking = chunking;
        self
    }

    /// Whether every chunk records a checksum of its content
    /// (`ZL_CParam_contentChecksum`).
    #[must_use]
    pub const fn with_content_checksum(mut self, enabled: bool) -> Self {
        self.content_checksum = enabled;
        self
    }

    /// Whether every chunk records a checksum of its compressed bytes, and the
    /// frame header a check byte (`ZL_CParam_compressedChecksum`).
    #[must_use]
    pub const fn with_compressed_checksum(mut self, enabled: bool) -> Self {
        self.compressed_checksum = enabled;
        self
    }

    /// The compression level.
    #[must_use]
    pub const fn level(&self) -> Level {
        self.level
    }

    /// The format version.
    #[must_use]
    pub const fn format_version(&self) -> FormatVersion {
        self.format_version
    }

    /// How the input is cut into chunks.
    #[must_use]
    pub const fn chunking(&self) -> Chunking {
        self.chunking
    }

    /// Whether chunks carry content checksums.
    #[must_use]
    pub const fn content_checksum(&self) -> bool {
        self.content_checksum
    }

    /// Whether chunks carry compressed checksums.
    #[must_use]
    pub const fn compressed_checksum(&self) -> bool {
        self.compressed_checksum
    }
}

impl Default for EncoderConfig {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl From<Level> for EncoderConfig {
    fn from(level: Level) -> Self {
        Self::DEFAULT.with_level(level)
    }
}

/// The configuration value a [`ConfigError`] is about.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum Parameter {
    /// [`Level`].
    Level,
    /// [`ChunkSize`].
    ChunkSize,
}

impl fmt::Display for Parameter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Level => "compression level",
            Self::ChunkSize => "chunk size",
        })
    }
}

/// A configuration value outside its valid range.
///
/// # Examples
///
/// ```
/// use mopenzl::{ConfigError, Level, Parameter};
///
/// assert_eq!(
///     Level::try_from(99),
///     Err(ConfigError::OutOfRange { parameter: Parameter::Level, value: 99 })
/// );
/// ```
#[derive(Error, Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub enum ConfigError {
    /// The value lies outside the parameter's range.
    #[error("{parameter} {value} is out of range")]
    OutOfRange {
        /// Which parameter.
        parameter: Parameter,
        /// The rejected value.
        value: i64,
    },
}

/// A compressed-size bound that does not fit in `usize`.
///
/// # Examples
///
/// ```
/// use mopenzl::{Compressor, SizeOverflow};
///
/// assert_eq!(Compressor::max_compressed_size(usize::MAX), Err(SizeOverflow));
/// ```
#[derive(Error, Debug, Clone, Copy, Eq, PartialEq)]
#[error("the compressed-size bound overflows usize")]
pub struct SizeOverflow;

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

    #[test]
    fn levels_follow_the_reference_range_and_zero_default() {
        assert_eq!(Level::try_from(0), Ok(Level::DEFAULT));
        assert_eq!(Level::try_from(-131_072), Ok(Level::MIN));
        assert_eq!(Level::try_from(22), Ok(Level::MAX));
        assert!(Level::try_from(-131_073).is_err());
        assert_eq!(Level::default().get(), 6);
        assert_eq!(i32::from(Level::try_from(-5).unwrap()), -5);
    }

    #[test]
    fn chunk_sizes_follow_the_segmenter_range() {
        assert_eq!(ChunkSize::try_from(32_768), Ok(ChunkSize::MIN));
        assert_eq!(ChunkSize::try_from(i32::MAX as usize), Ok(ChunkSize::MAX));
        assert_eq!(
            ChunkSize::try_from(32_767),
            Err(ConfigError::OutOfRange {
                parameter: Parameter::ChunkSize,
                value: 32_767,
            })
        );
        assert!(ChunkSize::try_from(1usize << 31).is_err());
        assert_eq!(ChunkSize::default(), ChunkSize::DEFAULT);
        assert_eq!(
            Chunking::from(ChunkSize::MIN),
            Chunking::Fixed(ChunkSize::MIN)
        );
        assert_eq!(Chunking::Whole.limit(), WHOLE_CHUNK_LIMIT);
        assert_eq!(Chunking::Fixed(ChunkSize::MIN).limit(), 32_768);
    }

    #[test]
    fn config_builders_set_every_field() {
        let config = EncoderConfig::from(Level::MAX)
            .with_format_version(FormatVersion::MIN)
            .with_chunking(Chunking::Fixed(ChunkSize::MIN))
            .with_content_checksum(false)
            .with_compressed_checksum(false);
        assert_eq!(config.level(), Level::MAX);
        assert_eq!(config.format_version(), FormatVersion::MIN);
        assert_eq!(config.chunking(), Chunking::Fixed(ChunkSize::MIN));
        assert!(!config.content_checksum());
        assert!(!config.compressed_checksum());
        assert_eq!(EncoderConfig::default(), EncoderConfig::DEFAULT);
    }

    #[test]
    fn errors_name_the_parameter() {
        let error = ConfigError::OutOfRange {
            parameter: Parameter::ChunkSize,
            value: 3,
        };
        assert_eq!(error.to_string(), "chunk size 3 is out of range");
        assert_eq!(Parameter::Level.to_string(), "compression level");
        assert!(SizeOverflow.to_string().contains("overflows"));
    }
}