X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=Lucu.git;a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FETag.hs;h=de7780c538992f696d1d7aa322685f2f8d13192f;hp=f7ef8387c7044d02acc1451a7fa0e140c3197c60;hb=90fca0675b1694e69b8e431c989343855cbd125d;hpb=ac2ff93f647d60d43ca3cc54eb776fe0f701ac9e diff --git a/Network/HTTP/Lucu/ETag.hs b/Network/HTTP/Lucu/ETag.hs index f7ef838..de7780c 100644 --- a/Network/HTTP/Lucu/ETag.hs +++ b/Network/HTTP/Lucu/ETag.hs @@ -1,87 +1,91 @@ {-# LANGUAGE - OverloadedStrings + DeriveDataTypeable + , FlexibleInstances + , MultiParamTypeClasses + , OverloadedStrings + , RecordWildCards + , TemplateHaskell + , TypeSynonymInstances , UnicodeSyntax #-} --- |Manipulation of entity tags. +-- |An internal module for entity tags. module Network.HTTP.Lucu.ETag ( ETag(..) - , parseETag - , printETag - , strongETag , weakETag - , eTagP - , eTagListP ) where import Control.Applicative -import Control.Monad import Data.Ascii (Ascii, AsciiBuilder) -import qualified Data.Ascii as A import Data.Attoparsec.Char8 +import Data.Convertible.Base +import Data.Convertible.Instances.Ascii () +import Data.Convertible.Utils +import Data.Data +import Data.Default import Data.Monoid.Unicode +import Language.Haskell.TH.Syntax +import Network.HTTP.Lucu.OrphanInstances () import Network.HTTP.Lucu.Parser.Http hiding (token) import Network.HTTP.Lucu.Utils import Prelude.Unicode --- |An entity tag is made of a weakness flag and a opaque string. +-- |An entity tag consists of a weakness flag and an opaque string. data ETag = ETag { - -- |The weakness flag. Weak tags looks like W\/\"blahblah\" and - -- strong tags are like \"blahblah\". + -- |The weakness flag. Weak tags looks like @W\/\"blahblah\"@ + -- and strong tags are like @\"blahblah\"@. See: + -- etagIsWeak ∷ !Bool -- |An opaque string. Only characters from 0x20 (sp) to 0x7e (~) -- are allowed. , etagToken ∷ !Ascii - } deriving (Eq, Show) + } deriving (Eq, Show, Data, Typeable) --- |Convert an 'ETag' to 'AsciiBuilder'. -printETag ∷ ETag → AsciiBuilder -{-# INLINEABLE printETag #-} -printETag et - = ( if etagIsWeak et then - A.toAsciiBuilder "W/" - else - (∅) - ) - ⊕ - quoteStr (etagToken et) +instance Lift ETag where + lift (ETag {..}) + = [| ETag { + etagIsWeak = $(lift etagIsWeak) + , etagToken = $(lift etagToken ) + } + |] --- |Parse 'Etag' from an 'Ascii'. This functions throws an exception --- for parse error. -parseETag ∷ Ascii → ETag -{-# INLINEABLE parseETag #-} -parseETag str - = case parseOnly p $ A.toByteString str of - Right et → et - Left err → error ("unparsable ETag: " ⧺ A.toString str ⧺ ": " ⧺ err) - where - p ∷ Parser ETag - {-# INLINE p #-} - p = do et ← eTagP - endOfInput - return et +instance ConvertSuccess ETag Ascii where + {-# INLINE convertSuccess #-} + convertSuccess = convertSuccessVia ((⊥) ∷ AsciiBuilder) + +instance ConvertSuccess ETag AsciiBuilder where + {-# INLINE convertSuccess #-} + convertSuccess (ETag {..}) + = ( if etagIsWeak then + cs ("W/" ∷ Ascii) + else + (∅) + ) + ⊕ + quoteStr etagToken + +deriveAttempts [ ([t| ETag |], [t| Ascii |]) + , ([t| ETag |], [t| AsciiBuilder |]) + ] --- |This is equivalent to @'ETag' 'Prelude.False'@. If you want to --- generate an ETag from a file, try using +-- |This is equivalent to @'ETag' 'False'@. If you want to generate an +-- ETag from a file, try using -- 'Network.HTTP.Lucu.StaticFile.generateETagFromFile'. strongETag ∷ Ascii → ETag {-# INLINE strongETag #-} strongETag = ETag False --- |This is equivalent to @'ETag' 'Prelude.True'@. +-- |This is equivalent to @'ETag' 'True'@. weakETag ∷ Ascii → ETag {-# INLINE weakETag #-} weakETag = ETag True -eTagP ∷ Parser ETag -{-# INLINEABLE eTagP #-} -eTagP = do isWeak ← option False (string "W/" *> return True) - str ← quotedStr - return $ ETag isWeak str +instance Default (Parser ETag) where + {-# INLINEABLE def #-} + def = do isWeak ← option False (string "W/" *> return True) + str ← quotedStr + return $ ETag isWeak str -eTagListP ∷ Parser [ETag] -{-# INLINEABLE eTagListP #-} -eTagListP = do xs ← listOf eTagP - when (null xs) $ - fail "empty list of ETags" - return xs +instance Default (Parser [ETag]) where + {-# INLINE def #-} + def = listOf def