]> gitweb.michael.orlitzky.com - dead/harbl.git/blobdiff - harbl/src/Network/DNS/RBL/Domain/Digit.hs
Begin moving the name parsers to the Network.DNS.RBL.Domain namespace.
[dead/harbl.git] / harbl / src / Network / DNS / RBL / Domain / Digit.hs
diff --git a/harbl/src/Network/DNS/RBL/Domain/Digit.hs b/harbl/src/Network/DNS/RBL/Domain/Digit.hs
new file mode 100644 (file)
index 0000000..fd7ce06
--- /dev/null
@@ -0,0 +1,71 @@
+-- | The simplest module you'll ever see. It contains the 'Digit' type
+--   and a Parsec parser to parse one. We don't export its constructor
+--   because then you could do something dumb like stick a letter
+--   inside one.
+--
+--   These are defined in RFC1035, Section 2.3.1, \"Preferred name
+--   syntax\" <https://tools.ietf.org/html/rfc1035#section-2.3.1>:
+--
+--     <digit> ::= any one of the ten digits 0 through 9
+--
+module Network.DNS.RBL.Domain.Digit (
+  Digit,
+  digit )
+where
+
+import qualified Text.Parsec as Parsec ( digit )
+import Text.Parsec.String ( Parser )
+
+import Network.DNS.RBL.Pretty ( Pretty(..) )
+
+
+-- | A wrapper around a single digit character.
+--
+--   ==== _Examples_
+--
+--   >>> Digit '1'
+--   Digit '1'
+--
+--   >>> let d1 = Digit '2'
+--   >>> let d2 = Digit '2'
+--   >>> let d3 = Digit '3'
+--   >>> d1 == d2
+--   True
+--   >>> d1 == d3
+--   False
+--
+newtype Digit = Digit Char deriving (Eq, Show)
+
+
+-- | Pretty-printing for digits that we've already parsed. Just
+--   shows/prints the digit character.
+--
+--   ==== _Examples_
+--
+--   >>> let d = Digit '1'
+--   >>> pretty_print d
+--   1
+--
+instance Pretty Digit where pretty_show (Digit d) = [d]
+
+
+-- | Parse a single digit, but wrap it in our 'Digit' type.
+--
+--   ==== _Examples_
+--
+--   >>> import Text.Parsec ( parseTest )
+--
+--   Digits are parsed correctly:
+--
+--   >>> parseTest digit "3"
+--   Digit '3'
+--
+--   But letters are not:
+--
+--   >>> parseTest digit "x"
+--   parse error at (line 1, column 1):
+--   unexpected "x"
+--   expecting digit
+--
+digit :: Parser Digit
+digit = fmap Digit Parsec.digit