+{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
-- | The 'Weight' type, its instances, and a Parsec parser to parse
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 (
(<|>),
-- >>> 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'.
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.