module Data.Text.Encoding.Base16
( encodeBase16
, decodeBase16
, decodeBase16Untyped
, decodeBase16With
, decodeBase16Lenient
, parseBase16
) where
import Data.Base16.Types
import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base16 as B16
import Data.Char (chr)
import Data.Text (Text)
import Data.Text.Encoding.Base16.Error (Base16Error(..))
import qualified Data.Text.Encoding as T
encodeBase16 :: Text -> Base16 Text
encodeBase16 :: Text -> Base16 Text
encodeBase16 = ByteString -> Base16 Text
B16.encodeBase16 (ByteString -> Base16 Text)
-> (Text -> ByteString) -> Text -> Base16 Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
{-# INLINE encodeBase16 #-}
decodeBase16 :: Base16 Text -> Text
decodeBase16 :: Base16 Text -> Text
decodeBase16 = ByteString -> Text
T.decodeUtf8 (ByteString -> Text)
-> (Base16 Text -> ByteString) -> Base16 Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base16 ByteString -> ByteString
B16.decodeBase16 (Base16 ByteString -> ByteString)
-> (Base16 Text -> Base16 ByteString) -> Base16 Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> ByteString) -> Base16 Text -> Base16 ByteString
forall a b. (a -> b) -> Base16 a -> Base16 b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ByteString
T.encodeUtf8
{-# INLINE decodeBase16 #-}
decodeBase16Untyped :: Text -> Either Text Text
decodeBase16Untyped :: Text -> Either Text Text
decodeBase16Untyped = (ByteString -> Text) -> Either Text ByteString -> Either Text Text
forall a b. (a -> b) -> Either Text a -> Either Text b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
T.decodeUtf8 (Either Text ByteString -> Either Text Text)
-> (Text -> Either Text ByteString) -> Text -> Either Text Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Text ByteString
B16.decodeBase16Untyped (ByteString -> Either Text ByteString)
-> (Text -> ByteString) -> Text -> Either Text ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
{-# INLINE decodeBase16Untyped #-}
decodeBase16With
:: (ByteString -> Either err Text)
-> ByteString
-> Either (Base16Error err) Text
decodeBase16With :: forall err.
(ByteString -> Either err Text)
-> ByteString -> Either (Base16Error err) Text
decodeBase16With ByteString -> Either err Text
f ByteString
t = case ByteString -> Either Text ByteString
B16.decodeBase16Untyped ByteString
t of
Left Text
de -> Base16Error err -> Either (Base16Error err) Text
forall a b. a -> Either a b
Left (Base16Error err -> Either (Base16Error err) Text)
-> Base16Error err -> Either (Base16Error err) Text
forall a b. (a -> b) -> a -> b
$ Text -> Base16Error err
forall e. Text -> Base16Error e
DecodeError Text
de
Right ByteString
a -> (err -> Base16Error err)
-> Either err Text -> Either (Base16Error err) Text
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first err -> Base16Error err
forall e. e -> Base16Error e
ConversionError (ByteString -> Either err Text
f ByteString
a)
{-# INLINE decodeBase16With #-}
decodeBase16Lenient :: Text -> Text
decodeBase16Lenient :: Text -> Text
decodeBase16Lenient = OnDecodeError -> ByteString -> Text
T.decodeUtf8With (\String
_ -> (Word8 -> Char) -> Maybe Word8 -> Maybe Char
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Char
chr (Int -> Char) -> (Word8 -> Int) -> Word8 -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral))
(ByteString -> Text) -> (Text -> ByteString) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B16.decodeBase16Lenient
(ByteString -> ByteString)
-> (Text -> ByteString) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
{-# INLINE decodeBase16Lenient #-}
parseBase16 :: Text -> Either Text (Base16 Text)
parseBase16 :: Text -> Either Text (Base16 Text)
parseBase16 Text
bs = Text -> Base16 Text
forall a. a -> Base16 a
assertBase16 Text
bs Base16 Text
-> Either Text (Base16 ByteString) -> Either Text (Base16 Text)
forall a b. a -> Either Text b -> Either Text a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ByteString -> Either Text (Base16 ByteString)
B16.parseBase16 (Text -> ByteString
T.encodeUtf8 Text
bs)
{-# INLINE parseBase16 #-}