module Text.Email.Validate
( isValid
, validate
, emailAddress
, canonicalizeEmail
, EmailAddress
, localPart
, domainPart
, toByteString
)
where
import Control.Applicative ((<*))
import Data.ByteString (ByteString)
import Data.Attoparsec.ByteString (parseOnly, endOfInput)
import Text.Email.Parser (EmailAddress, toByteString, addrSpec, localPart, domainPart)
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 <* endOfInput)