{-# 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 = Text -> Field
forall a. ToField a => a -> Field
toField (Text -> Field) -> (BCP47 -> Text) -> BCP47 -> Field
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 <- Field -> Parser Text
forall a. FromField a => Field -> Parser a
parseField Field
bytes
    (Text -> Parser BCP47)
-> (BCP47 -> Parser BCP47) -> Either Text BCP47 -> Parser BCP47
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser BCP47
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser BCP47)
-> (Text -> String) -> Text -> Parser BCP47
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
unpack) BCP47 -> Parser BCP47
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Text BCP47 -> Parser BCP47)
-> Either Text BCP47 -> Parser BCP47
forall a b. (a -> b) -> a -> b
$ Text -> Either Text BCP47
fromText Text
text