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