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