Begin moving the name parsers to the Network.DNS.RBL.Domain namespace.
[dead/harbl.git] / harbl / src / Network / DNS / RBL / Domain / Digit.hs
1 -- | The simplest module you'll ever see. It contains the 'Digit' type
2 -- and a Parsec parser to parse one. We don't export its constructor
3 -- because then you could do something dumb like stick a letter
4 -- inside one.
5 --
6 -- These are defined in RFC1035, Section 2.3.1, \"Preferred name
7 -- syntax\" <https://tools.ietf.org/html/rfc1035#section-2.3.1>:
8 --
9 -- <digit> ::= any one of the ten digits 0 through 9
10 --
11 module Network.DNS.RBL.Domain.Digit (
12 Digit,
13 digit )
14 where
15
16 import qualified Text.Parsec as Parsec ( digit )
17 import Text.Parsec.String ( Parser )
18
19 import Network.DNS.RBL.Pretty ( Pretty(..) )
20
21
22 -- | A wrapper around a single digit character.
23 --
24 -- ==== _Examples_
25 --
26 -- >>> Digit '1'
27 -- Digit '1'
28 --
29 -- >>> let d1 = Digit '2'
30 -- >>> let d2 = Digit '2'
31 -- >>> let d3 = Digit '3'
32 -- >>> d1 == d2
33 -- True
34 -- >>> d1 == d3
35 -- False
36 --
37 newtype Digit = Digit Char deriving (Eq, Show)
38
39
40 -- | Pretty-printing for digits that we've already parsed. Just
41 -- shows/prints the digit character.
42 --
43 -- ==== _Examples_
44 --
45 -- >>> let d = Digit '1'
46 -- >>> pretty_print d
47 -- 1
48 --
49 instance Pretty Digit where pretty_show (Digit d) = [d]
50
51
52 -- | Parse a single digit, but wrap it in our 'Digit' type.
53 --
54 -- ==== _Examples_
55 --
56 -- >>> import Text.Parsec ( parseTest )
57 --
58 -- Digits are parsed correctly:
59 --
60 -- >>> parseTest digit "3"
61 -- Digit '3'
62 --
63 -- But letters are not:
64 --
65 -- >>> parseTest digit "x"
66 -- parse error at (line 1, column 1):
67 -- unexpected "x"
68 -- expecting digit
69 --
70 digit :: Parser Digit
71 digit = fmap Digit Parsec.digit