1 -- | The... also... the simplest module you'll ever see. It contains
2 -- the 'Letter' type and a Parsec parser to parse one. We don't
3 -- export its constructor because then you could do something dumb
4 -- like stick a digit inside one.
6 -- These are defined in RFC1035, Section 2.3.1, \"Preferred name
7 -- syntax\" <https://tools.ietf.org/html/rfc1035#section-2.3.1>:
9 -- <letter> ::= any one of the 52 alphabetic characters A through
10 -- Z in upper case and a through z in lower case
12 module Network.DNS.RBL.Domain.Letter (
17 import Data.Char ( toLower )
18 import qualified Text.Parsec as Parsec ( letter )
19 import Text.Parsec.String ( Parser )
21 import Network.DNS.RBL.Pretty ( Pretty(..) )
24 -- | A wrapper around a letter character.
31 newtype Letter = Letter Char deriving (Show)
34 -- | Pretty-printing for letters that we've already parsed. Just
35 -- shows/prints the letter character.
39 -- >>> let l = Letter 'x'
43 instance Pretty Letter where pretty_show (Letter l) = [l]
46 -- | Parse a single letter, but wrap it in our 'Letter' type.
50 -- >>> import Text.Parsec ( parseTest )
52 -- Letters are parsed correctly:
54 -- >>> parseTest letter "x"
57 -- But digits are not:
59 -- >>> parseTest letter "1"
60 -- parse error at (line 1, column 1):
64 letter :: Parser Letter
65 letter = fmap Letter Parsec.letter
69 -- | The derived instance of 'Eq' for letters is incorrect. All
70 -- comparisons should be made case-insensitively. The following
71 -- is an excerpt from RFC1035:
73 -- 2.3.3. Character Case
75 -- For all parts of the DNS that are part of the official
76 -- protocol, all comparisons between character strings (e.g.,
77 -- labels, domain names, etc.) are done in a case-insensitive
80 -- Since each part of DNS name is composed of our custom types, it
81 -- suffices to munge the equality for 'Letter'. RFC4343
82 -- <https://tools.ietf.org/html/rfc4343> clarifies the
83 -- case-insensitivity rules, but the fact that we're treating DNS
84 -- names as strings makes most of those problems go away (in
85 -- exchange for new ones).
89 -- >>> let l1 = Letter 'x'
90 -- >>> let l2 = Letter 'x'
91 -- >>> let l3 = Letter 'X'
92 -- >>> let l4 = Letter 'X'
102 instance Eq Letter where
103 (Letter l1) == (Letter l2) = (toLower l1) == (toLower l2)