X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fharbl.git;a=blobdiff_plain;f=harbl%2Fsrc%2FNetwork%2FDNS%2FRBL%2FWeight.hs;h=db8c7a740b3ce2918a06eaa7cd50daaea9bd4c48;hp=8447ab35cbd5de48411e68520ec9bbc16e440c74;hb=98b9d8768fe78d9948151e499ec52f7f616bd6e9;hpb=b47aaa60a797aee4ecdcd5535ed40c1a7b15ddce diff --git a/harbl/src/Network/DNS/RBL/Weight.hs b/harbl/src/Network/DNS/RBL/Weight.hs index 8447ab3..db8c7a7 100644 --- a/harbl/src/Network/DNS/RBL/Weight.hs +++ b/harbl/src/Network/DNS/RBL/Weight.hs @@ -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.