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

View File

@ -1,6 +1,6 @@
{ mkDerivation, attoparsec, base, bytestring-trie, hedgehog { mkDerivation, attoparsec, base, bytestring, bytestring-trie
, hedgehog-corpus, HUnit, mtl, pipes, pipes-bytestring, stdenv , conduit, conduit-extra, hedgehog, hedgehog-corpus, HUnit, lens
, tasty, tasty-hedgehog, tasty-hunit, text, vector , mtl, stdenv, tasty, tasty-hedgehog, tasty-hunit, text, vector
}: }:
mkDerivation { mkDerivation {
pname = "addressbook"; pname = "addressbook";
@ -9,13 +9,13 @@ mkDerivation {
isLibrary = true; isLibrary = true;
isExecutable = true; isExecutable = true;
libraryHaskellDepends = [ libraryHaskellDepends = [
attoparsec base bytestring-trie mtl pipes pipes-bytestring text attoparsec base bytestring bytestring-trie conduit conduit-extra
vector lens mtl text vector
]; ];
executableHaskellDepends = [ base ]; executableHaskellDepends = [ base ];
testHaskellDepends = [ testHaskellDepends = [
base hedgehog hedgehog-corpus HUnit tasty tasty-hedgehog base bytestring conduit conduit-extra hedgehog hedgehog-corpus
tasty-hunit text vector HUnit tasty tasty-hedgehog tasty-hunit text vector
]; ];
license = stdenv.lib.licenses.bsd3; 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 Test.Tasty
import qualified Test.Data.Email as Data.Email
import qualified Test.Data.Email.Header as Data.Email.Header import qualified Test.Data.Email.Header as Data.Email.Header
tests :: TestTree tests :: TestTree
tests = testGroup "tests" tests = testGroup "tests"
[ Data.Email.Header.tests [ Data.Email.Header.tests
, Data.Email.tests
] ]
main :: IO () 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"]]
]