diff --git a/addressbook.cabal b/addressbook.cabal index 75ca429..719827f 100644 --- a/addressbook.cabal +++ b/addressbook.cabal @@ -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 diff --git a/default.nix b/default.nix index e120e20..c4377c6 100644 --- a/default.nix +++ b/default.nix @@ -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; } diff --git a/src/Data/Email.hs b/src/Data/Email.hs new file mode 100644 index 0000000..77e73ef --- /dev/null +++ b/src/Data/Email.hs @@ -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 diff --git a/test/MyLibTest.hs b/test/MyLibTest.hs index c31a9ee..3f29032 100644 --- a/test/MyLibTest.hs +++ b/test/MyLibTest.hs @@ -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 () diff --git a/test/Test/Data/Email.hs b/test/Test/Data/Email.hs new file mode 100644 index 0000000..a5edecf --- /dev/null +++ b/test/Test/Data/Email.hs @@ -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 \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"]] + ]