]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/Configuration.hs
Allow "TBA" laps in TSN.XML.AutoRacingSchedule.
[dead/htsn-import.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 -- System imports.
11 import System.Console.CmdArgs.Default ( Default(..) )
12 import System.Log ( Priority( INFO ) )
13
14 -- Local imports.
15 import Backend ( Backend(..) )
16 import ConnectionString ( ConnectionString )
17 import qualified OptionalConfiguration as OC (
18 OptionalConfiguration(..),
19 merge_maybes )
20
21
22 -- | The main configuration data type. It contains all options that
23 -- can be set in a config file or on the command line.
24 --
25 data Configuration =
26 Configuration {
27 backend :: Backend,
28 connection_string :: ConnectionString,
29 log_file :: Maybe FilePath,
30 log_level :: Priority,
31 remove :: Bool,
32 syslog :: Bool }
33 deriving (Show)
34
35 -- | A Configuration with all of its fields set to their default
36 -- values.
37 instance Default Configuration where
38 def = Configuration {
39 backend = def,
40 connection_string = def,
41 log_file = def,
42 log_level = INFO,
43 remove = def,
44 syslog = def }
45
46
47 -- | Merge a Configuration with an OptionalConfiguration. This is more
48 -- or less the Monoid instance for 'OptionalConfiguration', but since
49 -- the two types are different, we have to repeat ourselves.
50 --
51 merge_optional :: Configuration
52 -> OC.OptionalConfiguration
53 -> Configuration
54 merge_optional cfg opt_cfg =
55 Configuration
56 (merge (backend cfg) (OC.backend opt_cfg))
57 (merge (connection_string cfg) (OC.connection_string opt_cfg))
58 (OC.merge_maybes (log_file cfg) (OC.log_file opt_cfg))
59 (merge (log_level cfg) (OC.log_level opt_cfg))
60 (merge (remove cfg) (OC.remove opt_cfg))
61 (merge (syslog cfg) (OC.syslog opt_cfg))
62 where
63 -- | If the thing on the right is Just something, return that
64 -- something, otherwise return the thing on the left.
65 merge :: a -> Maybe a -> a
66 merge x Nothing = x
67 merge _ (Just y) = y
68