{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE TypeApplications #-}
module Data.TypedEncoding.Instances.Restriction.UTF8 where
import Data.TypedEncoding.Instances.Support
import Data.Proxy
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text.Encoding as TE
import qualified Data.Text.Lazy.Encoding as TEL
import Data.Either
prxyUtf8 = Proxy :: Proxy "r-UTF8"
instance Encode (Either EncodeEx) "r-UTF8" "r-UTF8" c B.ByteString where
encoding = encUTF8B
instance Encode (Either EncodeEx) "r-UTF8" "r-UTF8" c BL.ByteString where
encoding = encUTF8BL :: Encoding (Either EncodeEx) "r-UTF8" "r-UTF8" c BL.ByteString
encUTF8B :: Encoding (Either EncodeEx) "r-UTF8" "r-UTF8" c B.ByteString
encUTF8B = _implEncodingEx (fmap TE.encodeUtf8 . TE.decodeUtf8')
{-# WARNING encUTF8B "This method was not optimized for performance." #-}
encUTF8BL :: Encoding (Either EncodeEx) "r-UTF8" "r-UTF8" c BL.ByteString
encUTF8BL = _implEncodingEx (fmap TEL.encodeUtf8 . TEL.decodeUtf8')
{-# WARNING encUTF8BL "This method was not optimized for performance." #-}
instance (Applicative f) => Decode f "r-UTF8" "r-UTF8" c str where
decoding = decAnyR
instance (RecreateErr f, Applicative f) => Validate f "r-UTF8" "r-UTF8" c B.ByteString where
validation = validR encUTF8B
instance (RecreateErr f, Applicative f) => Validate f "r-UTF8" "r-UTF8" c BL.ByteString where
validation = validR encUTF8BL
verEncoding :: B.ByteString -> Either err B.ByteString -> Bool
verEncoding bs (Left _) = isLeft . TE.decodeUtf8' $ bs
verEncoding bs (Right _) = isRight . TE.decodeUtf8' $ bs