module Data.Icao.F9
( AircraftType
, WakeTurbulenceCategory(..)
, Data(acNbr, acType, wtc)
, mkAircraftType
, acParser
, parser
) where
import Control.Monad.Fail
import Data.Aeromess.Parser
import Data.Char
import Data.Maybe (fromMaybe)
import Prelude hiding (fail)
newtype AircraftType =
AircraftType String
deriving (Eq, Show)
data WakeTurbulenceCategory
= Light
| Medium
| Heavy
| Jumbo
deriving (Bounded, Enum, Eq, Read, Show)
data Data = Data
{ acNbr :: Int
, acType :: AircraftType
, wtc :: WakeTurbulenceCategory
}
mkAircraftType
:: (MonadFail m)
=> String -> m AircraftType
mkAircraftType s
| length s < 2 || length s > 4 = fail "aircraft type must be 2-4 characters"
| not (all (\c -> isDigit c || isUpper c) s) = fail ("invalid aircraft type=" ++ s)
| otherwise = return (AircraftType s)
acParser :: Parser (Int, AircraftType)
acParser = do
n <- optional (try (natural 1 <|> natural 2))
a <- identifier >>= mkAircraftType
return (fromMaybe 1 n, a)
wtcParser :: Parser WakeTurbulenceCategory
wtcParser = do
c <- oneOf "LMHJ"
return $
case c of
'L' -> Light
'M' -> Medium
'H' -> Heavy
'J' -> Jumbo
_ -> error "?"
parser :: Parser Data
parser = do
na <- acParser
w <- wtcParser
return (uncurry Data na w)