{-# LANGUAGE Safe #-}
module Data.Text.Lazy.Encoding.Base32
(
encodeBase32
, encodeBase32Unpadded
, decodeBase32
, decodeBase32With
, decodeBase32Unpadded
, decodeBase32UnpaddedWith
, decodeBase32Padded
, decodeBase32PaddedWith
, isBase32
, isValidBase32
) where
import Data.Bifunctor (first)
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy.Base32 as BL32
import qualified Data.Text as T
import Data.Text.Encoding.Base32.Error
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
encodeBase32 :: TL.Text -> TL.Text
encodeBase32 :: Text -> Text
encodeBase32 = ByteString -> Text
BL32.encodeBase32 (ByteString -> Text) -> (Text -> ByteString) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TL.encodeUtf8
{-# INLINE encodeBase32 #-}
decodeBase32 :: TL.Text -> Either T.Text TL.Text
decodeBase32 :: Text -> Either Text Text
decodeBase32 = (ByteString -> Text) -> Either Text ByteString -> Either Text Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
TL.decodeLatin1 (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
BL32.decodeBase32 (ByteString -> Either Text ByteString)
-> (Text -> ByteString) -> Text -> Either Text ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TL.encodeUtf8
{-# INLINE decodeBase32 #-}
decodeBase32With
:: (ByteString -> Either err TL.Text)
-> ByteString
-> Either (Base32Error err) TL.Text
decodeBase32With :: (ByteString -> Either err Text)
-> ByteString -> Either (Base32Error err) Text
decodeBase32With ByteString -> Either err Text
f ByteString
t = case ByteString -> Either Text ByteString
BL32.decodeBase32 ByteString
t of
Left Text
de -> Base32Error err -> Either (Base32Error err) Text
forall a b. a -> Either a b
Left (Base32Error err -> Either (Base32Error err) Text)
-> Base32Error err -> Either (Base32Error err) Text
forall a b. (a -> b) -> a -> b
$ Text -> Base32Error err
forall e. Text -> Base32Error e
DecodeError Text
de
Right ByteString
a -> (err -> Base32Error err)
-> Either err Text -> Either (Base32Error err) Text
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 (ByteString -> Either err Text
f ByteString
a)
{-# INLINE decodeBase32With #-}
encodeBase32Unpadded :: TL.Text -> TL.Text
encodeBase32Unpadded :: Text -> Text
encodeBase32Unpadded = ByteString -> Text
BL32.encodeBase32Unpadded (ByteString -> Text) -> (Text -> ByteString) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TL.encodeUtf8
{-# INLINE encodeBase32Unpadded #-}
decodeBase32Unpadded :: TL.Text -> Either T.Text TL.Text
decodeBase32Unpadded :: Text -> Either Text Text
decodeBase32Unpadded = (ByteString -> Text) -> Either Text ByteString -> Either Text Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
TL.decodeLatin1
(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
BL32.decodeBase32Unpadded
(ByteString -> Either Text ByteString)
-> (Text -> ByteString) -> Text -> Either Text ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TL.encodeUtf8
{-# INLINE decodeBase32Unpadded #-}
decodeBase32UnpaddedWith
:: (ByteString -> Either err TL.Text)
-> ByteString
-> Either (Base32Error err) TL.Text
decodeBase32UnpaddedWith :: (ByteString -> Either err Text)
-> ByteString -> Either (Base32Error err) Text
decodeBase32UnpaddedWith ByteString -> Either err Text
f ByteString
t = case ByteString -> Either Text ByteString
BL32.decodeBase32Unpadded ByteString
t of
Left Text
de -> Base32Error err -> Either (Base32Error err) Text
forall a b. a -> Either a b
Left (Base32Error err -> Either (Base32Error err) Text)
-> Base32Error err -> Either (Base32Error err) Text
forall a b. (a -> b) -> a -> b
$ Text -> Base32Error err
forall e. Text -> Base32Error e
DecodeError Text
de
Right ByteString
a -> (err -> Base32Error err)
-> Either err Text -> Either (Base32Error err) Text
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 (ByteString -> Either err Text
f ByteString
a)
{-# INLINE decodeBase32UnpaddedWith #-}
decodeBase32Padded :: TL.Text -> Either T.Text TL.Text
decodeBase32Padded :: Text -> Either Text Text
decodeBase32Padded = (ByteString -> Text) -> Either Text ByteString -> Either Text Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
TL.decodeLatin1
(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
BL32.decodeBase32Padded
(ByteString -> Either Text ByteString)
-> (Text -> ByteString) -> Text -> Either Text ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TL.encodeUtf8
{-# INLINE decodeBase32Padded #-}
decodeBase32PaddedWith
:: (ByteString -> Either err TL.Text)
-> ByteString
-> Either (Base32Error err) TL.Text
decodeBase32PaddedWith :: (ByteString -> Either err Text)
-> ByteString -> Either (Base32Error err) Text
decodeBase32PaddedWith ByteString -> Either err Text
f ByteString
t = case ByteString -> Either Text ByteString
BL32.decodeBase32Padded ByteString
t of
Left Text
de -> Base32Error err -> Either (Base32Error err) Text
forall a b. a -> Either a b
Left (Base32Error err -> Either (Base32Error err) Text)
-> Base32Error err -> Either (Base32Error err) Text
forall a b. (a -> b) -> a -> b
$ Text -> Base32Error err
forall e. Text -> Base32Error e
DecodeError Text
de
Right ByteString
a -> (err -> Base32Error err)
-> Either err Text -> Either (Base32Error err) Text
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 (ByteString -> Either err Text
f ByteString
a)
{-# INLINE decodeBase32PaddedWith #-}
isBase32 :: TL.Text -> Bool
isBase32 :: Text -> Bool
isBase32 = ByteString -> Bool
BL32.isBase32 (ByteString -> Bool) -> (Text -> ByteString) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TL.encodeUtf8
{-# INLINE isBase32 #-}
isValidBase32 :: TL.Text -> Bool
isValidBase32 :: Text -> Bool
isValidBase32 = ByteString -> Bool
BL32.isValidBase32 (ByteString -> Bool) -> (Text -> ByteString) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TL.encodeUtf8
{-# INLINE isValidBase32 #-}