From 86361fe7bd87ed088207ed488c93a98f09b8dc6c Mon Sep 17 00:00:00 2001 From: Brian McKeon <135748266+brianjosephmckeon@users.noreply.github.com> Date: Tue, 16 Jan 2024 19:29:55 -0500 Subject: [PATCH] Added eq and show instances. * Added Eq and Show to data types. * Code cleanup. * Bumped version number to 0.3.2.0 and updated changelog. --- CHANGELOG.md | 4 ++++ http-interchange.cabal | 2 +- src/Http/Bodied.hs | 2 +- src/Http/Header.hs | 4 +--- src/Http/Headers.hs | 3 ++- src/Http/Request.hs | 10 +++------- src/Http/Response.hs | 10 ++++------ test/Main.hs | 1 + 8 files changed, 17 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 687b27d..81bae86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Revision history for http-interchange +## 0.3.2.0 -- 2024-01-16 + +* Add `Eq` and `Show` to all data types. + ## 0.3.1.0 -- 2023-08-16 * Add these to `Http.Headers`: cons, snoc, lookupHost, lookupAccept, diff --git a/http-interchange.cabal b/http-interchange.cabal index 57db983..c29cb09 100644 --- a/http-interchange.cabal +++ b/http-interchange.cabal @@ -1,6 +1,6 @@ cabal-version: 3.0 name: http-interchange -version: 0.3.1.0 +version: 0.3.2.0 license: BSD-3-Clause license-file: LICENSE author: Andrew Martin diff --git a/src/Http/Bodied.hs b/src/Http/Bodied.hs index d057e95..4fa2551 100644 --- a/src/Http/Bodied.hs +++ b/src/Http/Bodied.hs @@ -11,4 +11,4 @@ data Bodied a = Bodied , body :: !Chunks -- ^ The body. } - deriving (Show, Eq) + deriving (Eq, Show) diff --git a/src/Http/Header.hs b/src/Http/Header.hs index 2939d6b..d7b4d31 100644 --- a/src/Http/Header.hs +++ b/src/Http/Header.hs @@ -9,14 +9,12 @@ module Http.Header , builderSmallArray ) where -import Control.Monad (when) import Data.Bytes (Bytes) import Data.Bytes.Builder (Builder) import Data.Bytes.Parser (Parser) import Data.Bytes.Types (Bytes (Bytes)) import Data.Primitive (ByteArray (ByteArray), SmallArray, SmallMutableArray) import Data.Text (Text) -import Data.Word (Word16, Word8) import Data.Bytes qualified as Bytes import Data.Bytes.Builder qualified as Builder @@ -35,7 +33,7 @@ data Header = Header { name :: {-# UNPACK #-} !Text , value :: {-# UNPACK #-} !Text } - deriving (Show) + deriving (Eq, Show) uninitializedHeader :: Header {-# NOINLINE uninitializedHeader #-} diff --git a/src/Http/Headers.hs b/src/Http/Headers.hs index d6820ed..ba39e58 100644 --- a/src/Http/Headers.hs +++ b/src/Http/Headers.hs @@ -67,7 +67,7 @@ This preserves the original order of the headers and the original case of the header names. -} newtype Headers = Headers (SmallArray Header) - deriving newtype (Show, Semigroup, Monoid) + deriving newtype (Eq, Show, Semigroup, Monoid) {- | Many headers cannot appear more than once. This is part of the return type for 'lookup', and it helps us track whether the @@ -78,6 +78,7 @@ lookup failure was the result of something that might be expected data LookupException = Duplicate | Missing + deriving (Eq, Show) {- | Convert array of headers to a 'Headers' collection that supports efficient lookup. diff --git a/src/Http/Request.hs b/src/Http/Request.hs index 4a5488f..b030474 100644 --- a/src/Http/Request.hs +++ b/src/Http/Request.hs @@ -1,5 +1,4 @@ {-# LANGUAGE DuplicateRecordFields #-} -{-# LANGUAGE LambdaCase #-} {-# LANGUAGE MagicHash #-} module Http.Request @@ -17,12 +16,9 @@ module Http.Request import Data.Bytes.Builder (Builder) import Data.Bytes.Chunks (Chunks) -import Data.Primitive (SmallArray) import Data.Text (Text) -import Data.Word (Word8) import GHC.Exts (Ptr (Ptr)) import Http.Bodied (Bodied (..)) -import Http.Header (Header) import Http.Headers (Headers) import Data.Bytes.Builder qualified as Builder @@ -36,14 +32,14 @@ data Request = Request { requestLine :: !RequestLine , headers :: !Headers } - deriving (Show) + deriving (Eq, Show) -- | An HTTP request line data RequestLine = RequestLine { method :: {-# UNPACK #-} !Text , path :: {-# UNPACK #-} !Text } - deriving (Show) + deriving (Eq, Show) builderRequestLine :: RequestLine -> Builder builderRequestLine RequestLine {method, path} = @@ -57,7 +53,7 @@ toChunks :: Request -> Chunks toChunks = Builder.run 256 . builder toChunksOnto :: Request -> Chunks -> Chunks -toChunksOnto r ch = Builder.runOnto 256 (builder r) ch +toChunksOnto r = Builder.runOnto 256 (builder r) builder :: Request -> Builder builder Request {requestLine, headers} = diff --git a/src/Http/Response.hs b/src/Http/Response.hs index 7f6b2cc..b7ae92d 100644 --- a/src/Http/Response.hs +++ b/src/Http/Response.hs @@ -1,5 +1,4 @@ {-# LANGUAGE DuplicateRecordFields #-} -{-# LANGUAGE LambdaCase #-} module Http.Response ( Response (..) @@ -11,10 +10,9 @@ import Control.Monad (when) import Data.Bytes (Bytes) import Data.Bytes.Parser (Parser) import Data.Bytes.Types (Bytes (Bytes)) -import Data.Primitive (ByteArray (ByteArray), SmallArray) +import Data.Primitive (ByteArray (ByteArray)) import Data.Text (Text) -import Data.Word (Word16, Word8) -import Http.Header (Header) +import Data.Word (Word16) import Http.Headers (Headers) import Data.Bytes.Parser qualified as Parser @@ -29,13 +27,13 @@ data Response = Response { statusLine :: !StatusLine , headers :: !Headers } - deriving (Show) + deriving (Eq, Show) data StatusLine = StatusLine { statusCode :: !Word16 , statusReason :: {-# UNPACK #-} !Text } - deriving (Show) + deriving (Eq, Show) {- | Decode the response status line and the response headers. Fails if any extraneous input is present after the double CRLF sequence that diff --git a/test/Main.hs b/test/Main.hs index e70a1d9..1c8cd37 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,5 +1,6 @@ {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DerivingStrategies #-} +{-# OPTIONS_GHC -Wno-orphans #-} module Main ( main