packages feed

fishfood-0.0.0.1: src/FishFood/Data/CommandOptions.hs

{-
	Copyright (C) 2013 Dr. Alistair Ward

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]

	* Defines options for program-operation.

	* Defines an appropriate default value, which is expected to be over-ridden on the command-line.

	* Self-validates.
-}

module FishFood.Data.CommandOptions(
-- * Types
-- ** Data-types
	CommandOptions(
--		MkCommandOptions,
		getBinSize,
		getIncludeEmpty,
		getNDecimalDigits,
		getVerbosity
	)
) where

import qualified	Data.Maybe
import qualified	Distribution.Verbosity
import qualified	FishFood.Data.File	as Data.File
import qualified	ToolShed.Defaultable
import qualified	ToolShed.SelfValidate

-- | Declares a record to contain command-line options.
data CommandOptions	= MkCommandOptions {
	getBinSize		:: Maybe Data.File.FileSize,		-- ^ The intervals into which file-sizes are categorised.
	getIncludeEmpty		:: Bool,				-- ^ Whether to display empty bins.
	getNDecimalDigits	:: Int,					-- ^ The precision to which fractional auxiliary data is displayed.
	getVerbosity		:: Distribution.Verbosity.Verbosity	-- ^ The threshold for ancillary information-output.
} deriving Show

instance ToolShed.Defaultable.Defaultable CommandOptions	where
	defaultValue	= MkCommandOptions {
		getBinSize		= Nothing,	--Interpreted as one standard-deviation.
		getIncludeEmpty		= False,
		getNDecimalDigits	= 3,
		getVerbosity		= Distribution.Verbosity.normal
	}

instance ToolShed.SelfValidate.SelfValidator CommandOptions	where
	getErrors commandOptions@MkCommandOptions {
		getBinSize		= binSize,
		getNDecimalDigits	= nDecimalDigits
	} = map snd $ filter fst [
		(
			Data.Maybe.maybe False (<= 0) binSize,
			"the bin-size must exceed zero; " ++ show commandOptions ++ "."
		), (
			nDecimalDigits < 1,
			"the number of decimal digits must exceed zero; " ++ show commandOptions ++ "."
		),
		let
			maxNDecimalDigits	= floor $ fromIntegral (
				floatDigits (
					undefined :: Double	--CAVEAT: the actual type could be merely 'Float', but that's currently unknown.
				)
			 ) * (logBase 10 2 :: Double)
		in (
			nDecimalDigits > maxNDecimalDigits,
			"the number of decimal digits shouldn't exceed " ++ show maxNDecimalDigits ++ "; " ++ show commandOptions ++ "."
		)
	 ]