1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
4 -- | The 'Weight' type, its instances, and a Parsec parser to parse
5 -- one off the end of a 'Site'.
7 -- This is a simple newtype wrapper around an 'Int', meant to be
8 -- used wherever a weight or an RBL score is intended (to prevent
9 -- integer mixups). For example, the RBLs are all weighted with
10 -- 'Weight's, and when a user supplied a \"badness\" threshold, it
11 -- will be as a 'Weight' as well. The two are then comparable but
12 -- not with other 'Int's.
14 module Network.DNS.RBL.Weight (
19 import Data.Configurator () -- Needed for predefined instances.
20 import Data.Configurator.Types ( Configured(..), Value( Number ), convert )
21 import Data.Data ( Data )
22 import Data.Ratio ( numerator )
23 import Data.Typeable ( Typeable )
24 import System.Console.CmdArgs.Default ( Default(..) )
33 import Text.Parsec.String ( Parser )
34 import Text.Read ( readMaybe )
36 import Network.DNS.RBL.Pretty ( Pretty(..) )
39 -- | The 'Weight' wrapper around an 'Int. We use
40 -- GeneralizedNewtypeDeriving to derive num automatically (so that we
41 -- can sum these things).
45 -- >>> let w1 = Weight 1
48 -- >>> let w2 = Weight 1
51 -- >>> let w3 = Weight 2
54 -- >>> sum [w1, w2, w3]
57 newtype Weight = Weight Int deriving (Data, Eq, Num, Ord, Show, Typeable)
60 -- | Pretty-print a 'Weight'. This just shows/prints the underlying 'Int'.
64 -- >>> pretty_print $ Weight 17
67 instance Pretty Weight where
68 pretty_show (Weight w) = show w
71 -- | Set a default value for the weight. We use the 'Default' class
72 -- from the CmdArgs class so that 'Weight's can be easily parsed on
80 instance Default Weight where def = 1
83 -- | Allow the configurator library to parse a 'Weight' from a config
88 -- >>> import Data.Configurator () -- Get predefined 'Configured' instances.
89 -- >>> import Data.Text ( pack )
90 -- >>> import Data.Configurator.Types ( Value( Number, String ) )
91 -- >>> let n1 = Number 2
92 -- >>> convert n1 :: Maybe Weight
94 -- >>> let s = String (pack "foo1")
95 -- >>> convert s :: Maybe Weight
98 instance Configured Weight where
99 -- Don't give us a fractional weight, we'll ignore the denominator.
100 convert (Number x) = Just (Weight (fromInteger $ numerator x))
104 -- | Parse the weight multiplier off the end of an input 'Site'. This
105 -- expects there to be a \"multiplier\" character (an asterisk)
106 -- before the integral weight.
110 -- >>> import Text.Parsec ( parseTest )
112 -- Negative, zero, and positive integers are all supported:
114 -- >>> parseTest weight "*-5"
117 -- >>> parseTest weight "*0"
120 -- >>> parseTest weight "*17"
123 -- If the weight is empty, it defaults to @1@:
125 -- >>> parseTest weight ""
128 -- The default is used whenever parsing fails:
130 -- >>> parseTest weight "*hello"
133 -- The 'Pretty' instance works as intended:
135 -- >>> import Text.Parsec ( parse )
136 -- >>> pretty_print $ parse weight "" "*3"
139 weight :: Parser Weight
140 weight = try parse_weight <|> return (Weight 1)
144 sign <- (char '-') <|> (option '+' (char '+'))
146 case ( readMaybe w :: Maybe Int ) of
147 -- If "many1 digit" gives us a list of digits, we should be able
148 -- to convert that to an Int! It will overflow rather than fail
149 -- if the input is too big/small, so it should really always
151 Nothing -> unexpected "weight: readMaybe failed on a sequence of digits!"
152 Just k -> return $ Weight (if sign == '-' then negate k else k)