Parse headers from an email

This commit is contained in:
Mats Rauhala 2020-12-10 22:22:46 +02:00
parent 98ec13e0cd
commit d0521df8bb
5 changed files with 63 additions and 9 deletions

View File

@ -19,6 +19,7 @@ extra-source-files: CHANGELOG.md
library
exposed-modules: MyLib
, Data.Email.Header
, Data.Email
-- other-modules:
-- other-extensions:
default-extensions: OverloadedStrings
@ -26,8 +27,10 @@ library
build-depends: base ^>=4.13.0.0
, attoparsec
, mtl
, pipes
, pipes-bytestring
, conduit
, conduit-extra
, bytestring
, lens
, text
, bytestring-trie
, vector
@ -47,6 +50,7 @@ test-suite addressbook-test
default-extensions: OverloadedStrings
LambdaCase
other-modules: Test.Data.Email.Header
, Test.Data.Email
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: MyLibTest.hs
@ -57,6 +61,9 @@ test-suite addressbook-test
, tasty-hunit
, HUnit
, hedgehog
, bytestring
, hedgehog-corpus
, text
, vector
, conduit
, conduit-extra

View File

@ -1,6 +1,6 @@
{ mkDerivation, attoparsec, base, bytestring-trie, hedgehog
, hedgehog-corpus, HUnit, mtl, pipes, pipes-bytestring, stdenv
, tasty, tasty-hedgehog, tasty-hunit, text, vector
{ mkDerivation, attoparsec, base, bytestring, bytestring-trie
, conduit, conduit-extra, hedgehog, hedgehog-corpus, HUnit, lens
, mtl, stdenv, tasty, tasty-hedgehog, tasty-hunit, text, vector
}:
mkDerivation {
pname = "addressbook";
@ -9,13 +9,13 @@ mkDerivation {
isLibrary = true;
isExecutable = true;
libraryHaskellDepends = [
attoparsec base bytestring-trie mtl pipes pipes-bytestring text
vector
attoparsec base bytestring bytestring-trie conduit conduit-extra
lens mtl text vector
];
executableHaskellDepends = [ base ];
testHaskellDepends = [
base hedgehog hedgehog-corpus HUnit tasty tasty-hedgehog
tasty-hunit text vector
base bytestring conduit conduit-extra hedgehog hedgehog-corpus
HUnit tasty tasty-hedgehog tasty-hunit text vector
];
license = stdenv.lib.licenses.bsd3;
}

13
src/Data/Email.hs Normal file
View File

@ -0,0 +1,13 @@
module Data.Email where
import Data.Email.Header
import Conduit
import qualified Data.Conduit.Combinators as C
import qualified Data.Conduit.Text as CT
import Data.ByteString
(ByteString)
parseEmail :: (MonadThrow m, Monad m) => ConduitM ByteString Header m ()
parseEmail = CT.decode CT.utf8 .| CT.lines .| C.concatMap decode

View File

@ -2,11 +2,13 @@ module Main (main) where
import Test.Tasty
import qualified Test.Data.Email as Data.Email
import qualified Test.Data.Email.Header as Data.Email.Header
tests :: TestTree
tests = testGroup "tests"
[ Data.Email.Header.tests
, Data.Email.tests
]
main :: IO ()

32
test/Test/Data/Email.hs Normal file
View File

@ -0,0 +1,32 @@
{-# LANGUAGE OverloadedLists #-}
module Test.Data.Email where
import Test.Tasty
import Test.Tasty.HUnit
import Data.ByteString.Lazy.Char8 (ByteString)
import qualified Data.Conduit.List as CL
import qualified Data.Conduit.Binary as CB
import Conduit
import Data.Email.Header
import Data.Email
sample :: ByteString
sample =
"Subject: Hello worldddd\n\
\From: me@example.com\n\
\To: you <you@example.com>\n\
\ \n\n \
\foo"
parseToList :: ByteString -> IO [Header]
parseToList _ = runConduit (CB.sourceLbs sample .| parseEmail .| CL.consume)
tests :: TestTree
tests = testGroup "Data.Email"
[ testCase "Can parse a sample email" $ do
got <- parseToList sample
got @?= [ From "me@example.com", To ["you@example.com"]]
]