{-# LANGUAGE Trustworthy #-}
module Data.Text.Short.Encoding.Base32
(
encodeBase32
, encodeBase32Unpadded
, decodeBase32
, decodeBase32With
, decodeBase32Unpadded
, decodeBase32UnpaddedWith
, decodeBase32Padded
, decodeBase32PaddedWith
, isBase32
, isValidBase32
) where
import Data.Bifunctor (first)
import qualified Data.ByteString.Base32 as B32
import Data.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short.Base32 as BS32
import Data.Text (Text)
import qualified Data.Text.Encoding.Base32 as B32T
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
B32.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
B32T.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
BS32.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
B32.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
B32T.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
BS32.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
B32T.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
BS32.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 #-}
isBase32 :: ShortText -> Bool
isBase32 :: ShortText -> Bool
isBase32 = ByteString -> Bool
B32.isBase32 (ByteString -> Bool)
-> (ShortText -> ByteString) -> ShortText -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> ByteString
toByteString
{-# INLINE isBase32 #-}
isValidBase32 :: ShortText -> Bool
isValidBase32 :: ShortText -> Bool
isValidBase32 = ByteString -> Bool
B32.isValidBase32 (ByteString -> Bool)
-> (ShortText -> ByteString) -> ShortText -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortText -> ByteString
toByteString
{-# INLINE isValidBase32 #-}