Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CaseInsensitive for domain part to follow RFC #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion email-validate.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ library
base >= 4.4 && < 5,
attoparsec >= 0.10.0 && < 0.14,
bytestring >= 0.9 && < 0.11,
template-haskell >= 2.10.0.0 && < 2.16
template-haskell >= 2.10.0.0 && < 2.16,
case-insensitive >= 1.2.0.11 && < 1.3
default-language: Haskell2010
hs-source-dirs: src
ghc-options: -Wall
Expand Down
17 changes: 12 additions & 5 deletions src/Text/Email/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Text.Email.Parser
( addrSpec
, localPart
, domainPart
, domainPartOriginal
, EmailAddress
, unsafeEmailAddress
, toByteString
Expand All @@ -15,20 +16,21 @@ import Control.Monad (guard, void, when)
import Data.Attoparsec.ByteString.Char8
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS
import qualified Data.CaseInsensitive as CI
import Data.Data (Data, Typeable)
import GHC.Generics (Generic)
import qualified Text.Read as Read

-- | Represents an email address.
data EmailAddress = EmailAddress ByteString ByteString
data EmailAddress = EmailAddress (CI.CI ByteString) (CI.CI ByteString)
deriving (Eq, Ord, Data, Typeable, Generic)

-- | Creates an email address without validating it.
-- You should only use this when reading data from
-- somewhere it has already been validated (e.g. a
-- database).
unsafeEmailAddress :: ByteString -> ByteString -> EmailAddress
unsafeEmailAddress = EmailAddress
unsafeEmailAddress lp dp = EmailAddress ( CI.mk lp ) ( CI.mk dp )

instance Show EmailAddress where
show = show . toByteString
Expand All @@ -43,15 +45,20 @@ instance Read EmailAddress where

-- | Converts an email address back to a ByteString
toByteString :: EmailAddress -> ByteString
toByteString (EmailAddress l d) = BS.concat [l, BS.singleton '@', d]
toByteString (EmailAddress l d) = BS.concat [CI.foldedCase l, BS.singleton '@', CI.foldedCase d]

-- | Extracts the local part of an email address.
localPart :: EmailAddress -> ByteString
localPart (EmailAddress l _) = l
localPart (EmailAddress l _) = CI.foldedCase l

-- | Extracts the domain part of an email address.
domainPart :: EmailAddress -> ByteString
domainPart (EmailAddress _ d) = d
domainPart (EmailAddress _ d) = CI.foldedCase d

-- | Extracts the domain part of an email address in its original case
domainPartOriginal :: EmailAddress -> ByteString
domainPartOriginal (EmailAddress _ d) = CI.original d


-- | A parser for email addresses.
addrSpec :: Parser EmailAddress
Expand Down