]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/ETag.hs
The library now compiles, and I'm now working on ImplantFile.hs
[Lucu.git] / Network / HTTP / Lucu / ETag.hs
index acc496fa2113ef6a0848e570dffd56c453566f4b..7e618782c45e17e75ec16ef8667510447c518a20 100644 (file)
@@ -2,12 +2,10 @@
     OverloadedStrings
   , UnicodeSyntax
   #-}
-{-# OPTIONS_HADDOCK prune #-}
-
 -- |Manipulation of entity tags.
 module Network.HTTP.Lucu.ETag
     ( ETag(..)
-
+    , parseETag
     , printETag
 
     , strongETag
@@ -16,14 +14,15 @@ module Network.HTTP.Lucu.ETag
     , eTagListP
     )
     where
+import Control.Applicative
 import Control.Monad
-import Control.Monad.Unicode
 import Data.Ascii (Ascii)
 import qualified Data.Ascii as A
 import Data.Attoparsec.Char8
 import Data.Monoid.Unicode
 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.
 data ETag = ETag {
@@ -47,6 +46,19 @@ printETag et
         ⊕
         quoteStr (etagToken et) )
 
+-- |Parse 'Etag' from an 'Ascii'. This functions throws an exception
+-- for parse error.
+parseETag ∷ Ascii → ETag
+parseETag str
+    = let p  = do et ← eTagP
+                  endOfInput
+                  return et
+          bs = A.toByteString str
+      in
+        case parseOnly p bs of
+          Right et → et
+          Left err → error ("unparsable ETag: " ⧺ A.toString str ⧺ ": " ⧺ err)
+
 -- |This is equivalent to @'ETag' 'Prelude.False'@. If you want to
 -- generate an ETag from a file, try using
 -- 'Network.HTTP.Lucu.StaticFile.generateETagFromFile'.
@@ -58,7 +70,7 @@ weakETag ∷ Ascii → ETag
 weakETag = ETag True
 
 eTagP ∷ Parser ETag
-eTagP = do isWeak ← option False (string "W/"  return True)
+eTagP = do isWeak ← option False (string "W/" *> return True)
            str    ← quotedStr
            return $ ETag isWeak str