]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/ConnectionString.hs
Provide a default connection string (for sqlite).
[dead/htsn-import.git] / src / ConnectionString.hs
diff --git a/src/ConnectionString.hs b/src/ConnectionString.hs
new file mode 100644 (file)
index 0000000..26f81d6
--- /dev/null
@@ -0,0 +1,46 @@
+{-# LANGUAGE DeriveDataTypeable #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+-- | Definition of and instances for the ConnectionString type.
+--
+module ConnectionString (
+  ConnectionString(..) )
+where
+
+-- DC is needed only for the DCT.Configured instance of String.
+import qualified Data.Configurator as DC()
+import qualified Data.Configurator.Types as DCT (
+  Configured,
+  Value( String ),
+  convert )
+import Data.Data (Data)
+import System.Console.CmdArgs.Default (Default(..))
+import Data.Typeable (Typeable)
+
+-- | A newtype around a string that allows us to give a more
+--   appropriate default value for a connection string.
+--
+newtype ConnectionString =
+  ConnectionString { get_connection_string :: String }
+  deriving (Data, Show, Typeable)
+
+instance Default ConnectionString where
+  -- | This default is appropriate for SQLite databases which require
+  --   no authentication and live entirely in a file (or in this case,
+  --   memory).
+  def = ConnectionString ":memory:"
+
+
+instance DCT.Configured ConnectionString where
+  -- | This allows us to read a ConnectionString out of a Configurator
+  --   config file. By default Configurator wouldn't know what to do,
+  --   so we have to tell it that we expect a DCT.String, and if one
+  --   exists, to apply the ConnectionString constructor to it.
+  convert s@(DCT.String _) =
+    fmap ConnectionString (convert_string s)
+    where
+      convert_string :: DCT.Value -> Maybe String
+      convert_string = DCT.convert
+
+  -- If we read anything other than a DCT.String out of the file, fail.
+  convert _ = Nothing