module Referees.Types.Internal where
import Referees.Solver.Types ( Capacity )
import Control.Applicative ( pure, (<$>), (<*>) )
import Control.Monad ( mzero )
import qualified Data.ByteString.Char8 as BC ( split, unpack )
import Data.Csv
( FromField(..), FromRecord(..), Parser, runParser, (.!) )
import Data.Vector as V ( length )
type Name = String
type Language = String
type Area = String
type Subarea = String
data Entry subtype = Entry
{ _name :: Name
, _capacity :: Maybe Capacity
, _languages :: [Language]
, _areas :: [(Area, [Subarea])]
} deriving (Eq, Show)
data Referee = Referee
data Proposal = Proposal
type Match = (Entry Referee, [Entry Proposal])
data CSV_Warning = DifferingCapacities Name
newtype StringListParse sep = StrLstP
{ _strLstP :: [String]
} deriving (Eq, Show)
data Semicolon = Semicolon
newtype MaybeCapacityParse = MaybeCapP
{ _maybeCapP :: Maybe Capacity
} deriving (Eq, Show)
data CSVentry = CSVentry
{ _nameCSV :: !Name
, _capacityCSV :: !MaybeCapacityParse
, _languagesCSV :: !(StringListParse Semicolon)
, _areaCSV :: !Area
, _subareasCSV :: !(StringListParse Semicolon) }
instance FromField (StringListParse Semicolon) where
parseField s =
either
(const . pure . StrLstP . map BC.unpack . BC.split ';' $ s)
(const . pure . StrLstP $ [])
$ runParser (parseField s :: Parser Int)
instance FromField MaybeCapacityParse where
parseField s =
either
(const . pure $ MaybeCapP Nothing)
(pure . MaybeCapP . Just)
$ runParser (parseField s :: Parser Int)
instance FromRecord CSVentry where
parseRecord v
| V.length v == 2 = CSVentry <$>
v .! 0 <*>
pure (MaybeCapP Nothing) <*>
pure (StrLstP []) <*>
v .! 1 <*>
pure (StrLstP [])
| V.length v == 3 = CSVentry <$>
v .! 0 <*>
pure (MaybeCapP Nothing) <*>
pure (StrLstP []) <*>
v .! 1 <*>
v .! 2
| V.length v == 4 = CSVentry <$>
v .! 0 <*>
v .! 1 <*>
(v .! 1 :: Parser (StringListParse Semicolon)) <*>
v .! 2 <*>
v .! 3
| V.length v == 5 = CSVentry <$>
v .! 0 <*>
v .! 1 <*>
v .! 2 <*>
v .! 3 <*>
v .! 4
| otherwise = mzero