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(..) )
25 -- | A wrapper around a letter character.
32 newtype Letter = Letter Char deriving (Show)
35 -- | Pretty-printing for letters that we've already parsed. Just
36 -- shows/prints the letter character.
40 -- >>> let l = Letter 'x'
44 instance Pretty Letter where pretty_show (Letter l) = [l]
47 -- | Parse a single letter, but wrap it in our 'Letter' type.
51 -- >>> import Text.Parsec ( parseTest )
53 -- Letters are parsed correctly:
55 -- >>> parseTest letter "x"
58 -- But digits are not:
60 -- >>> parseTest letter "1"
61 -- parse error at (line 1, column 1):
65 letter :: Parser Letter
66 letter = fmap Letter Parsec.letter
70 -- | The derived instance of 'Eq' for letters is incorrect. All
71 -- comparisons should be made case-insensitively. The following
72 -- is an excerpt from RFC1035:
74 -- 2.3.3. Character Case
76 -- For all parts of the DNS that are part of the official
77 -- protocol, all comparisons between character strings (e.g.,
78 -- labels, domain names, etc.) are done in a case-insensitive
81 -- Since each part of DNS name is composed of our custom types, it
82 -- suffices to munge the equality for 'Letter'. RFC4343
83 -- <https://tools.ietf.org/html/rfc4343> clarifies the
84 -- case-insensitivity rules, but the fact that we're treating DNS
85 -- names as strings makes most of those problems go away (in
86 -- exchange for new ones).
90 -- >>> let l1 = Letter 'x'
91 -- >>> let l2 = Letter 'x'
92 -- >>> let l3 = Letter 'X'
93 -- >>> let l4 = Letter 'X'
103 instance Eq Letter where
104 (Letter l1) == (Letter l2) = (toLower l1) == (toLower l2)