]> gitweb.michael.orlitzky.com - mailbox-count.git/blobdiff - src/OptionalConfiguration.hs
mailbox-count.cabal: bump to version 0.0.8
[mailbox-count.git] / src / OptionalConfiguration.hs
index d2b6a051a3e8593024c7c9dceffadf41a4fa9e2e..95a6fdba4af28140ed6fd64a5ac9a06a47e9b6f9 100644 (file)
@@ -10,7 +10,8 @@
 
 module OptionalConfiguration (
   OptionalConfiguration(..),
-  from_rc )
+  from_rc,
+  merge_maybes )
 where
 
 import qualified Data.Configurator as DC (
@@ -18,8 +19,6 @@ import qualified Data.Configurator as DC (
   load,
   lookup )
 import Data.Data ( Data )
-import Data.Typeable ( Typeable )
-import Data.Monoid ( Monoid(..) )
 import Paths_mailbox_count ( getSysconfDir )
 import System.Directory ( getHomeDirectory )
 import System.FilePath ( (</>) )
@@ -33,14 +32,15 @@ import System.IO.Error ( catchIOError )
 --
 data OptionalConfiguration =
   OptionalConfiguration {
-    both     :: Maybe Bool,
     database :: Maybe String,
     detail   :: Maybe Bool,
+    detail_query :: Maybe String,
     host     :: Maybe String,
     password :: Maybe String,
     port     :: Maybe Int,
+    summary_query :: Maybe String,
     username :: Maybe String }
-  deriving (Show, Data, Typeable)
+  deriving (Show, Data)
 
 
 -- | Combine two Maybes into one, essentially mashing them
@@ -53,14 +53,30 @@ merge_maybes Nothing (Just x)  = Just x
 merge_maybes (Just _) (Just y) = Just y
 
 
--- | The Monoid instance for these lets us "combine" two
---   OptionalConfigurations. The "combine" operation that we'd like to
---   perform is, essentially, to mash them together. So if we have two
+-- | The Semigroup instance for these lets us "combine" two
+--   configurations. The "combine" operation that we'd like to perform
+--   is, essentially, to mash them together. So if we have two
 --   OptionalConfigurations, each half full, we could combine them
 --   into one big one.
 --
 --   This is used to merge command-line and config-file settings.
 --
+instance Semigroup OptionalConfiguration where
+  -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
+  cfg1 <> cfg2 =
+    OptionalConfiguration
+      (merge_maybes (database cfg1) (database cfg2))
+      (merge_maybes (detail cfg1) (detail cfg2))
+      (merge_maybes (detail_query cfg1) (detail_query cfg2))
+      (merge_maybes (host cfg1) (host cfg2))
+      (merge_maybes (password cfg1) (password cfg2))
+      (merge_maybes (port cfg1) (port cfg2))
+      (merge_maybes (summary_query cfg1) (summary_query cfg2))
+      (merge_maybes (username cfg1) (username cfg2))
+
+
+-- | The Monoid instance essentially only provides the "empty
+--   configuration."
 instance Monoid OptionalConfiguration where
   -- | An empty OptionalConfiguration.
   mempty = OptionalConfiguration
@@ -71,17 +87,8 @@ instance Monoid OptionalConfiguration where
              Nothing
              Nothing
              Nothing
-
-  -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
-  cfg1 `mappend` cfg2 =
-    OptionalConfiguration
-      (merge_maybes (both cfg1) (both cfg2))
-      (merge_maybes (database cfg1) (database cfg2))
-      (merge_maybes (detail cfg1) (detail cfg2))
-      (merge_maybes (host cfg1) (host cfg2))
-      (merge_maybes (password cfg1) (password cfg2))
-      (merge_maybes (port cfg1) (port cfg2))
-      (merge_maybes (username cfg1) (username cfg2))
+             Nothing
+  mappend = (<>)
 
 
 -- | Obtain an OptionalConfiguration from mailbox-countrc in either
@@ -106,19 +113,21 @@ from_rc = do
   let user_config_path = home </> ".mailbox-countrc"
   cfg <- DC.load [ DC.Optional global_config_path,
                    DC.Optional user_config_path ]
-  cfg_both <- DC.lookup cfg "both"
   cfg_database <- DC.lookup cfg "database"
   cfg_detail <- DC.lookup cfg "detail"
+  cfg_detail_query <- DC.lookup cfg "detail_query"
   cfg_host <- DC.lookup cfg "host"
   cfg_password <- DC.lookup cfg "password"
   cfg_port <- DC.lookup cfg "port"
+  cfg_summary_query <- DC.lookup cfg "summary_query"
   cfg_username <- DC.lookup cfg "username"
 
   return $ OptionalConfiguration
-             cfg_both
              cfg_database
              cfg_detail
+             cfg_detail_query
              cfg_host
              cfg_password
              cfg_port
+             cfg_summary_query
              cfg_username