]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/ConnectionString.hs
Add SportInfo support for WNBABlocksXML.dtd.
[dead/htsn-import.git] / src / ConnectionString.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE OverloadedStrings #-}
3
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.
7 --
8 module ConnectionString (
9 ConnectionString(..) )
10 where
11
12 -- System imports.
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 (
16 Configured,
17 Value( String ),
18 convert )
19 import Data.Data (Data)
20 import System.Console.CmdArgs.Default (Default(..))
21 import Data.Typeable (Typeable)
22
23
24 -- | A newtype around a string that allows us to give a more
25 -- appropriate default value for a connection string.
26 --
27 newtype ConnectionString =
28 ConnectionString { get_connection_string :: String }
29 deriving (Data, Show, Typeable)
30
31
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,
35 -- memory).
36 def = ConnectionString ":memory:"
37
38
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)
46 where
47 convert_string :: DCT.Value -> Maybe String
48 convert_string = DCT.convert
49
50 -- If we read anything other than a DCT.String out of the file, fail.
51 convert _ = Nothing