module Text.Email.Validate
( isValid
, validate
, emailAddress
, canonicalizeEmail
, EmailAddress
, domainPart
, localPart
, toByteString
, unsafeEmailAddress
)
where
import Data.Attoparsec.ByteString (endOfInput, parseOnly)
import Data.ByteString (ByteString)
import Text.Email.Parser
( EmailAddress
, addrSpec
, domainPart
, localPart
, toByteString
, unsafeEmailAddress)
emailAddress :: ByteString -> Maybe EmailAddress
emailAddress = either (const Nothing) Just . validate
canonicalizeEmail :: ByteString -> Maybe ByteString
canonicalizeEmail = fmap toByteString . emailAddress
isValid :: ByteString -> Bool
isValid = either (const False) (const True) . validate
validate :: ByteString -> Either String EmailAddress
validate = parseOnly (addrSpec >>= \r -> endOfInput >> return r)