1use core::fmt::Display;
4use std::error::Error;
5
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
12pub struct CompressionLevel(u8);
13
14impl CompressionLevel {
15 #[inline]
19 pub const fn new(level: u8) -> Option<Self> {
20 if level <= 9 {
21 Some(Self(level))
22 } else {
23 None
24 }
25 }
26
27 #[inline]
33 pub const unsafe fn new_unchecked(level: u8) -> Self {
34 Self(level)
35 }
36
37 #[inline]
39 pub const fn none() -> Self {
40 Self(0)
41 }
42
43 #[inline]
45 pub const fn fast() -> Self {
46 Self(1)
47 }
48
49 #[inline]
51 pub const fn balanced() -> Self {
52 Self(6)
53 }
54
55 #[inline]
57 pub const fn best() -> Self {
58 Self(9)
59 }
60
61 #[inline]
63 pub const fn get(self) -> u8 {
64 self.0
65 }
66}
67
68impl Default for CompressionLevel {
69 fn default() -> Self {
71 Self::balanced()
72 }
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77pub struct InvalidCompressionLevel(u32);
78
79impl InvalidCompressionLevel {
80 pub fn value(self) -> u32 {
82 self.0
83 }
84}
85
86impl Display for InvalidCompressionLevel {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88 write!(f, "Invalid compression level number: {}", self.0)
89 }
90}
91
92impl Error for InvalidCompressionLevel {}
93
94impl From<CompressionLevel> for u8 {
95 #[inline]
96 fn from(value: CompressionLevel) -> Self {
97 value.0
98 }
99}
100
101impl TryFrom<u8> for CompressionLevel {
102 type Error = InvalidCompressionLevel;
103
104 #[inline]
105 fn try_from(value: u8) -> Result<Self, Self::Error> {
106 Self::new(value).ok_or(InvalidCompressionLevel(value.into()))
107 }
108}