Fix bug in parser
This commit is contained in:
parent
a47d4903cb
commit
5182c47a19
@ -1,4 +1,5 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE ApplicativeDo #-}
|
||||||
module Data.Email.Header where
|
module Data.Email.Header where
|
||||||
|
|
||||||
import qualified Data.Foldable as F
|
import qualified Data.Foldable as F
|
||||||
@ -21,28 +22,39 @@ data Header
|
|||||||
|
|
||||||
decode :: ByteString -> Either String Header
|
decode :: ByteString -> Either String Header
|
||||||
decode = parseOnly parseHeader
|
decode = parseOnly parseHeader
|
||||||
where
|
{-# INLINE decode #-}
|
||||||
parseHeader :: Parser Header
|
|
||||||
parseHeader = parseFrom <|> parseTo
|
parseHeader :: Parser Header
|
||||||
parseFrom :: Parser Header
|
parseHeader = parseFrom <|> parseTo
|
||||||
parseFrom = From <$> (string "From:" *> emptySpace *> (bracketEmail <|> email))
|
{-# INLINE parseHeader #-}
|
||||||
parseTo :: Parser Header
|
|
||||||
parseTo = To <$> (string "To:" *> emptySpace *> emails)
|
parseFrom :: Parser Header
|
||||||
emptySpace = many' space
|
parseFrom = From <$> (string "From:" *> emptySpace *> (bracketEmail <|> email))
|
||||||
emails :: Parser (Vector ByteString)
|
|
||||||
emails = V.fromList <$> (bracketEmail <|> email) `sepBy` char ','
|
parseTo :: Parser Header
|
||||||
bracketEmail :: Parser ByteString
|
parseTo = To <$> (string "To:" *> emptySpace *> emails)
|
||||||
bracketEmail = do
|
|
||||||
|
emptySpace :: Parser ()
|
||||||
|
emptySpace = () <$ many' space
|
||||||
|
|
||||||
|
emails :: Parser (Vector ByteString)
|
||||||
|
emails = V.fromList <$> (bracketEmail <|> email) `sepBy` char ','
|
||||||
|
|
||||||
|
bracketEmail :: Parser ByteString
|
||||||
|
bracketEmail = do
|
||||||
_ <- manyTill anyChar (char '<')
|
_ <- manyTill anyChar (char '<')
|
||||||
email
|
email
|
||||||
email :: Parser ByteString
|
{-# INLINE bracketEmail #-}
|
||||||
email = do
|
|
||||||
|
email :: Parser ByteString
|
||||||
|
email = do
|
||||||
_ <- many' space
|
_ <- many' space
|
||||||
name <- BC.pack <$> many' (satisfy (\c -> not (isSpace c) && c /= '@'))
|
name <- BC.pack <$> many' (satisfy (\c -> not (isSpace c) && c /= '@'))
|
||||||
_ <- char '@'
|
_ <- char '@'
|
||||||
rest <- BC.pack <$> many' (satisfy (\c -> not (isSpace c) && c /= ',' && c /= '>'))
|
rest <- BC.pack <$> many' (satisfy (\c -> not (isSpace c) && c /= ',' && c /= '>' && c /= '<'))
|
||||||
_ <- many' (notChar ',')
|
_ <- many' (notChar ',')
|
||||||
pure (name <> "@" <> rest)
|
pure (name <> "@" <> rest)
|
||||||
|
{-# INLINE email #-}
|
||||||
|
|
||||||
|
|
||||||
encode :: Header -> ByteString
|
encode :: Header -> ByteString
|
||||||
|
@ -31,4 +31,7 @@ tests = testGroup "Data.Email"
|
|||||||
[ testCase "Can parse a sample email" $ do
|
[ testCase "Can parse a sample email" $ do
|
||||||
got <- parseToList sample
|
got <- parseToList sample
|
||||||
got @?= [ From "me@example.com", To ["you@example.com"]]
|
got @?= [ From "me@example.com", To ["you@example.com"]]
|
||||||
|
, testCase "Combined mailto" $ do
|
||||||
|
let got = decode "To: James Doe<james.doe@example.com<mailto:james.doe@example.com>>"
|
||||||
|
got @?= Right (To ["james.doe@example.com"])
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user