1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE OverloadedStrings #-}
4 -- | Definition of and instances for the ConnectionString type.
6 module ConnectionString (
10 -- DC is needed only for the DCT.Configured instance of String.
11 import qualified Data.Configurator as DC()
12 import qualified Data.Configurator.Types as DCT (
16 import Data.Data (Data)
17 import System.Console.CmdArgs.Default (Default(..))
18 import Data.Typeable (Typeable)
20 -- | A newtype around a string that allows us to give a more
21 -- appropriate default value for a connection string.
23 newtype ConnectionString =
24 ConnectionString { get_connection_string :: String }
25 deriving (Data, Show, Typeable)
27 instance Default ConnectionString where
28 -- | This default is appropriate for SQLite databases which require
29 -- no authentication and live entirely in a file (or in this case,
31 def = ConnectionString ":memory:"
34 instance DCT.Configured ConnectionString where
35 -- | This allows us to read a ConnectionString out of a Configurator
36 -- config file. By default Configurator wouldn't know what to do,
37 -- so we have to tell it that we expect a DCT.String, and if one
38 -- exists, to apply the ConnectionString constructor to it.
39 convert s@(DCT.String _) =
40 fmap ConnectionString (convert_string s)
42 convert_string :: DCT.Value -> Maybe String
43 convert_string = DCT.convert
45 -- If we read anything other than a DCT.String out of the file, fail.