]> gitweb.michael.orlitzky.com - mailbox-count.git/blob - src/Configuration.hs
Make all connection string parameters optional.
[mailbox-count.git] / src / Configuration.hs
1 -- | This module defines the 'Configuration' type, which is just a
2 -- wrapper around all of the configuration options we accept on the
3 -- command line.
4 --
5 module Configuration (
6 Configuration(..),
7 merge_optional )
8 where
9
10 import System.Console.CmdArgs.Default ( Default(..) )
11
12 import qualified OptionalConfiguration as OC (
13 OptionalConfiguration(..),
14 merge_maybes )
15
16 -- | The main configuration data type. This will be passed to most of
17 -- the important functions once it has been created.
18 --
19 data Configuration =
20 Configuration {
21 both :: Bool,
22 database :: Maybe String,
23 detail :: Bool,
24 host :: Maybe String,
25 password :: Maybe String,
26 port :: Maybe Int,
27 username :: Maybe String }
28 deriving (Show)
29
30 -- | A Configuration with all of its fields set to their default
31 -- values.
32 instance Default Configuration where
33 def = Configuration {
34 both = def,
35 database = def,
36 detail = def,
37 host = def,
38 password = def,
39 port = def,
40 username = def }
41
42 -- | Merge a 'Configuration' with an 'OptionalConfiguration'. This is
43 -- more or less the Monoid instance for 'OptionalConfiguration', but
44 -- since the two types are different, we have to repeat ourselves.
45 merge_optional :: Configuration
46 -> OC.OptionalConfiguration
47 -> Configuration
48 merge_optional cfg opt_cfg =
49 Configuration
50 (merge (both cfg) (OC.both opt_cfg))
51 (OC.merge_maybes (database cfg) (OC.database opt_cfg))
52 (merge (detail cfg) (OC.detail opt_cfg))
53 (OC.merge_maybes (host cfg) (OC.host opt_cfg))
54 (OC.merge_maybes (password cfg) (OC.password opt_cfg))
55 (OC.merge_maybes (port cfg) (OC.port opt_cfg))
56 (OC.merge_maybes (username cfg) (OC.username opt_cfg))
57 where
58 -- | If the thing on the right is Just something, return that
59 -- something, otherwise return the thing on the left.
60 merge :: a -> Maybe a -> a
61 merge x Nothing = x
62 merge _ (Just y) = y