{-# OPTIONS_GHC -fno-warn-orphans #-}

module Data.BCP47.Csv () where

import Data.BCP47 (BCP47, fromText, toText)
import Data.Csv (FromField(..), ToField(..))
import Data.Text (unpack)

instance ToField BCP47 where
  toField :: BCP47 -> Field
toField = forall a. ToField a => a -> Field
toField forall b c a. (b -> c) -> (a -> b) -> a -> c
. BCP47 -> Text
toText

instance FromField BCP47 where
  parseField :: Field -> Parser BCP47
parseField Field
bytes = do
    Text
text <- forall a. FromField a => Field -> Parser a
parseField Field
bytes
    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
unpack) forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Text -> Either Text BCP47
fromText Text
text