{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Data.CAProvinceCodes
( Code(..)
, all
, toName
, fromName
, isProvince
, isTerritory
)
where
import Data.Aeson ( ToJSON
, FromJSON
)
import qualified Data.Text as T
import GHC.Generics ( Generic )
import Prelude hiding ( all )
data Code
= AB
| BC
| MB
| NB
| NL
| NS
| NT
| NU
| ON
| PE
| QC
| SK
| YT
deriving (Show, Read, Eq, Enum, Bounded, Generic)
instance ToJSON Code
instance FromJSON Code
all :: [Code]
all = enumFrom minBound
toName :: Code -> T.Text
toName c = case c of
AB -> "Alberta"
BC -> "British Columbia"
MB -> "Manitoba"
NB -> "New Brunswick"
NL -> "Newfoundland And Labrador"
NS -> "Nova Scotia"
NT -> "Northwest Territories"
NU -> "Nunavut"
ON -> "Ontario"
PE -> "Prince Edward Island"
QC -> "Quebec"
SK -> "Saskatchewan"
YT -> "Yukon"
fromName :: T.Text -> Maybe Code
fromName n = case T.toLower n of
"alberta" -> Just AB
"british columbia" -> Just BC
"manitoba" -> Just MB
"new brunswick" -> Just NB
"newfoundland and labrador" -> Just NL
"nova scotia" -> Just NS
"northwest territories" -> Just NT
"nunavut" -> Just NU
"ontario" -> Just ON
"prince edward island" -> Just PE
"quebec" -> Just QC
"saskatchewan" -> Just SK
"yukon" -> Just YT
_ -> Nothing
isProvince :: Code -> Bool
isProvince = not . isTerritory
isTerritory :: Code -> Bool
isTerritory = (`elem` [NT, NU, YT])