packages feed

htsn-import-0.0.1: src/ConnectionString.hs

{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Definition of and instances for the ConnectionString type. This
--   type is simply a wrapper around a 'String', but the newtype
--   allows us to give it a separate 'Default' instance.
--
module ConnectionString (
  ConnectionString(..) )
where

-- System imports.
-- 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