]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/StatusCode/Internal.hs
Code clean-up using convertible-text.
[Lucu.git] / Network / HTTP / Lucu / StatusCode / Internal.hs
index 3addcf2abd748ea5125b710b56048ef6c003e6fd..21210375bbfa3ae5d34cd84fe759bf8c6ecbe7ad 100644 (file)
@@ -1,25 +1,31 @@
 {-# LANGUAGE
-    DeriveDataTypeable
-  , ExistentialQuantification
+    ExistentialQuantification
   , FlexibleInstances
+  , MultiParamTypeClasses
+  , OverlappingInstances
   , TemplateHaskell
   , UnicodeSyntax
   , ViewPatterns
   #-}
+{-# OPTIONS_GHC -fno-warn-orphans #-}
 module Network.HTTP.Lucu.StatusCode.Internal
     ( StatusCode(..)
     , SomeStatusCode(..)
+    , (≈)
+    , (≉)
     , statusCodes
     )
     where
 import Control.Applicative
-import Data.Ascii (Ascii)
+import Data.Ascii (Ascii, AsciiBuilder)
 import qualified Data.Ascii as A
-import Data.Attoparsec.Char8 as P
-import Data.Attoparsec.Lazy as LP
+import Data.Attoparsec.Char8
+import qualified Data.Attoparsec.Lazy as LP
 import qualified Data.ByteString.Lazy.Char8 as Lazy
+import Data.Convertible.Base
+import Data.Convertible.Instances.Ascii ()
+import Data.Convertible.Utils
 import Data.List
-import Data.Typeable
 import Language.Haskell.TH.Lib
 import Language.Haskell.TH.Syntax
 import Language.Haskell.TH.Quote
@@ -32,38 +38,62 @@ import Prelude.Unicode
 -- 'statusCodes'
 --
 -- Minimal complete definition: 'numericCode' and 'textualStatus'.
-class (Eq sc, Show sc, Typeable sc) ⇒ StatusCode sc where
+class Show sc ⇒ StatusCode sc where
     -- |Return the 3-digit integer for this status e.g. @200@
     numericCode ∷ sc → Int
     -- |Return the combination of 3-digit integer and reason phrase
     -- for this status e.g. @200 OK@
-    textualStatus ∷ sc → Ascii
+    textualStatus ∷ sc → AsciiBuilder
     -- |Wrap the status code into 'SomeStatusCode'.
     fromStatusCode ∷ sc → SomeStatusCode
     fromStatusCode = SomeStatusCode
-    -- |Cast the status code from 'SomeStatusCode'.
-    toStatusCode ∷ SomeStatusCode → Maybe sc
-    toStatusCode (SomeStatusCode sc) = cast sc
 
 -- |Container type for 'StatusCode' type class.
 data SomeStatusCode
     = ∀sc. StatusCode sc ⇒ SomeStatusCode sc
-      deriving Typeable
 
 instance Show SomeStatusCode where
     show (SomeStatusCode sc) = show sc
 
--- |Two 'StatusCode's @a@ and @b@ are said to be equivalent iff
--- @'numericCode' a == 'numericCode' b@.
 instance Eq SomeStatusCode where
-    (SomeStatusCode α) == (SomeStatusCode β)
-        = numericCode α ≡ numericCode β
+    (SomeStatusCode α) == (SomeStatusCode β) = α ≈ β
+
+infix 4 ≈, ≉
+-- |Equivalence of 'StatusCode's. Two 'StatusCode's @a@ and @b@ are
+-- said to be equivalent iff @'numericCode' a '==' 'numericCode' b@.
+--
+-- U+2248, ALMOST EQUAL TO
+(≈) ∷ (StatusCode α, StatusCode β) ⇒ α → β → Bool
+{-# INLINE (≈) #-}
+α ≈ β = numericCode α ≡ numericCode β
+
+-- |@(a ≉ b) '==' 'not' (a ≈ b)@
+--
+-- U+2249, NOT ALMOST EQUAL TO
+(≉) ∷ (StatusCode α, StatusCode β) ⇒ α → β → Bool
+{-# INLINE (≉) #-}
+(≉) = ((¬) ∘) ∘ (≈)
 
 instance StatusCode SomeStatusCode where
     numericCode   (SomeStatusCode sc) = numericCode   sc
     textualStatus (SomeStatusCode sc) = textualStatus sc
     fromStatusCode = id
-    toStatusCode   = Just
+
+instance StatusCode sc ⇒ ConvertSuccess sc Ascii where
+    {-# INLINE convertSuccess #-}
+    convertSuccess = convertSuccessVia ((⊥) ∷ AsciiBuilder)
+
+instance StatusCode sc ⇒ ConvertSuccess sc AsciiBuilder where
+    {-# INLINE convertSuccess #-}
+    convertSuccess = textualStatus
+
+instance StatusCode sc ⇒ ConvertAttempt sc Ascii where
+    {-# INLINE convertAttempt #-}
+    convertAttempt = return ∘ cs
+
+instance StatusCode sc ⇒ ConvertAttempt sc AsciiBuilder where
+    {-# INLINE convertAttempt #-}
+    convertAttempt = return ∘ cs
 
 -- |'QuasiQuoter' for 'StatusCode' declarations.
 --
@@ -80,20 +110,20 @@ instance StatusCode SomeStatusCode where
 -- becomes:
 --
 -- @
---   data OK = OK deriving ('Eq', 'Show', 'Typeable')
+--   data OK = OK deriving ('Show')
 --   instance OK where
 --     'numericCode'   _ = 200
---     'textualStatus' _ = 'A.unsafeFromString' \"200 OK\"
+--     'textualStatus' _ = 'cs' (\"200 OK\" ∷ Ascii)
 --
---   data BadRequest = BadRequest deriving ('Eq', 'Show', 'Typeable')
+--   data BadRequest = BadRequest deriving ('Show')
 --   instance BadRequest where
 --     'numericCode'   _ = 400
---     'textualStatus' _ = 'A.unsafeFromString' \"400 Bad Request\"
+--     'textualStatus' _ = 'cs' (\"400 Bad Request\" ∷ Ascii)
 --
---   data MethodNotAllowed = MethodNotAllowed deriving ('Eq', 'Show', 'Typeable')
+--   data MethodNotAllowed = MethodNotAllowed deriving ('Show')
 --   instance MethodNotAllowed where
 --     'numericCode'   _ = 405
---     'textualStatus' _ = 'A.unsafeFromString' \"405 Method Not Allowed\"
+--     'textualStatus' _ = 'cs' (\"405 Method Not Allowed\" ∷ Ascii)
 -- @
 statusCodes ∷ QuasiQuoter
 statusCodes = QuasiQuoter {
@@ -136,7 +166,7 @@ parseStatusCodes src
              "pair"
 
       word ∷ Parser Ascii
-      word = A.unsafeFromByteString <$> P.takeWhile1 isAlpha_ascii
+      word = A.unsafeFromByteString <$> takeWhile1 isAlpha_ascii
 
 statusDecl ∷ (Int, [Ascii]) → Q [Dec]
 statusDecl (num, phrase)
@@ -145,17 +175,10 @@ statusDecl (num, phrase)
          return (a:bs)
     where
       name ∷ Name
-      name = mkName $ concatMap A.toString phrase
+      name = mkName $ concatMap cs phrase
 
       dataDecl ∷ Q Dec
-      dataDecl = dataD (cxt [])
-                       name
-                       []
-                       [con]
-                       [ mkName "Eq"
-                       , mkName "Show"
-                       , mkName "Typeable"
-                       ]
+      dataDecl = dataD (cxt []) name [] [con] [''Show]
 
       instanceDecl ∷ Q [Dec]
       instanceDecl
@@ -173,8 +196,8 @@ statusDecl (num, phrase)
       con = return $ NormalC name []
 
       txt ∷ Q Exp
-      txt = [| A.unsafeFromString $(lift txt') |]
+      txt = [| cs ($(lift txt') ∷ Ascii) |]
 
       txt' ∷ String
       txt' = concat $ intersperse "\x20"
-                    $ show num : map A.toString phrase
+                    $ show num : map cs phrase