]> gitweb.michael.orlitzky.com - dead/harbl.git/blobdiff - harbl/src/Network/DNS/RBL/Weight.hs
Add a "threshold" to the configuration.
[dead/harbl.git] / harbl / src / Network / DNS / RBL / Weight.hs
index 8447ab35cbd5de48411e68520ec9bbc16e440c74..db8c7a740b3ce2918a06eaa7cd50daaea9bd4c48 100644 (file)
@@ -1,3 +1,4 @@
+{-# LANGUAGE DeriveDataTypeable #-}
 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
 
 -- | The 'Weight' type, its instances, and a Parsec parser to parse
@@ -15,6 +16,11 @@ module Network.DNS.RBL.Weight (
   weight )
 where
 
+import Data.Configurator () -- Needed for predefined instances.
+import Data.Configurator.Types ( Configured(..), Value( Number ), convert )
+import Data.Data ( Data )
+import Data.Ratio ( numerator )
+import Data.Typeable ( Typeable )
 import System.Console.CmdArgs.Default ( Default(..) )
 import Text.Parsec (
   (<|>),
@@ -48,7 +54,7 @@ import Network.DNS.RBL.Pretty ( Pretty(..) )
 --   >>> sum [w1, w2, w3]
 --   Weight 4
 --
-newtype Weight = Weight Int deriving (Eq, Num, Ord, Show)
+newtype Weight = Weight Int deriving (Data, Eq, Num, Ord, Show, Typeable)
 
 
 -- | Pretty-print a 'Weight'. This just shows/prints the underlying 'Int'.
@@ -74,6 +80,27 @@ instance Pretty Weight where
 instance Default Weight where def = 1
 
 
+-- | Allow the configurator library to parse a 'Weight' from a config
+--   file.
+--
+--   ==== _Examples_
+--
+--   >>> import Data.Configurator () -- Get predefined 'Configured' instances.
+--   >>> import Data.Text ( pack )
+--   >>> import Data.Configurator.Types ( Value( Number, String ) )
+--   >>> let n1 = Number 2
+--   >>> convert n1 :: Maybe Weight
+--   Just (Weight 2)
+--   >>> let s = String (pack "foo1")
+--   >>> convert s :: Maybe Weight
+--   Nothing
+--
+instance Configured Weight where
+  -- Don't give us a fractional weight, we'll ignore the denominator.
+  convert (Number x) = Just (Weight (fromInteger $ numerator x))
+  convert _          = Nothing
+
+
 -- | Parse the weight multiplier off the end of an input 'Site'. This
 --   expects there to be a \"multiplier\" character (an asterisk)
 --   before the integral weight.