{-# LANGUAGE Trustworthy #-}
module Data.Text.Short.Encoding.Base32.Hex
(
encodeBase32
, encodeBase32Unpadded
, decodeBase32
, decodeBase32With
, decodeBase32Unpadded
, decodeBase32UnpaddedWith
, decodeBase32Padded
, decodeBase32PaddedWith
, isBase32Hex
, isValidBase32Hex
) where
import Data.Bifunctor (first)
import qualified Data.ByteString.Base32.Hex as B32H
import Data.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short.Base32.Hex as BS32H
import Data.Text (Text)
import qualified Data.Text.Encoding.Base32.Hex as B32TH
import Data.Text.Encoding.Base32.Error
import Data.Text.Short
import Data.Text.Short.Unsafe
encodeBase32 :: ShortText -> ShortText
encodeBase32 :: ShortText -> ShortText
encodeBase32 = ByteString -> ShortText
fromByteStringUnsafe
(ByteString -> ShortText)
-> (ShortText -> ByteString) -> ShortText -> ShortText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B32H.encodeBase32'
(ByteString -> ByteString)
-> (ShortText -> ByteString) -> ShortText -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> ByteString
toByteString
{-# INLINE encodeBase32 #-}
decodeBase32 :: ShortText -> Either Text ShortText
decodeBase32 :: ShortText -> Either Text ShortText
decodeBase32 = (Text -> ShortText) -> Either Text Text -> Either Text ShortText
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ShortText
fromText (Either Text Text -> Either Text ShortText)
-> (ShortText -> Either Text Text)
-> ShortText
-> Either Text ShortText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text Text
B32TH.decodeBase32 (Text -> Either Text Text)
-> (ShortText -> Text) -> ShortText -> Either Text Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> Text
toText
{-# INLINE decodeBase32 #-}
decodeBase32With
:: (ShortByteString -> Either err ShortText)
-> ShortByteString
-> Either (Base32Error err) ShortText
decodeBase32With :: (ShortByteString -> Either err ShortText)
-> ShortByteString -> Either (Base32Error err) ShortText
decodeBase32With ShortByteString -> Either err ShortText
f ShortByteString
t = case ShortByteString -> Either Text ShortByteString
BS32H.decodeBase32 ShortByteString
t of
Left Text
de -> Base32Error err -> Either (Base32Error err) ShortText
forall a b. a -> Either a b
Left (Base32Error err -> Either (Base32Error err) ShortText)
-> Base32Error err -> Either (Base32Error err) ShortText
forall a b. (a -> b) -> a -> b
$ Text -> Base32Error err
forall e. Text -> Base32Error e
DecodeError Text
de
Right ShortByteString
a -> (err -> Base32Error err)
-> Either err ShortText -> Either (Base32Error err) ShortText
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first err -> Base32Error err
forall e. e -> Base32Error e
ConversionError (ShortByteString -> Either err ShortText
f ShortByteString
a)
{-# INLINE decodeBase32With #-}
encodeBase32Unpadded :: ShortText -> ShortText
encodeBase32Unpadded :: ShortText -> ShortText
encodeBase32Unpadded = ByteString -> ShortText
fromByteStringUnsafe
(ByteString -> ShortText)
-> (ShortText -> ByteString) -> ShortText -> ShortText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B32H.encodeBase32Unpadded'
(ByteString -> ByteString)
-> (ShortText -> ByteString) -> ShortText -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> ByteString
toByteString
{-# INLINE encodeBase32Unpadded #-}
decodeBase32Unpadded :: ShortText -> Either Text ShortText
decodeBase32Unpadded :: ShortText -> Either Text ShortText
decodeBase32Unpadded = (Text -> ShortText) -> Either Text Text -> Either Text ShortText
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ShortText
fromText (Either Text Text -> Either Text ShortText)
-> (ShortText -> Either Text Text)
-> ShortText
-> Either Text ShortText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text Text
B32TH.decodeBase32Unpadded (Text -> Either Text Text)
-> (ShortText -> Text) -> ShortText -> Either Text Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> Text
toText
{-# INLINE decodeBase32Unpadded #-}
decodeBase32UnpaddedWith
:: (ShortByteString -> Either err ShortText)
-> ShortByteString
-> Either (Base32Error err) ShortText
decodeBase32UnpaddedWith :: (ShortByteString -> Either err ShortText)
-> ShortByteString -> Either (Base32Error err) ShortText
decodeBase32UnpaddedWith ShortByteString -> Either err ShortText
f ShortByteString
t = case ShortByteString -> Either Text ShortByteString
BS32H.decodeBase32Unpadded ShortByteString
t of
Left Text
de -> Base32Error err -> Either (Base32Error err) ShortText
forall a b. a -> Either a b
Left (Base32Error err -> Either (Base32Error err) ShortText)
-> Base32Error err -> Either (Base32Error err) ShortText
forall a b. (a -> b) -> a -> b
$ Text -> Base32Error err
forall e. Text -> Base32Error e
DecodeError Text
de
Right ShortByteString
a -> (err -> Base32Error err)
-> Either err ShortText -> Either (Base32Error err) ShortText
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first err -> Base32Error err
forall e. e -> Base32Error e
ConversionError (ShortByteString -> Either err ShortText
f ShortByteString
a)
{-# INLINE decodeBase32UnpaddedWith #-}
decodeBase32Padded :: ShortText -> Either Text ShortText
decodeBase32Padded :: ShortText -> Either Text ShortText
decodeBase32Padded = (Text -> ShortText) -> Either Text Text -> Either Text ShortText
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ShortText
fromText (Either Text Text -> Either Text ShortText)
-> (ShortText -> Either Text Text)
-> ShortText
-> Either Text ShortText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text Text
B32TH.decodeBase32Padded (Text -> Either Text Text)
-> (ShortText -> Text) -> ShortText -> Either Text Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> Text
toText
{-# INLINE decodeBase32Padded #-}
decodeBase32PaddedWith
:: (ShortByteString -> Either err ShortText)
-> ShortByteString
-> Either (Base32Error err) ShortText
decodeBase32PaddedWith :: (ShortByteString -> Either err ShortText)
-> ShortByteString -> Either (Base32Error err) ShortText
decodeBase32PaddedWith ShortByteString -> Either err ShortText
f ShortByteString
t = case ShortByteString -> Either Text ShortByteString
BS32H.decodeBase32Padded ShortByteString
t of
Left Text
de -> Base32Error err -> Either (Base32Error err) ShortText
forall a b. a -> Either a b
Left (Base32Error err -> Either (Base32Error err) ShortText)
-> Base32Error err -> Either (Base32Error err) ShortText
forall a b. (a -> b) -> a -> b
$ Text -> Base32Error err
forall e. Text -> Base32Error e
DecodeError Text
de
Right ShortByteString
a -> (err -> Base32Error err)
-> Either err ShortText -> Either (Base32Error err) ShortText
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first err -> Base32Error err
forall e. e -> Base32Error e
ConversionError (ShortByteString -> Either err ShortText
f ShortByteString
a)
{-# INLINE decodeBase32PaddedWith #-}
isBase32Hex :: ShortText -> Bool
isBase32Hex :: ShortText -> Bool
isBase32Hex = ByteString -> Bool
B32H.isBase32Hex (ByteString -> Bool)
-> (ShortText -> ByteString) -> ShortText -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> ByteString
toByteString
{-# INLINE isBase32Hex #-}
isValidBase32Hex :: ShortText -> Bool
isValidBase32Hex :: ShortText -> Bool
isValidBase32Hex = ByteString -> Bool
B32H.isValidBase32Hex (ByteString -> Bool)
-> (ShortText -> ByteString) -> ShortText -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> ByteString
toByteString
{-# INLINE isValidBase32Hex #-}