Compare commits

..

No commits in common. "5182c47a196dc82e69eec3544b23a743fddb240a" and "03860c370ae1dd5f963910fe8f70577e6f6e6982" have entirely different histories.

3 changed files with 28 additions and 45 deletions

View File

@ -29,7 +29,6 @@ import qualified Data.ByteString.Lazy.Char8 as LBC
import System.IO.Unsafe (unsafeInterleaveIO) import System.IO.Unsafe (unsafeInterleaveIO)
import Control.Parallel.Strategies (rseq, parMap) import Control.Parallel.Strategies (rseq, parMap)
import qualified Data.List as L import qualified Data.List as L
import Control.Monad (unless)
combine :: (MonadUnliftIO m, MonadResource m, MonadThrow m, MonadIO m) => ConduitM FilePath Header m () combine :: (MonadUnliftIO m, MonadResource m, MonadThrow m, MonadIO m) => ConduitM FilePath Header m ()
combine = await >>= \case combine = await >>= \case
@ -47,13 +46,12 @@ run = do
let datFile = datDir </> ".addressbook.dat" let datFile = datDir </> ".addressbook.dat"
original <- Set.fromList . map LBS.toStrict . lbsLines <$> LBS.readFile datFile original <- Set.fromList . map LBS.toStrict . lbsLines <$> LBS.readFile datFile
xs <- LBS.getContents >>= stream xs <- LBS.getContents >>= stream
let set = original `Set.union` F.fold (parMap rseq F.fold (chunks 200 xs)) let set = original `Set.union` F.fold (parMap rseq F.fold (chunks 20 xs))
unless (original == set) $ runResourceT $
runResourceT $ runConduit $
runConduit $ CL.sourceList (Set.elems set)
CL.sourceList (Set.elems set) .| C.map (<> "\n")
.| C.map (<> "\n") .| CB.sinkFileCautious datFile
.| CB.sinkFileCautious datFile
where where
separate = \case separate = \case
From x -> [x] From x -> [x]

View File

@ -1,5 +1,4 @@
{-# 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
@ -22,39 +21,28 @@ data Header
decode :: ByteString -> Either String Header decode :: ByteString -> Either String Header
decode = parseOnly parseHeader decode = parseOnly parseHeader
{-# INLINE decode #-} where
parseHeader :: Parser Header
parseHeader :: Parser Header parseHeader = parseFrom <|> parseTo
parseHeader = parseFrom <|> parseTo parseFrom :: Parser Header
{-# INLINE parseHeader #-} parseFrom = From <$> (string "From:" *> emptySpace *> (bracketEmail <|> email))
parseTo :: Parser Header
parseFrom :: Parser Header parseTo = To <$> (string "To:" *> emptySpace *> emails)
parseFrom = From <$> (string "From:" *> emptySpace *> (bracketEmail <|> email)) emptySpace = many' space
emails :: Parser (Vector ByteString)
parseTo :: Parser Header emails = V.fromList <$> (bracketEmail <|> email) `sepBy` char ','
parseTo = To <$> (string "To:" *> emptySpace *> emails) bracketEmail :: Parser ByteString
bracketEmail = do
emptySpace :: Parser () _ <- manyTill anyChar (char '<')
emptySpace = () <$ many' space email
email :: Parser ByteString
emails :: Parser (Vector ByteString) email = do
emails = V.fromList <$> (bracketEmail <|> email) `sepBy` char ',' _ <- many' space
name <- BC.pack <$> many' (satisfy (\c -> not (isSpace c) && c /= '@'))
bracketEmail :: Parser ByteString _ <- char '@'
bracketEmail = do rest <- BC.pack <$> many' (satisfy (\c -> not (isSpace c) && c /= ',' && c /= '>'))
_ <- manyTill anyChar (char '<') _ <- many' (notChar ',')
email pure (name <> "@" <> rest)
{-# INLINE bracketEmail #-}
email :: Parser ByteString
email = do
_ <- many' space
name <- BC.pack <$> many' (satisfy (\c -> not (isSpace c) && c /= '@'))
_ <- char '@'
rest <- BC.pack <$> many' (satisfy (\c -> not (isSpace c) && c /= ',' && c /= '>' && c /= '<'))
_ <- many' (notChar ',')
pure (name <> "@" <> rest)
{-# INLINE email #-}
encode :: Header -> ByteString encode :: Header -> ByteString

View File

@ -31,7 +31,4 @@ 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"])
] ]