]> gitweb.michael.orlitzky.com - dead/harbl.git/blobdiff - harbl/src/Network/DNS/RBL/Domain/Hyphen.hs
Begin moving the name parsers to the Network.DNS.RBL.Domain namespace.
[dead/harbl.git] / harbl / src / Network / DNS / RBL / Domain / Hyphen.hs
diff --git a/harbl/src/Network/DNS/RBL/Domain/Hyphen.hs b/harbl/src/Network/DNS/RBL/Domain/Hyphen.hs
new file mode 100644 (file)
index 0000000..65793ff
--- /dev/null
@@ -0,0 +1,117 @@
+-- | OK, I lied about "Network.DNS.RBL.Domain.Digit" and
+-- "Network.DNS.RBL.Domain.Letter" being the simplest modules you'd
+-- ever see. Because this is. It contains the 'Hyphen' 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.
+--
+module Network.DNS.RBL.Domain.Hyphen (
+  Hyphen,
+  hyphen )
+where
+
+import Text.Parsec ( char )
+import Text.Parsec.String ( Parser )
+
+import Network.DNS.RBL.Pretty ( Pretty(..) )
+
+
+
+-- | A wrapper around a single hyphen character.
+--
+--   ==== _Examples_
+--
+--   >>> Hyphen '-'
+--   Hyphen '-'
+--
+--   >>> let h1 = Hyphen '-'
+--   >>> let h2 = Hyphen '-'
+--   >>> h1 == h2
+--   True
+--
+newtype Hyphen = Hyphen Char
+
+
+-- | Equality is defined semantically (all hyphens are equal).
+--
+--   ==== _Examples_
+--
+--   >>> let h1 = Hyphen '-'
+--   >>> let h2 = Hyphen '-'
+--   >>> h1 == h2
+--   True
+--
+--   If you do something stupid, that's your fault:
+--
+--   >>> let h1 = Hyphen '-'
+--   >>> let h2 = Hyphen 'x'
+--   >>> h1 == h2
+--   True
+--
+instance Eq Hyphen where _ == _ = True
+
+
+-- | 'Show' is defined semantically; all hyphens display as \'-\'.
+--   The implementation is based on what GHC derives, discovered via
+--   @ghci -ddump-deriv@.
+--
+--   ==== _Examples_
+--
+--   >>> let h = Hyphen '-'
+--   >>> h
+--   Hyphen '-'
+--
+--   If you do something stupid, that's your fault:
+--
+--   >>> let h = Hyphen 'x'
+--   >>> h
+--   Hyphen '-'
+--
+instance Show Hyphen where
+  showsPrec d _ =
+    showParen (d > application_precedence) (showString "Hyphen '-'")
+    where
+      application_precedence = 10
+
+
+-- | 'Pretty' is defined semantically; all hyphens display as \'-\'.
+--
+--   ==== _Examples_
+--
+--   >>> let h = Hyphen '-'
+--   >>> pretty_print h
+--   -
+--
+--   If you do something stupid, that's your fault:
+--
+--   >>> let h = Hyphen 'x'
+--   >>> pretty_print h
+--   -
+--
+instance Pretty Hyphen where pretty_show _ = "-"
+
+
+-- | Parse a single hyphen and wrap it in our 'Hyphen' type.
+--
+--   ==== _Examples_
+--
+--   >>> import Text.Parsec ( parseTest )
+--
+--   Hyphens are parsed:
+--
+--   >>> parseTest hyphen "-"
+--   Hyphen '-'
+--
+--   But not letters or digits:
+--
+--   >>> parseTest hyphen "1"
+--   parse error at (line 1, column 1):
+--   unexpected "1"
+--   expecting "-"
+--
+--   >>> parseTest hyphen "x"
+--   parse error at (line 1, column 1):
+--   unexpected "x"
+--   expecting "-"
+--
+hyphen :: Parser Hyphen
+hyphen = fmap Hyphen (char '-')