{-
	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/Forsyth-Edwards_Notation>.

	* <https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation>.
-}

module BishBosh.Property.ForsythEdwards(
-- * Type-classes
	ReadsFEN(..),
	ShowsFEN(..),
-- * Constants
	showsNullField,
	showsSeparator,
-- * Functions
	readFEN,
	showFEN
) where

-- | An alternative to 'Read'.
class ReadsFEN a where
	readsFEN	:: ReadS a	-- ^ Read a datum from FEN.

-- | An alternative to 'Show'.
class ShowsFEN a where
	showsFEN	:: a -> ShowS	-- ^ Stringify a FEN-datum.

-- | Read from FEN.
readFEN	:: ReadsFEN a => String -> a
readFEN :: String -> a
readFEN String
s	= case ReadS a
forall a. ReadsFEN a => ReadS a
readsFEN String
s 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.ForsythEdwards.readFEN:\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
s String
"."

-- | Display in FEN.
showFEN	:: ShowsFEN a => a -> String
showFEN :: a -> String
showFEN a
a	= a -> String -> String
forall a. ShowsFEN a => a -> String -> String
showsFEN 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 FEN.
showsSeparator :: ShowS
showsSeparator :: String -> String
showsSeparator	= Char -> String -> String
showChar Char
' '