X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=Lucu.git;a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FETag.hs;h=3ebfc1d91b35b3f7e676571d0ed981a813b67dbf;hp=76df18378bf3e48417dddd8c73dc6222b65d5136;hb=67f9e87;hpb=a362be1c8664306b970c32e1df9b62081498feb1 diff --git a/Network/HTTP/Lucu/ETag.hs b/Network/HTTP/Lucu/ETag.hs index 76df183..3ebfc1d 100644 --- a/Network/HTTP/Lucu/ETag.hs +++ b/Network/HTTP/Lucu/ETag.hs @@ -1,12 +1,15 @@ {-# LANGUAGE - OverloadedStrings + DeriveDataTypeable + , FlexibleInstances + , MultiParamTypeClasses + , OverloadedStrings + , RecordWildCards + , TemplateHaskell , UnicodeSyntax #-} --- |Entity tags +-- |An internal module for entity tags. module Network.HTTP.Lucu.ETag ( ETag(..) - , parseETag - , printETag , strongETag , weakETag @@ -17,50 +20,55 @@ module Network.HTTP.Lucu.ETag 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.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 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\". See: + -- |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 an '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 ← eTag - 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' 'False'@. If you want to generate an -- ETag from a file, try using