Skip to content

Commit

Permalink
Make CompressionLevel a newtype
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodigrim committed Feb 7, 2024
1 parent fdc9194 commit 77c7656
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Codec/Compression/GZip.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module Codec.Compression.GZip (
DecompressParams(..), defaultDecompressParams,

-- ** The compression parameter types
CompressionLevel,
CompressionLevel(..),
defaultCompression,
noCompression,
bestSpeed,
Expand Down
2 changes: 1 addition & 1 deletion Codec/Compression/Zlib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Codec.Compression.Zlib (
DecompressParams(..), defaultDecompressParams,

-- ** The compression parameter types
CompressionLevel,
CompressionLevel(..),
defaultCompression,
noCompression,
bestSpeed,
Expand Down
2 changes: 1 addition & 1 deletion Codec/Compression/Zlib/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module Codec.Compression.Zlib.Internal (
Stream.zlibFormat,
Stream.rawFormat,
Stream.gzipOrZlibFormat,
Stream.CompressionLevel,
Stream.CompressionLevel(..),
Stream.defaultCompression,
Stream.noCompression,
Stream.bestSpeed,
Expand Down
2 changes: 1 addition & 1 deletion Codec/Compression/Zlib/Raw.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Codec.Compression.Zlib.Raw (
DecompressParams(..), defaultDecompressParams,

-- ** The compression parameter types
CompressionLevel,
CompressionLevel(..),
defaultCompression,
noCompression,
bestSpeed,
Expand Down
43 changes: 22 additions & 21 deletions Codec/Compression/Zlib/Stream.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Codec.Compression.Zlib.Stream (
rawFormat,
gzipOrZlibFormat,
formatSupportsDictionary,
CompressionLevel,
CompressionLevel(..),
defaultCompression,
noCompression,
bestSpeed,
Expand Down Expand Up @@ -640,12 +640,7 @@ fromMethod Deflated = #{const Z_DEFLATED}
-- is a trade-off between the amount of compression and the time required to do
-- the compression.
--
data CompressionLevel =
DefaultCompression
| NoCompression
| BestSpeed
| BestCompression
| CompressionLevel Int
newtype CompressionLevel = CompressionLevel Int
deriving
( Eq
, Ord -- ^ @since 0.7.0.0
Expand All @@ -654,37 +649,43 @@ data CompressionLevel =
, Generic
)

-- | The default compression level is 6 (that is, biased towards higher
-- compression at expense of speed).
-- | The default t'CompressionLevel'.
defaultCompression :: CompressionLevel
defaultCompression = DefaultCompression
defaultCompression = CompressionLevel 6

-- Ideally we should use #{const Z_DEFAULT_COMPRESSION} = -1, whose meaning
-- depends on zlib version and, strictly speaking, is not guaranteed to be 6.
-- It would however interact badly with Eq / Ord instances.

-- | No compression, just a block copy.
noCompression :: CompressionLevel
noCompression = CompressionLevel 0
noCompression = CompressionLevel #{const Z_NO_COMPRESSION}

-- | The fastest compression method (less compression)
-- | The fastest compression method (less compression).
bestSpeed :: CompressionLevel
bestSpeed = CompressionLevel 1
bestSpeed = CompressionLevel #{const Z_BEST_SPEED}

-- | The slowest compression method (best compression).
bestCompression :: CompressionLevel
bestCompression = CompressionLevel 9
bestCompression = CompressionLevel #{const Z_BEST_COMPRESSION}

-- | A specific compression level between 0 and 9.
-- | A specific compression level in the range @0..9@.
-- Throws an error for arguments outside of this range.
--
-- * 0 stands for 'noCompression',
-- * 1 stands for 'bestSpeed',
-- * 6 stands for 'defaultCompression',
-- * 9 stands for 'bestCompression'.
--
compressionLevel :: Int -> CompressionLevel
compressionLevel n
| n >= 0 && n <= 9 = CompressionLevel n
| otherwise = error "CompressionLevel must be in the range 0..9"
| otherwise = error "CompressionLevel must be in the range 0..9"

fromCompressionLevel :: CompressionLevel -> CInt
fromCompressionLevel DefaultCompression = -1
fromCompressionLevel NoCompression = 0
fromCompressionLevel BestSpeed = 1
fromCompressionLevel BestCompression = 9
fromCompressionLevel (CompressionLevel n)
| n >= 0 && n <= 9 = int2cint n
| otherwise = error "CompressLevel must be in the range 1..9"
| otherwise = error "CompressLevel must be in the range 0..9"


-- | This specifies the size of the compression window. Larger values of this
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ See also http://pvp.haskell.org/faq
* Flip flag `non-blocking-ffi` to be `True` be default.
* Make `WindowBits` a newtype over `Int`.
* Make `MemoryLevel` a newtype over `Int`.
* Make `CompressionLevel` a newtype over `Int`.

0.6.3.0 Bodigrim <andrew.lelechenko@gmail.com> May 2022

Expand Down

0 comments on commit 77c7656

Please sign in to comment.