module Network.HTTP.Lucu.ETag
( ETag(..)
, strongETag
, weakETag
)
where
import Control.Applicative
import Data.Ascii (Ascii, AsciiBuilder)
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
data ETag = ETag {
etagIsWeak ∷ !Bool
, etagToken ∷ !Ascii
} deriving (Eq, Show, Data, Typeable)
instance Lift ETag where
lift (ETag {..})
= [| ETag {
etagIsWeak = $(lift etagIsWeak)
, etagToken = $(lift etagToken )
}
|]
instance ConvertSuccess ETag Ascii where
convertSuccess = convertSuccessVia ((⊥) ∷ AsciiBuilder)
instance ConvertSuccess ETag AsciiBuilder where
convertSuccess (ETag {..})
= ( if etagIsWeak then
cs ("W/" ∷ Ascii)
else
(∅)
)
⊕
quoteStr etagToken
deriveAttempts [ ([t| ETag |], [t| Ascii |])
, ([t| ETag |], [t| AsciiBuilder |])
]
strongETag ∷ Ascii → ETag
strongETag = ETag False
weakETag ∷ Ascii → ETag
weakETag = ETag True
instance Default (Parser ETag) where
def = do isWeak ← option False (string "W/" *> return True)
str ← quotedStr
return $ ETag isWeak str
instance Default (Parser [ETag]) where
def = listOf def