{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
module To
(
ToString(..),
Utf8ToString(..),
ToText(..),
Utf8ToText(..),
ToLazyText(..),
Utf8ToLazyText(..),
ToTextBuilder(..),
Utf8ToTextBuilder(..),
ToByteString(..),
ToUtf8ByteString(..),
ToLazyByteString(..),
ToUtf8LazyByteString(..),
)
where
import GHC.TypeLits (TypeError, ErrorMessage(..))
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Encoding.Error as T
import qualified Data.Text.Lazy.Builder as TB
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Lazy.UTF8 as UTF8L
import qualified Data.ByteString.UTF8 as UTF8
class ToText a where
toText :: a -> T.Text
instance (a ~ Char) => ToText [a] where
toText = T.pack
{-# INLINE toText #-}
instance ToText TL.Text where
toText = TL.toStrict
{-# INLINE toText #-}
instance ToText TB.Builder where
toText = TL.toStrict . TB.toLazyText
{-# INLINE toText #-}
instance TypeError (SpecifyDecoding BS.ByteString "utf8ToText") =>
ToText BS.ByteString where
toText = error "unreachable"
instance TypeError (SpecifyDecoding BSL.ByteString "utf8ToText") =>
ToText BSL.ByteString where
toText = error "unreachable"
class ToLazyText a where
toLazyText :: a -> TL.Text
instance (a ~ Char) => ToLazyText [a] where
toLazyText = TL.pack
{-# INLINE toLazyText #-}
instance ToLazyText T.Text where
toLazyText = TL.fromStrict
{-# INLINE toLazyText #-}
instance ToLazyText TB.Builder where
toLazyText = TB.toLazyText
{-# INLINE toLazyText #-}
instance TypeError (SpecifyDecoding BS.ByteString "utf8ToLazyText") =>
ToLazyText BS.ByteString where
toLazyText = error "unreachable"
instance TypeError (SpecifyDecoding BSL.ByteString "utf8ToLazyText") =>
ToLazyText BSL.ByteString where
toLazyText = error "unreachable"
class ToTextBuilder a where
toTextBuilder :: a -> TB.Builder
instance (a ~ Char) => ToTextBuilder [a] where
toTextBuilder = TB.fromString
{-# INLINE toTextBuilder #-}
instance ToTextBuilder T.Text where
toTextBuilder = TB.fromText
{-# INLINE toTextBuilder #-}
instance ToTextBuilder TL.Text where
toTextBuilder = TB.fromLazyText
{-# INLINE toTextBuilder #-}
instance TypeError (SpecifyDecoding BS.ByteString "utf8ToTextBuilder") =>
ToTextBuilder BS.ByteString where
toTextBuilder = error "unreachable"
instance TypeError (SpecifyDecoding BSL.ByteString "utf8ToTextBuilder") =>
ToTextBuilder BSL.ByteString where
toTextBuilder = error "unreachable"
class ToString a where
toString :: a -> String
instance ToString T.Text where
toString = T.unpack
{-# INLINE toString #-}
instance ToString TL.Text where
toString = TL.unpack
{-# INLINE toString #-}
instance ToString TB.Builder where
toString = TL.unpack . TB.toLazyText
{-# INLINE toString #-}
instance TypeError (SpecifyDecoding BS.ByteString "utf8ToString") =>
ToString BS.ByteString where
toString = error "unreachable"
instance TypeError (SpecifyDecoding BSL.ByteString "utf8ToString") =>
ToString BSL.ByteString where
toString = error "unreachable"
class ToByteString a where
toByteString :: a -> BS.ByteString
instance TypeError (SpecifyEncoding T.Text "toUtf8ByteString") =>
ToByteString T.Text where
toByteString = error "unreachable"
instance TypeError (SpecifyEncoding TL.Text "toUtf8ByteString") =>
ToByteString TL.Text where
toByteString = error "unreachable"
instance TypeError (SpecifyEncoding TB.Builder "toUtf8ByteString") =>
ToByteString TB.Builder where
toByteString = error "unreachable"
instance (a ~ Char, TypeError (SpecifyEncoding String "toUtf8ByteString")) =>
ToByteString [a] where
toByteString = error "unreachable"
instance ToByteString BSL.ByteString where
toByteString = BSL.toStrict
{-# INLINE toByteString #-}
class ToLazyByteString a where
toLazyByteString :: a -> BSL.ByteString
instance TypeError (SpecifyEncoding T.Text "toUtf8LazyByteString") =>
ToLazyByteString T.Text where
toLazyByteString = error "unreachable"
instance TypeError (SpecifyEncoding TL.Text "toUtf8LazyByteString") =>
ToLazyByteString TL.Text where
toLazyByteString = error "unreachable"
instance TypeError (SpecifyEncoding TB.Builder "toUtf8LazyByteString") =>
ToLazyByteString TB.Builder where
toLazyByteString = error "unreachable"
instance (a ~ Char, TypeError (SpecifyEncoding String "toUtf8LazyByteString")) =>
ToLazyByteString [a] where
toLazyByteString = error "unreachable"
instance ToLazyByteString BS.ByteString where
toLazyByteString = BSL.fromStrict
{-# INLINE toLazyByteString #-}
class Utf8ToString a where
utf8ToString :: a -> String
instance Utf8ToString BS.ByteString where
utf8ToString = UTF8.toString
{-# INLINE utf8ToString #-}
instance Utf8ToString BSL.ByteString where
utf8ToString = UTF8L.toString
{-# INLINE utf8ToString #-}
class Utf8ToText a where
utf8ToText :: a -> T.Text
instance Utf8ToText BS.ByteString where
utf8ToText = T.decodeUtf8With T.lenientDecode
{-# INLINE utf8ToText #-}
instance Utf8ToText BSL.ByteString where
utf8ToText = T.decodeUtf8With T.lenientDecode . BSL.toStrict
{-# INLINE utf8ToText #-}
class Utf8ToLazyText a where
utf8ToLazyText :: a -> TL.Text
instance Utf8ToLazyText BS.ByteString where
utf8ToLazyText = TL.fromStrict . T.decodeUtf8With T.lenientDecode
{-# INLINE utf8ToLazyText #-}
instance Utf8ToLazyText BSL.ByteString where
utf8ToLazyText = TL.decodeUtf8With T.lenientDecode
{-# INLINE utf8ToLazyText #-}
class Utf8ToTextBuilder a where
utf8ToTextBuilder :: a -> TB.Builder
instance Utf8ToTextBuilder BS.ByteString where
utf8ToTextBuilder = TB.fromText . T.decodeUtf8With T.lenientDecode
{-# INLINE utf8ToTextBuilder #-}
instance Utf8ToTextBuilder BSL.ByteString where
utf8ToTextBuilder = TB.fromLazyText . TL.decodeUtf8With T.lenientDecode
{-# INLINE utf8ToTextBuilder #-}
class ToUtf8ByteString a where
toUtf8ByteString :: a -> BS.ByteString
instance ToUtf8ByteString T.Text where
toUtf8ByteString = T.encodeUtf8
{-# INLINE toUtf8ByteString #-}
instance ToUtf8ByteString TL.Text where
toUtf8ByteString = T.encodeUtf8 . TL.toStrict
{-# INLINE toUtf8ByteString #-}
instance ToUtf8ByteString TB.Builder where
toUtf8ByteString = T.encodeUtf8 . TL.toStrict . TB.toLazyText
{-# INLINE toUtf8ByteString #-}
instance (a ~ Char) => ToUtf8ByteString [a] where
toUtf8ByteString = UTF8.fromString
{-# INLINE toUtf8ByteString #-}
class ToUtf8LazyByteString a where
toUtf8LazyByteString :: a -> BSL.ByteString
instance ToUtf8LazyByteString T.Text where
toUtf8LazyByteString = TL.encodeUtf8 . TL.fromStrict
{-# INLINE toUtf8LazyByteString #-}
instance ToUtf8LazyByteString TL.Text where
toUtf8LazyByteString = TL.encodeUtf8
{-# INLINE toUtf8LazyByteString #-}
instance ToUtf8LazyByteString TB.Builder where
toUtf8LazyByteString = TL.encodeUtf8 . TB.toLazyText
{-# INLINE toUtf8LazyByteString #-}
instance (a ~ Char) => ToUtf8LazyByteString [a] where
toUtf8LazyByteString = UTF8L.fromString
{-# INLINE toUtf8LazyByteString #-}
type SpecifyEncoding type_ proposed =
'Text "Can not encode a " :<>: 'ShowType type_ :<>:
'Text " without specifying encoding." :$$:
'Text "Use '" :<>: 'Text proposed :<>:
'Text "' if you want to encode as UTF8."
type SpecifyDecoding type_ proposed =
'Text "Can not decode a " :<>: 'ShowType type_ :<>:
'Text " without specifying encoding." :$$:
'Text "Use '" :<>: 'Text proposed :<>:
'Text "' if you want to decode ASCII or UTF8."