]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/ETag.hs
ImplantFile.hs now compiles.
[Lucu.git] / Network / HTTP / Lucu / ETag.hs
index acc496fa2113ef6a0848e570dffd56c453566f4b..9bfa9aa29e67555499dc62bfecfa3b8b8f97b04b 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 Data.Ascii (Ascii, AsciiBuilder)
 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 {
@@ -35,17 +34,29 @@ data ETag = ETag {
     , etagToken  ∷ !Ascii
     } deriving (Eq, Show)
 
--- |Convert an 'ETag' to 'Ascii'.
-printETag ∷ ETag → Ascii
+-- |Convert an 'ETag' to 'AsciiBuilder'.
+printETag ∷ ETag → AsciiBuilder
 printETag et
-    = A.fromAsciiBuilder $
-      ( ( if etagIsWeak et then
-              A.toAsciiBuilder "W/"
-          else
-              (∅)
-        )
-        ⊕
-        quoteStr (etagToken et) )
+    = ( if etagIsWeak et then
+            A.toAsciiBuilder "W/"
+        else
+            (∅)
+      )
+      ⊕
+      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
@@ -58,7 +69,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