X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FConnectionString.hs;fp=src%2FConnectionString.hs;h=26f81d6f620a010987cf16104ad3ed8734d6e0dd;hb=cdd0f36ab4aa8aadc416f6b9cbe6117c26d2ddf2;hp=0000000000000000000000000000000000000000;hpb=ce611b60b9c42e176b215453f0d0f862d2d5d0fd;p=dead%2Fhtsn-import.git diff --git a/src/ConnectionString.hs b/src/ConnectionString.hs new file mode 100644 index 0000000..26f81d6 --- /dev/null +++ b/src/ConnectionString.hs @@ -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