]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/Response.hs
Code clean-up using convertible-text
[Lucu.git] / Network / HTTP / Lucu / Response.hs
index 0ebfa71080647983d95d428fa6bda605239c1185..e9da057c4bdb07415ccc944bd6bcd902f81354d1 100644 (file)
@@ -15,10 +15,13 @@ module Network.HTTP.Lucu.Response
 
       -- * Functions
     , emptyResponse
+    , setStatusCode
     , resCanHaveBody
     , printStatusCode
     , printResponse
 
+    , (≈)
+    , (≉)
     , isInformational
     , isSuccessful
     , isRedirection
@@ -29,6 +32,9 @@ module Network.HTTP.Lucu.Response
     where
 import Data.Ascii (AsciiBuilder)
 import qualified Data.Ascii as A
+import Data.Convertible.Base
+import Data.Convertible.Instances.Ascii ()
+import Data.Convertible.Utils
 import Data.Monoid.Unicode
 import Network.HTTP.Lucu.Headers
 import Network.HTTP.Lucu.HttpVersion
@@ -61,53 +67,60 @@ emptyResponse sc
       , resHeaders = (∅)
       }
 
+-- |@'setStatusCode' sc res@ sets the status code of @res@ to @sc@.
+setStatusCode ∷ StatusCode sc ⇒ sc → Response → Response
+setStatusCode sc res
+    = res {
+        resStatus = fromStatusCode sc
+      }
+
 -- |Returns 'True' iff a given 'Response' allows the existence of
 -- response entity body.
 resCanHaveBody ∷ Response → Bool
 {-# INLINEABLE resCanHaveBody #-}
 resCanHaveBody (Response {..})
-    | isInformational resStatus                  = False
-    | toStatusCode resStatus ≡ Just NoContent    = False
-    | toStatusCode resStatus ≡ Just ResetContent = False
-    | toStatusCode resStatus ≡ Just NotModified  = False
-    | otherwise                                  = True
+    | isInformational resStatus = False
+    | resStatus ≈ NoContent     = False
+    | resStatus ≈ ResetContent  = False
+    | resStatus ≈ NotModified   = False
+    | otherwise                 = True
 
 -- |Convert a 'Response' to 'AsciiBuilder'.
 printResponse ∷ Response → AsciiBuilder
 {-# INLINEABLE printResponse #-}
 printResponse (Response {..})
-    = printHttpVersion resVersion ⊕
+    = cs resVersion ⊕
       A.toAsciiBuilder " "        ⊕
       printStatusCode  resStatus  ⊕
       A.toAsciiBuilder "\x0D\x0A" ⊕
-      printHeaders     resHeaders
+      cs resHeaders
 
--- |@'isInformational' sc@ returns 'True' iff @sc < 200@.
+-- |@'isInformational' sc@ returns 'True' iff @sc '<' 200@.
 isInformational ∷ StatusCode sc ⇒ sc → Bool
 {-# INLINE isInformational #-}
 isInformational = satisfy (< 200)
 
--- |@'isSuccessful' sc@ returns 'True' iff @200 <= sc < 300@.
+-- |@'isSuccessful' sc@ returns 'True' iff @200 '<=' sc '<' 300@.
 isSuccessful ∷ StatusCode sc ⇒ sc → Bool
 {-# INLINE isSuccessful #-}
 isSuccessful = satisfy (\ n → n ≥ 200 ∧ n < 300)
 
--- |@'isRedirection' sc@ returns 'True' iff @300 <= sc < 400@.
+-- |@'isRedirection' sc@ returns 'True' iff @300 '<=' sc '<' 400@.
 isRedirection ∷ StatusCode sc ⇒ sc → Bool
 {-# INLINE isRedirection #-}
 isRedirection = satisfy (\ n → n ≥ 300 ∧ n < 400)
 
--- |@'isError' sc@ returns 'True' iff @400 <= sc@
+-- |@'isError' sc@ returns 'True' iff @400 '<=' sc@
 isError ∷ StatusCode sc ⇒ sc → Bool
 {-# INLINE isError #-}
 isError = satisfy (≥ 400)
 
--- |@'isClientError' sc@ returns 'True' iff @400 <= sc < 500@.
+-- |@'isClientError' sc@ returns 'True' iff @400 '<=' sc '<' 500@.
 isClientError ∷ StatusCode sc ⇒ sc → Bool
 {-# INLINE isClientError #-}
 isClientError = satisfy (\ n → n ≥ 400 ∧ n < 500)
 
--- |@'isServerError' sc@ returns 'True' iff @500 <= sc@.
+-- |@'isServerError' sc@ returns 'True' iff @500 '<=' sc@.
 isServerError ∷ StatusCode sc ⇒ sc → Bool
 {-# INLINE isServerError #-}
 isServerError = satisfy (≥ 500)