{-
	Copyright (C) 2018 Dr. Alistair Ward

	This file is part of BishBosh.

	BishBosh 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.

	BishBosh 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 BishBosh.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]

	* <https://www.chessprogramming.org/Extended_Position_Description>.
-}

module BishBosh.Property.ExtendedPositionDescription(
-- * Types
-- ** Type-synonyms
	EPD,
-- * Type-classes
	ReadsEPD(..),
	ShowsEPD(..),
-- * Constants
	tag,
	rankSeparator,
	showsNullField,
	showsSeparator,
-- * Functions
	readEPD,
	showEPD
) where

-- | An alternative to 'Read'.
class ReadsEPD a where
	readsEPD	:: ReadS a	-- ^ Read a datum from EPD.

-- | An alternative to 'Show'.
class ShowsEPD a where
	showsEPD	:: a -> ShowS	-- ^ Stringify a EPD-datum.

-- | Self-documentation.
type EPD	= String

-- | Input-format.
tag :: String
tag :: String
tag	= String
"epd"

-- | The delimiter between each row of data.
rankSeparator :: Char
rankSeparator :: Char
rankSeparator	= Char
'/'

-- | Read from EPD.
readEPD	:: ReadsEPD a => EPD -> a
readEPD :: String -> a
readEPD String
epd	= case ReadS a
forall a. ReadsEPD a => ReadS a
readsEPD String
epd of
	[(a
a, String
_)]	-> a
a
	[(a, String)]
_		-> String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> (String -> String) -> String -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
"BishBosh.Property.ExtendedPositionDescription.readEPD:\tfailed to parse " (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String -> String -> String
forall a. Show a => a -> String -> String
shows String
epd String
"."

-- | Display in EPD.
showEPD	:: ShowsEPD a => a -> EPD
showEPD :: a -> String
showEPD a
a	= a -> String -> String
forall a. ShowsEPD a => a -> String -> String
showsEPD a
a String
""

-- | The standard way to denote the absence of a field.
showsNullField :: ShowS
showsNullField :: String -> String
showsNullField	= Char -> String -> String
showChar Char
'-'

-- | The standard separator between fields in EPD.
showsSeparator :: ShowS
showsSeparator :: String -> String
showsSeparator	= Char -> String -> String
showChar Char
' '