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
use std::{error, fmt};

const MIN: u8 = 0;

#[cfg(feature = "libdeflate")]
const MAX: u8 = 12;
#[cfg(not(feature = "libdeflate"))]
const MAX: u8 = 9;

/// A DEFLATE compression level.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct CompressionLevel(u8);

impl CompressionLevel {
    /// No compression.
    pub const NONE: Self = Self(0);

    /// A compression level optimized for speed.
    pub const FAST: Self = Self(1);

    /// A compression level optimized for compression rate.
    pub const BEST: Self = Self(MAX);

    /// Creates a compression level.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bgzf::writer::CompressionLevel;
    /// assert_eq!(CompressionLevel::new(0), Some(CompressionLevel::NONE));
    /// assert!(CompressionLevel::new(255).is_none());
    /// ```
    pub const fn new(n: u8) -> Option<Self> {
        if n <= MAX {
            Some(Self(n))
        } else {
            None
        }
    }

    /// Returns the inner value.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bgzf::writer::CompressionLevel;
    /// assert_eq!(CompressionLevel::NONE.get(), 0);
    /// ```
    pub const fn get(&self) -> u8 {
        self.0
    }

    /// Returns a compression level to disable compression.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bgzf::writer::CompressionLevel;
    /// let compression_level = CompressionLevel::none();
    /// ```
    #[deprecated(since = "0.29.0", note = "Use `CompressionLevel::NONE` instead.")]
    pub fn none() -> Self {
        Self(0)
    }

    /// Returns a compression level optimized for speed.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bgzf::writer::CompressionLevel;
    /// let compression_level = CompressionLevel::fast();
    /// ```
    #[deprecated(since = "0.29.0", note = "Use `CompressionLevel::FAST` instead.")]
    pub fn fast() -> Self {
        Self(1)
    }

    /// Returns a compression level optimized for compression rate.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_bgzf::writer::CompressionLevel;
    /// let compression_level = CompressionLevel::best();
    /// ```
    #[deprecated(since = "0.29.0", note = "Use `CompressionLevel::BEST` instead.")]
    pub fn best() -> Self {
        Self(MAX)
    }
}

impl Default for CompressionLevel {
    fn default() -> Self {
        Self(6)
    }
}

/// An error returned when a raw DEFLATE compression level fails to convert.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TryFromU8Error {
    Invalid(u8),
}

impl error::Error for TryFromU8Error {}

impl fmt::Display for TryFromU8Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Invalid(n) => write!(f, "invalid input: {n}"),
        }
    }
}

impl TryFrom<u8> for CompressionLevel {
    type Error = TryFromU8Error;

    fn try_from(n: u8) -> Result<Self, Self::Error> {
        match n {
            MIN..=MAX => Ok(Self(n)),
            _ => Err(TryFromU8Error::Invalid(n)),
        }
    }
}

impl From<CompressionLevel> for u8 {
    fn from(compression_level: CompressionLevel) -> Self {
        compression_level.0
    }
}

#[cfg(feature = "libdeflate")]
impl From<CompressionLevel> for libdeflater::CompressionLvl {
    fn from(compression_level: CompressionLevel) -> Self {
        // SAFETY: The raw value is guaranteed to be between MIN and MAX, inclusive.
        Self::new(i32::from(u8::from(compression_level))).unwrap()
    }
}

#[cfg(not(feature = "libdeflate"))]
impl From<CompressionLevel> for flate2::Compression {
    fn from(compression_level: CompressionLevel) -> Self {
        Self::new(u32::from(u8::from(compression_level)))
    }
}

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

    #[test]
    fn test_none() {
        assert_eq!(CompressionLevel::none(), CompressionLevel(0));
    }

    #[test]
    fn test_fast() {
        assert_eq!(CompressionLevel::fast(), CompressionLevel(1));
    }

    #[test]
    fn test_best() {
        assert_eq!(CompressionLevel::best(), CompressionLevel(MAX));
    }

    #[test]
    fn test_default() {
        assert_eq!(CompressionLevel::default(), CompressionLevel(6));
    }
}