-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Cady
committed
Jan 24, 2016
1 parent
eab2150
commit faf9c1b
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Text.Domain.Validate | ||
( isValid | ||
, validate | ||
, domainName | ||
, DomainName | ||
, toByteString | ||
) | ||
where | ||
|
||
import Control.Applicative ((<*)) | ||
|
||
import Data.Attoparsec.ByteString (endOfInput, parseOnly) | ||
import Data.ByteString (ByteString) | ||
|
||
import Data.Data (Data, Typeable) | ||
import GHC.Generics (Generic) | ||
import Text.Email.Parser (domainLiteral) | ||
import qualified Text.Read as Read | ||
|
||
|
||
-- | Represents a domain name. | ||
data DomainName = DomainName ByteString | ||
deriving (Eq, Ord, Data, Typeable, Generic) | ||
|
||
-- | Smart constructor for a domain name | ||
domainName :: ByteString -> Maybe DomainName | ||
domainName = either (const Nothing) Just . validate | ||
|
||
-- | Validates whether a particular string is a domain name | ||
-- according to RFC5322. | ||
isValid :: ByteString -> Bool | ||
isValid = either (const False) (const True) . validate | ||
|
||
-- | If you want to find out *why* a particular string is not | ||
-- a domain name, use this. | ||
|
||
validate :: ByteString -> Either String DomainName | ||
validate = fmap DomainName . parseOnly (domainLiteral <* endOfInput) | ||
|
||
instance Show DomainName where | ||
show = show . toByteString | ||
|
||
instance Read DomainName where | ||
readListPrec = Read.readListPrecDefault | ||
readPrec = Read.parens (do | ||
bs <- Read.readPrec | ||
case parseOnly (domainLiteral <* endOfInput) bs of | ||
Left _ -> Read.pfail | ||
Right a -> return $ DomainName a) | ||
|
||
-- | Converts an email address back to a ByteString | ||
toByteString :: DomainName -> ByteString | ||
toByteString (DomainName d) = d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ module Text.Email.Parser | |
, EmailAddress | ||
, unsafeEmailAddress | ||
, toByteString | ||
, domainLiteral | ||
) | ||
where | ||
|
||
|