module BishBosh.Input.IOOptions(
MaximumPGNNames,
IOOptions(
getMaybeOutputConfigFilePath,
getMaybeMaximumPGNNames,
getPGNOptionsList,
getMaybePersistence,
getUIOptions
),
tag,
outputConfigFilePathTag,
mkIOOptions,
setMaybeOutputConfigFilePath,
setEitherNativeUIOrCECPOptions,
setMaybePrintMoveTree,
updateCECPFeature,
deleteCECPFeature,
setVerbosity
) where
import qualified BishBosh.Data.Exception as Data.Exception
import qualified BishBosh.Input.CECPFeatures as Input.CECPFeatures
import qualified BishBosh.Input.PGNOptions as Input.PGNOptions
import qualified BishBosh.Input.UIOptions as Input.UIOptions
import qualified BishBosh.Property.Tree as Property.Tree
import qualified BishBosh.Text.ShowList as Text.ShowList
import qualified Control.Arrow
import qualified Control.DeepSeq
import qualified Control.Exception
import qualified Data.Default
import qualified Data.Maybe
import qualified Distribution.Verbosity
import qualified System.FilePath
import qualified Text.XML.HXT.Arrow.Pickle as HXT
import qualified ToolShed.Data.Foldable
tag :: String
tag = "ioOptions"
maximumPGNNamesTag :: String
maximumPGNNamesTag = "maximumPGNNames"
outputConfigFilePathTag :: String
outputConfigFilePathTag = "outputConfigFilePath"
persistenceTag :: String
persistenceTag = "persistence"
filePathTag :: String
filePathTag = "filePath"
automaticTag :: String
automaticTag = "automatic"
type MaximumPGNNames = Int
data IOOptions row column = MkIOOptions {
getMaybeOutputConfigFilePath :: Maybe System.FilePath.FilePath,
getMaybeMaximumPGNNames :: Maybe MaximumPGNNames,
getPGNOptionsList :: [Input.PGNOptions.PGNOptions],
getMaybePersistence :: Maybe (System.FilePath.FilePath, Bool),
getUIOptions :: Input.UIOptions.UIOptions row column
} deriving Eq
instance (
Control.DeepSeq.NFData column,
Control.DeepSeq.NFData row
) => Control.DeepSeq.NFData (IOOptions row column) where
rnf MkIOOptions {
getMaybeOutputConfigFilePath = maybeOutputConfigFilePath,
getMaybeMaximumPGNNames = maybeMaximumPGNNames,
getPGNOptionsList = pgnOptionsList,
getMaybePersistence = maybePersistence,
getUIOptions = uiOptions
} = Control.DeepSeq.rnf (
maybeOutputConfigFilePath,
maybeMaximumPGNNames,
pgnOptionsList,
maybePersistence,
uiOptions
)
instance (Show column, Show row) => Show (IOOptions row column) where
showsPrec _ MkIOOptions {
getMaybeOutputConfigFilePath = maybeOutputConfigFilePath,
getMaybeMaximumPGNNames = maybeMaximumPGNNames,
getPGNOptionsList = pgnOptionsList,
getMaybePersistence = maybePersistence,
getUIOptions = uiOptions
} = Text.ShowList.showsAssociationList' . Data.Maybe.maybe id (
(:) . (,) outputConfigFilePathTag . shows
) maybeOutputConfigFilePath . Data.Maybe.maybe id (
(:) . (,) maximumPGNNamesTag . shows
) maybeMaximumPGNNames $ Data.Maybe.maybe id (
(:) . (,) persistenceTag . shows
) maybePersistence [
(
showString Input.PGNOptions.tag "List",
shows pgnOptionsList
), (
Input.UIOptions.tag,
shows uiOptions
)
]
instance (Num column, Num row) => Data.Default.Default (IOOptions row column) where
def = MkIOOptions {
getMaybeOutputConfigFilePath = Nothing,
getMaybeMaximumPGNNames = Nothing,
getPGNOptionsList = [],
getMaybePersistence = Nothing,
getUIOptions = Data.Default.def
}
instance (
HXT.XmlPickler column,
HXT.XmlPickler row,
Integral column,
Integral row,
Show column,
Show row
) => HXT.XmlPickler (IOOptions row column) where
xpickle = HXT.xpDefault Data.Default.def . HXT.xpElem tag . HXT.xpWrap (
\(a, b, c, d, e) -> mkIOOptions a b c d e,
\MkIOOptions {
getMaybeOutputConfigFilePath = maybeOutputConfigFilePath,
getMaybeMaximumPGNNames = maybeMaximumPGNNames,
getPGNOptionsList = pgnOptionsList,
getMaybePersistence = maybePersistence,
getUIOptions = uiOptions
} -> (
maybeOutputConfigFilePath,
maybeMaximumPGNNames,
pgnOptionsList,
maybePersistence,
uiOptions
)
) $ HXT.xp5Tuple (
HXT.xpOption $ HXT.xpTextAttr outputConfigFilePathTag
) (
HXT.xpAttrImplied maximumPGNNamesTag HXT.xpInt
) HXT.xpickle (
HXT.xpOption $ HXT.xpElem persistenceTag (
HXT.xpTextAttr filePathTag `HXT.xpPair` HXT.xpDefault True (HXT.xpAttr automaticTag HXT.xpickle )
)
) HXT.xpickle
mkIOOptions
:: Maybe System.FilePath.FilePath
-> Maybe MaximumPGNNames
-> [Input.PGNOptions.PGNOptions]
-> Maybe (System.FilePath.FilePath, Bool)
-> Input.UIOptions.UIOptions row column
-> IOOptions row column
mkIOOptions maybeOutputConfigFilePath maybeMaximumPGNNames pgnOptionsList maybePersistence uiOptions
| Data.Maybe.maybe False (
not . System.FilePath.isValid
) maybeOutputConfigFilePath = Control.Exception.throw . Data.Exception.mkInvalidDatum . showString "BishBosh.Input.IOOptions.mkIOOptions:\tinvalid " $ showString outputConfigFilePathTag "."
| Data.Maybe.isJust maybeMaximumPGNNames
, null pgnOptionsList = Control.Exception.throw . Data.Exception.mkIncompatibleData . showString "Specification of " $ shows maximumPGNNamesTag " is only irrelevant when at least one PGN-database has been referenced."
| Just maximumPGNNames <- maybeMaximumPGNNames
, maximumPGNNames < 0 = Control.Exception.throw . Data.Exception.mkOutOfBounds . showString "The maximum number of names, of matching PGN-games to display, can't be negative; " $ shows maximumPGNNames "."
| duplicateFilePaths@(_ : _) <- map head . filter ((/= 1) . length) . ToolShed.Data.Foldable.gather $ map (System.FilePath.normalise . Input.PGNOptions.getDatabaseFilePath) pgnOptionsList
= Control.Exception.throw . Data.Exception.mkDuplicateData . showString "BishBosh.Input.IOOptions.mkIOOptions:\tduplicate " . showString Input.PGNOptions.databaseFilePathTag . Text.ShowList.showsAssociation $ shows duplicateFilePaths "."
| Data.Maybe.maybe False (
not . System.FilePath.isValid . fst
) maybePersistence = Control.Exception.throw . Data.Exception.mkInvalidDatum . showString "BishBosh.Input.IOOptions.mkIOOptions:\tinvalid path for " $ showString persistenceTag "."
| otherwise = MkIOOptions {
getMaybeOutputConfigFilePath = System.FilePath.normalise <$> maybeOutputConfigFilePath,
getMaybeMaximumPGNNames = maybeMaximumPGNNames,
getPGNOptionsList = pgnOptionsList,
getMaybePersistence = Control.Arrow.first System.FilePath.normalise <$> maybePersistence,
getUIOptions = uiOptions
}
type Transformation row column = IOOptions row column -> IOOptions row column
setMaybeOutputConfigFilePath :: Maybe System.FilePath.FilePath -> Transformation row column
setMaybeOutputConfigFilePath maybeOutputConfigFilePath ioOptions
| Data.Maybe.maybe False (
not . System.FilePath.isValid
) maybeOutputConfigFilePath = Control.Exception.throw . Data.Exception.mkInvalidDatum . showString "BishBosh.Input.IOOptions.setMaybeOutputConfigFilePath:\tinvalid " $ showString outputConfigFilePathTag "."
| otherwise = ioOptions {
getMaybeOutputConfigFilePath = maybeOutputConfigFilePath
}
setEitherNativeUIOrCECPOptions :: Input.UIOptions.EitherNativeUIOrCECPOptions row column -> Transformation row column
setEitherNativeUIOrCECPOptions eitherNativeUIOrCECPOptions ioOptions@MkIOOptions { getUIOptions = uiOptions } = ioOptions {
getUIOptions = uiOptions {
Input.UIOptions.getEitherNativeUIOrCECPOptions = eitherNativeUIOrCECPOptions
}
}
setMaybePrintMoveTree :: Maybe Property.Tree.Depth -> Transformation row column
setMaybePrintMoveTree maybePrintMoveTree ioOptions@MkIOOptions { getUIOptions = uiOptions } = ioOptions {
getUIOptions = uiOptions {
Input.UIOptions.getMaybePrintMoveTree = maybePrintMoveTree
}
}
updateCECPFeature :: Input.CECPFeatures.Feature -> Transformation row column
updateCECPFeature feature ioOptions@MkIOOptions { getUIOptions = uiOptions } = ioOptions {
getUIOptions = Input.UIOptions.updateCECPFeature feature uiOptions
}
deleteCECPFeature :: Input.CECPFeatures.Feature -> Transformation row column
deleteCECPFeature feature ioOptions@MkIOOptions { getUIOptions = uiOptions } = ioOptions {
getUIOptions = Input.UIOptions.deleteCECPFeature feature uiOptions
}
setVerbosity :: Distribution.Verbosity.Verbosity -> Transformation row column
setVerbosity verbosity ioOptions@MkIOOptions { getUIOptions = uiOptions } = ioOptions {
getUIOptions = uiOptions {
Input.UIOptions.getVerbosity = verbosity
}
}