| 1 | use super::frame_info::{BlockChecksum, BlockMode, BlockSize, ContentChecksum, FrameInfo}; |
| 2 | use std::os::raw::{c_int, c_uint}; |
| 3 | |
| 4 | /// Predefined compression level (0). |
| 5 | pub const CLEVEL_DEFAULT: i32 = 0; |
| 6 | |
| 7 | /// Predefined compression level (10). |
| 8 | pub const CLEVEL_HIGH: i32 = 10; |
| 9 | |
| 10 | /// Predefined compression level (12). |
| 11 | pub const CLEVEL_MAX: i32 = 12; |
| 12 | |
| 13 | /// Auto flush mode flag. |
| 14 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 15 | #[repr(C)] |
| 16 | pub enum AutoFlush { |
| 17 | Disabled, |
| 18 | Enabled, |
| 19 | } |
| 20 | |
| 21 | impl Default for AutoFlush { |
| 22 | fn default() -> Self { |
| 23 | Self::Disabled |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /// Decompression speed mode flag. |
| 28 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 29 | #[repr(C)] |
| 30 | pub enum FavorDecSpeed { |
| 31 | Disabled, |
| 32 | Enabled, |
| 33 | } |
| 34 | |
| 35 | impl Default for FavorDecSpeed { |
| 36 | fn default() -> Self { |
| 37 | Self::Disabled |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /// Compression preferences. |
| 42 | #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)] |
| 43 | #[repr(C)] |
| 44 | pub struct Preferences { |
| 45 | frame_info: FrameInfo, |
| 46 | compression_level: c_int, |
| 47 | auto_flush: AutoFlush, |
| 48 | favor_dec_speed: FavorDecSpeed, |
| 49 | _reserved: [c_uint; 3], |
| 50 | } |
| 51 | |
| 52 | impl Preferences { |
| 53 | /// Returns the frame info. |
| 54 | pub const fn frame_info(&self) -> FrameInfo { |
| 55 | self.frame_info |
| 56 | } |
| 57 | |
| 58 | /// Returns the compression level. |
| 59 | pub const fn compression_level(&self) -> i32 { |
| 60 | self.compression_level |
| 61 | } |
| 62 | |
| 63 | /// Returns the auto flush mode flag. |
| 64 | pub const fn auto_flush(&self) -> AutoFlush { |
| 65 | self.auto_flush |
| 66 | } |
| 67 | |
| 68 | /// Returns the decompression speed mode flag. |
| 69 | pub const fn favor_dec_speed(&self) -> FavorDecSpeed { |
| 70 | self.favor_dec_speed |
| 71 | } |
| 72 | |
| 73 | pub(super) fn set_block_size(&mut self, block_size: BlockSize) { |
| 74 | self.frame_info.set_block_size(block_size); |
| 75 | } |
| 76 | |
| 77 | pub(super) fn set_block_mode(&mut self, block_mode: BlockMode) { |
| 78 | self.frame_info.set_block_mode(block_mode); |
| 79 | } |
| 80 | |
| 81 | pub(super) fn set_content_checksum(&mut self, checksum: ContentChecksum) { |
| 82 | self.frame_info.set_content_checksum(checksum); |
| 83 | } |
| 84 | |
| 85 | pub(super) fn set_content_size(&mut self, size: usize) { |
| 86 | self.frame_info.set_content_size(size); |
| 87 | } |
| 88 | |
| 89 | pub(super) fn set_dict_id(&mut self, dict_id: u32) { |
| 90 | self.frame_info.set_dict_id(dict_id); |
| 91 | } |
| 92 | |
| 93 | pub(super) fn set_block_checksum(&mut self, checksum: BlockChecksum) { |
| 94 | self.frame_info.set_block_checksum(checksum); |
| 95 | } |
| 96 | |
| 97 | pub(super) fn set_compression_level(&mut self, level: i32) { |
| 98 | self.compression_level = level as c_int; |
| 99 | } |
| 100 | |
| 101 | pub(super) fn set_favor_dec_speed(&mut self, dec_speed: FavorDecSpeed) { |
| 102 | self.favor_dec_speed = dec_speed; |
| 103 | } |
| 104 | |
| 105 | pub(super) fn set_auto_flush(&mut self, auto_flush: AutoFlush) { |
| 106 | self.auto_flush = auto_flush; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /// Builds a custom `Preferences`. |
| 111 | /// |
| 112 | /// # Example |
| 113 | /// |
| 114 | /// ``` |
| 115 | /// use lzzzz::lz4f::{BlockSize, PreferencesBuilder, CLEVEL_MAX}; |
| 116 | /// |
| 117 | /// let pref = PreferencesBuilder::new() |
| 118 | /// .block_size(BlockSize::Max1MB) |
| 119 | /// .compression_level(CLEVEL_MAX) |
| 120 | /// .build(); |
| 121 | /// ``` |
| 122 | #[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)] |
| 123 | pub struct PreferencesBuilder { |
| 124 | prefs: Preferences, |
| 125 | } |
| 126 | |
| 127 | impl PreferencesBuilder { |
| 128 | /// Creates a new `PreferencesBuilder`. |
| 129 | pub fn new() -> Self { |
| 130 | Default::default() |
| 131 | } |
| 132 | |
| 133 | /// Sets the block size. |
| 134 | pub fn block_size(&mut self, block_size: BlockSize) -> &mut Self { |
| 135 | self.prefs.set_block_size(block_size); |
| 136 | self |
| 137 | } |
| 138 | |
| 139 | /// Sets the block mode. |
| 140 | pub fn block_mode(&mut self, block_mode: BlockMode) -> &mut Self { |
| 141 | self.prefs.set_block_mode(block_mode); |
| 142 | self |
| 143 | } |
| 144 | |
| 145 | /// Sets the content checksum. |
| 146 | pub fn content_checksum(&mut self, checksum: ContentChecksum) -> &mut Self { |
| 147 | self.prefs.set_content_checksum(checksum); |
| 148 | self |
| 149 | } |
| 150 | |
| 151 | /// Sets the content size. |
| 152 | /// |
| 153 | /// A value greater than 0 enables the content size field in the frame header and |
| 154 | /// automatically replaced with an actual content size. |
| 155 | pub fn content_size(&mut self, size: usize) -> &mut Self { |
| 156 | self.prefs.set_content_size(size); |
| 157 | self |
| 158 | } |
| 159 | |
| 160 | /// Sets the dictionary id. |
| 161 | pub fn dict_id(&mut self, dict_id: u32) -> &mut Self { |
| 162 | self.prefs.set_dict_id(dict_id); |
| 163 | self |
| 164 | } |
| 165 | |
| 166 | /// Sets the block checksum. |
| 167 | pub fn block_checksum(&mut self, checksum: BlockChecksum) -> &mut Self { |
| 168 | self.prefs.set_block_checksum(checksum); |
| 169 | self |
| 170 | } |
| 171 | |
| 172 | /// Sets the compression level. |
| 173 | pub fn compression_level(&mut self, level: i32) -> &mut Self { |
| 174 | self.prefs.set_compression_level(level); |
| 175 | self |
| 176 | } |
| 177 | |
| 178 | /// Sets the decompression speed mode flag. |
| 179 | pub fn favor_dec_speed(&mut self, dec_speed: FavorDecSpeed) -> &mut Self { |
| 180 | self.prefs.set_favor_dec_speed(dec_speed); |
| 181 | self |
| 182 | } |
| 183 | |
| 184 | /// Sets the auto flush mode flag. |
| 185 | pub fn auto_flush(&mut self, auto_flush: AutoFlush) -> &mut Self { |
| 186 | self.prefs.set_auto_flush(auto_flush); |
| 187 | self |
| 188 | } |
| 189 | |
| 190 | /// Builds a `Preferences` with this configuration. |
| 191 | pub const fn build(&self) -> Preferences { |
| 192 | self.prefs |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | impl From<Preferences> for PreferencesBuilder { |
| 197 | fn from(prefs: Preferences) -> Self { |
| 198 | Self { prefs } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | #[cfg(test)] |
| 203 | mod tests { |
| 204 | use crate::lz4f::{ |
| 205 | BlockChecksum, BlockMode, BlockSize, ContentChecksum, FavorDecSpeed, Preferences, |
| 206 | PreferencesBuilder, CLEVEL_DEFAULT, CLEVEL_HIGH, CLEVEL_MAX, |
| 207 | }; |
| 208 | use std::{i32, u32}; |
| 209 | |
| 210 | #[test] |
| 211 | fn preferences_builder() { |
| 212 | assert_eq!(PreferencesBuilder::new().build(), Preferences::default()); |
| 213 | assert_eq!( |
| 214 | PreferencesBuilder::new() |
| 215 | .favor_dec_speed(FavorDecSpeed::Enabled) |
| 216 | .build() |
| 217 | .favor_dec_speed, |
| 218 | FavorDecSpeed::Enabled |
| 219 | ); |
| 220 | assert_eq!( |
| 221 | PreferencesBuilder::new() |
| 222 | .block_size(BlockSize::Max64KB) |
| 223 | .build() |
| 224 | .frame_info |
| 225 | .block_size(), |
| 226 | BlockSize::Max64KB |
| 227 | ); |
| 228 | assert_eq!( |
| 229 | PreferencesBuilder::new() |
| 230 | .block_size(BlockSize::Max256KB) |
| 231 | .build() |
| 232 | .frame_info |
| 233 | .block_size(), |
| 234 | BlockSize::Max256KB |
| 235 | ); |
| 236 | assert_eq!( |
| 237 | PreferencesBuilder::new() |
| 238 | .block_size(BlockSize::Max1MB) |
| 239 | .build() |
| 240 | .frame_info |
| 241 | .block_size(), |
| 242 | BlockSize::Max1MB |
| 243 | ); |
| 244 | assert_eq!( |
| 245 | PreferencesBuilder::new() |
| 246 | .block_size(BlockSize::Max4MB) |
| 247 | .build() |
| 248 | .frame_info |
| 249 | .block_size(), |
| 250 | BlockSize::Max4MB |
| 251 | ); |
| 252 | assert_eq!( |
| 253 | PreferencesBuilder::new() |
| 254 | .content_checksum(ContentChecksum::Enabled) |
| 255 | .build() |
| 256 | .frame_info |
| 257 | .content_checksum(), |
| 258 | ContentChecksum::Enabled |
| 259 | ); |
| 260 | assert_eq!( |
| 261 | PreferencesBuilder::new() |
| 262 | .block_mode(BlockMode::Independent) |
| 263 | .build() |
| 264 | .frame_info |
| 265 | .block_mode(), |
| 266 | BlockMode::Independent |
| 267 | ); |
| 268 | assert_eq!( |
| 269 | PreferencesBuilder::new() |
| 270 | .compression_level(i32::MAX) |
| 271 | .build() |
| 272 | .compression_level, |
| 273 | i32::MAX |
| 274 | ); |
| 275 | assert_eq!( |
| 276 | PreferencesBuilder::new() |
| 277 | .compression_level(CLEVEL_DEFAULT) |
| 278 | .build() |
| 279 | .compression_level, |
| 280 | CLEVEL_DEFAULT |
| 281 | ); |
| 282 | assert_eq!( |
| 283 | PreferencesBuilder::new() |
| 284 | .compression_level(CLEVEL_HIGH) |
| 285 | .build() |
| 286 | .compression_level, |
| 287 | CLEVEL_HIGH |
| 288 | ); |
| 289 | assert_eq!( |
| 290 | PreferencesBuilder::new() |
| 291 | .compression_level(CLEVEL_MAX) |
| 292 | .build() |
| 293 | .compression_level, |
| 294 | CLEVEL_MAX |
| 295 | ); |
| 296 | assert_eq!( |
| 297 | PreferencesBuilder::new() |
| 298 | .compression_level(i32::MIN) |
| 299 | .build() |
| 300 | .compression_level, |
| 301 | i32::MIN |
| 302 | ); |
| 303 | assert_eq!( |
| 304 | PreferencesBuilder::new() |
| 305 | .block_checksum(BlockChecksum::Enabled) |
| 306 | .build() |
| 307 | .frame_info |
| 308 | .block_checksum(), |
| 309 | BlockChecksum::Enabled |
| 310 | ); |
| 311 | assert_eq!( |
| 312 | PreferencesBuilder::new() |
| 313 | .dict_id(u32::MAX) |
| 314 | .build() |
| 315 | .frame_info |
| 316 | .dict_id(), |
| 317 | u32::MAX |
| 318 | ); |
| 319 | assert_eq!( |
| 320 | PreferencesBuilder::new() |
| 321 | .dict_id(u32::MIN) |
| 322 | .build() |
| 323 | .frame_info |
| 324 | .dict_id(), |
| 325 | u32::MIN |
| 326 | ); |
| 327 | } |
| 328 | } |