{-# LANGUAGE CPP #-}

{- |
   Module     : Data.Convertible.Instances.Text
   Copyright  : Copyright (C) 2011 MailRank, Inc.
   License    : LGPL

   Maintainer : John Goerzen <jgoerzen@complete.org>
   Stability  : provisional
   Portability: portable

Text instances for Convertible.

Copyright (C) 2011 MailRank, Inc. <bos@mailrank.com>

All rights reserved.

For license and copyright information, see the file COPYRIGHT

These instances perform conversion between text-like types such as
Text, ByteString, and the like.

The instances do /not/ include conversions between ByteString and
Text or String, since such conversions cannot safely be performed
without knowing the encoding of the ByteString.
-}

module Data.Convertible.Instances.Text()
where

import Data.Convertible.Base
import qualified Data.Text as TS
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TLB
import qualified Data.Text.Lazy.Encoding as TLE
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Builder as BB
import Data.Word (Word8)
import Data.Foldable



instance Convertible TS.Text [Char] where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult [Char]
safeConvert = [Char] -> ConvertResult [Char]
forall a b. b -> Either a b
Right ([Char] -> ConvertResult [Char])
-> (Text -> [Char]) -> Text -> ConvertResult [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Char]
TS.unpack

instance Convertible TS.Text TL.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. b -> Either a b
Right (Text -> ConvertResult Text)
-> (Text -> Text) -> Text -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.fromStrict

instance Convertible TS.Text TLB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> (Text -> Builder) -> Text -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
TLB.fromText

instance Convertible TS.Text BS.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. b -> Either a b
Right (ByteString -> ConvertResult ByteString)
-> (Text -> ByteString) -> Text -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf8

instance Convertible TS.Text BL.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. b -> Either a b
Right (ByteString -> ConvertResult ByteString)
-> (Text -> ByteString) -> Text -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BL.fromStrict (ByteString -> ByteString)
-> (Text -> ByteString) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf8

instance Convertible TS.Text BB.Builder where
    {-# INLINE safeConvert #-}
#if MIN_VERSION_text(1,2,0)
    safeConvert :: Text -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> (Text -> Builder) -> Text -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
TE.encodeUtf8Builder
#else
    safeConvert = safeConvert . TE.encodeUtf8
#endif



instance Convertible TL.Text [Char] where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult [Char]
safeConvert = [Char] -> ConvertResult [Char]
forall a b. b -> Either a b
Right ([Char] -> ConvertResult [Char])
-> (Text -> [Char]) -> Text -> ConvertResult [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Char]
TL.unpack

instance Convertible TL.Text TS.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. b -> Either a b
Right (Text -> ConvertResult Text)
-> (Text -> Text) -> Text -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.toStrict

instance Convertible TL.Text TLB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> (Text -> Builder) -> Text -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
TLB.fromLazyText

instance Convertible TL.Text BS.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. Convertible a b => a -> ConvertResult b
safeConvert (ByteString -> ConvertResult ByteString)
-> (Text -> ByteString) -> Text -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TLE.encodeUtf8

instance Convertible TL.Text BL.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: Text -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. b -> Either a b
Right (ByteString -> ConvertResult ByteString)
-> (Text -> ByteString) -> Text -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TLE.encodeUtf8

instance Convertible TL.Text BB.Builder where
    {-# INLINE safeConvert #-}
#if MIN_VERSION_text(1,2,0)
    safeConvert :: Text -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> (Text -> Builder) -> Text -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
TLE.encodeUtf8Builder
#else
    safeConvert = safeConvert . TLE.encodeUtf8
#endif



instance Convertible TLB.Builder [Char] where
    {-# INLINE safeConvert #-}
    safeConvert :: Builder -> ConvertResult [Char]
safeConvert = Text -> ConvertResult [Char]
forall a b. Convertible a b => a -> ConvertResult b
safeConvert (Text -> ConvertResult [Char])
-> (Builder -> Text) -> Builder -> ConvertResult [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TLB.toLazyText

instance Convertible TLB.Builder TS.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: Builder -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. Convertible a b => a -> ConvertResult b
safeConvert (Text -> ConvertResult Text)
-> (Builder -> Text) -> Builder -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TLB.toLazyText

instance Convertible TLB.Builder TL.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: Builder -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. b -> Either a b
Right (Text -> ConvertResult Text)
-> (Builder -> Text) -> Builder -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TLB.toLazyText

instance Convertible TLB.Builder BS.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: Builder -> ConvertResult ByteString
safeConvert = Text -> ConvertResult ByteString
forall a b. Convertible a b => a -> ConvertResult b
safeConvert (Text -> ConvertResult ByteString)
-> (Builder -> Text) -> Builder -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TLB.toLazyText

instance Convertible TLB.Builder BL.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: Builder -> ConvertResult ByteString
safeConvert = Text -> ConvertResult ByteString
forall a b. Convertible a b => a -> ConvertResult b
safeConvert (Text -> ConvertResult ByteString)
-> (Builder -> Text) -> Builder -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TLB.toLazyText

instance Convertible TLB.Builder BB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: Builder -> ConvertResult Builder
safeConvert = Text -> ConvertResult Builder
forall a b. Convertible a b => a -> ConvertResult b
safeConvert (Text -> ConvertResult Builder)
-> (Builder -> Text) -> Builder -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TLB.toLazyText



instance Convertible BS.ByteString [Word8] where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult [Word8]
safeConvert = [Word8] -> ConvertResult [Word8]
forall a b. b -> Either a b
Right ([Word8] -> ConvertResult [Word8])
-> (ByteString -> [Word8]) -> ByteString -> ConvertResult [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
BS.unpack

instance Convertible BS.ByteString TS.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. b -> Either a b
Right (Text -> ConvertResult Text)
-> (ByteString -> Text) -> ByteString -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
TE.decodeUtf8

instance Convertible BS.ByteString TL.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult Text
safeConvert = (Text -> Text) -> ConvertResult Text -> ConvertResult Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
TL.fromStrict (ConvertResult Text -> ConvertResult Text)
-> (ByteString -> ConvertResult Text)
-> ByteString
-> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ConvertResult Text
forall a b. Convertible a b => a -> ConvertResult b
safeConvert

instance Convertible BS.ByteString TLB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult Builder
safeConvert = (Text -> Builder) -> ConvertResult Text -> ConvertResult Builder
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Builder
TLB.fromText (ConvertResult Text -> ConvertResult Builder)
-> (ByteString -> ConvertResult Text)
-> ByteString
-> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ConvertResult Text
forall a b. Convertible a b => a -> ConvertResult b
safeConvert

instance Convertible BS.ByteString BL.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. b -> Either a b
Right (ByteString -> ConvertResult ByteString)
-> (ByteString -> ByteString)
-> ByteString
-> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BL.fromStrict

instance Convertible BS.ByteString BB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> (ByteString -> Builder) -> ByteString -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Builder
BB.byteString



instance Convertible BL.ByteString [Word8] where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult [Word8]
safeConvert = [Word8] -> ConvertResult [Word8]
forall a b. b -> Either a b
Right ([Word8] -> ConvertResult [Word8])
-> (ByteString -> [Word8]) -> ByteString -> ConvertResult [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
BL.unpack

instance Convertible BL.ByteString TS.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult Text
safeConvert = (Text -> Text) -> ConvertResult Text -> ConvertResult Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
TL.toStrict (ConvertResult Text -> ConvertResult Text)
-> (ByteString -> ConvertResult Text)
-> ByteString
-> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ConvertResult Text
forall a b. Convertible a b => a -> ConvertResult b
safeConvert

instance Convertible BL.ByteString TL.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. b -> Either a b
Right (Text -> ConvertResult Text)
-> (ByteString -> Text) -> ByteString -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
TLE.decodeUtf8

instance Convertible BL.ByteString TLB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult Builder
safeConvert = (Text -> Builder) -> ConvertResult Text -> ConvertResult Builder
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Builder
TLB.fromLazyText (ConvertResult Text -> ConvertResult Builder)
-> (ByteString -> ConvertResult Text)
-> ByteString
-> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ConvertResult Text
forall a b. Convertible a b => a -> ConvertResult b
safeConvert

instance Convertible BL.ByteString BS.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. b -> Either a b
Right (ByteString -> ConvertResult ByteString)
-> (ByteString -> ByteString)
-> ByteString
-> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
BL.toStrict

instance Convertible BL.ByteString BB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: ByteString -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> (ByteString -> Builder) -> ByteString -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Builder
BB.lazyByteString



instance Convertible [Char] TS.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: [Char] -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. b -> Either a b
Right (Text -> ConvertResult Text)
-> ([Char] -> Text) -> [Char] -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
TS.pack

instance Convertible [Char] TL.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: [Char] -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. b -> Either a b
Right (Text -> ConvertResult Text)
-> ([Char] -> Text) -> [Char] -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
TL.pack

instance Convertible [Char] TLB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: [Char] -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> ([Char] -> Builder) -> [Char] -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Builder
TLB.fromString



instance Convertible [Word8] BS.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: [Word8] -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. b -> Either a b
Right (ByteString -> ConvertResult ByteString)
-> ([Word8] -> ByteString) -> [Word8] -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ByteString
BS.pack

instance Convertible [Word8] BL.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: [Word8] -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. b -> Either a b
Right (ByteString -> ConvertResult ByteString)
-> ([Word8] -> ByteString) -> [Word8] -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ByteString
BL.pack

instance Convertible [Word8] BB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: [Word8] -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> ([Word8] -> Builder) -> [Word8] -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Builder) -> [Word8] -> Builder
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Word8 -> Builder
BB.word8



instance Convertible Char TS.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: Char -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. b -> Either a b
Right (Text -> ConvertResult Text)
-> (Char -> Text) -> Char -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Text
TS.singleton

instance Convertible Char TL.Text where
    {-# INLINE safeConvert #-}
    safeConvert :: Char -> ConvertResult Text
safeConvert = Text -> ConvertResult Text
forall a b. b -> Either a b
Right (Text -> ConvertResult Text)
-> (Char -> Text) -> Char -> ConvertResult Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Text
TL.singleton

instance Convertible Char TLB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: Char -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> (Char -> Builder) -> Char -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Builder
TLB.singleton



instance Convertible Word8 BS.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: Word8 -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. b -> Either a b
Right (ByteString -> ConvertResult ByteString)
-> (Word8 -> ByteString) -> Word8 -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> ByteString
BS.singleton

instance Convertible Word8 BL.ByteString where
    {-# INLINE safeConvert #-}
    safeConvert :: Word8 -> ConvertResult ByteString
safeConvert = ByteString -> ConvertResult ByteString
forall a b. b -> Either a b
Right (ByteString -> ConvertResult ByteString)
-> (Word8 -> ByteString) -> Word8 -> ConvertResult ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> ByteString
BL.singleton

instance Convertible Word8 BB.Builder where
    {-# INLINE safeConvert #-}
    safeConvert :: Word8 -> ConvertResult Builder
safeConvert = Builder -> ConvertResult Builder
forall a b. b -> Either a b
Right (Builder -> ConvertResult Builder)
-> (Word8 -> Builder) -> Word8 -> ConvertResult Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Builder
BB.word8