8edb167e30685abbd37b777dde3506e19bda7243
[dead/harbl.git] / harbl-cli / src / Lists.hs
1 -- | A newtype around a list of Strings representing blacklists.
2 -- This is all to avoid an orphan instance of 'Configured' for
3 -- [String] if we had defined one in e.g. 'OptionalConfiguration'.
4 --
5 {-# LANGUAGE DeriveDataTypeable #-}
6
7 module Lists ( Lists(..) )
8 where
9
10 -- DC is needed only for the DCT.Configured instance of String.
11 import qualified Data.Configurator as DC()
12 import qualified Data.Configurator.Types as DCT (
13 Configured,
14 Value( List ),
15 convert )
16 import Data.Data ( Data )
17 import System.Console.CmdArgs.Default ( Default(..) )
18 import Data.Typeable ( Typeable )
19
20
21 -- | A (wrapper around a) list of blacklists.
22 --
23 newtype Lists =
24 Lists { get_lists :: [String] }
25 deriving (Data, Show, Typeable)
26
27
28 -- | The default list of white/blacklists. It's empty.
29 --
30 instance Default Lists where def = Lists []
31
32 instance DCT.Configured Lists where
33 -- | This allows us to read a 'Lists' object out of a Configurator
34 -- config file. By default Configurator wouldn't know what to do,
35 -- so we have to tell it that we expect a list, and if that list
36 -- has strings in it, we can apply the Lists constructor to
37 -- it.
38 convert (DCT.List xs) =
39 -- mapM gives us a Maybe [String] here.
40 fmap Lists (mapM convert_string xs)
41 where
42 convert_string :: DCT.Value -> Maybe String
43 convert_string = DCT.convert
44
45 -- If we read anything other than a list of values out of the file,
46 -- fail.
47 convert _ = Nothing