module Data.Icao.F7
( AircraftIdentification
, SsrCode
, Data(aircraftIdentification, ssrCode)
, mkAircraftIdentification
, mkSsrCode
, parser
) where
import Control.Monad.Fail
import Data.Aeromess.Parser
import Data.Char
import Data.Maybe ()
import Prelude hiding (fail)
newtype AircraftIdentification =
AircraftIdentification String
deriving (Eq, Show)
newtype SsrCode =
SsrCode String
deriving (Eq, Show)
data Data = Data
{ aircraftIdentification :: AircraftIdentification
, ssrCode :: Maybe SsrCode
}
ssrCodeParser' :: Parser SsrCode
ssrCodeParser' = do
_ <- char 'A'
fmap SsrCode (octal 4)
ssrCodeParser :: Parser (Maybe SsrCode)
ssrCodeParser = optional (slash >> ssrCodeParser')
acIdParser :: Parser AircraftIdentification
acIdParser = do
r <- identifier
mkAircraftIdentification r
parser :: Parser Data
parser = do
acId <- acIdParser
code <- ssrCodeParser
_ <- dash
return (Data acId code)
mkAircraftIdentification
:: (MonadFail m)
=> String -> m AircraftIdentification
mkAircraftIdentification s
| null s = fail "empty aicraft identification"
| length s > 7 = fail "max aircraft identification length (7) exceed"
| not (all (\c -> isDigit c || isUpper c) s) = fail ("invalid aircraft identification=" ++ s)
| otherwise = return (AircraftIdentification s)
validCode :: String -> Bool
validCode c
| length c /= 4 = False
| all isOctDigit c = True
| otherwise = False
mkSsrCode
:: (MonadFail m)
=> String -> m SsrCode
mkSsrCode c
| validCode c = return (SsrCode c)
| otherwise = fail ("Invalid SSR code=" ++ c ++ ", expected 4 octal digits")