{-# LANGUAGE Safe #-}
module Data.Text.Encoding.Base32
( encodeBase32
, decodeBase32
, decodeBase32With
, encodeBase32Unpadded
, decodeBase32Unpadded
, decodeBase32UnpaddedWith
, decodeBase32Padded
, decodeBase32PaddedWith
, isBase32
, isValidBase32
) where
import Data.Bifunctor (first)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base32 as B32
import Data.Text (Text)
import qualified Data.Text.Encoding as T
import Data.Text.Encoding.Base32.Error (Base32Error(..))
encodeBase32 :: Text -> Text
encodeBase32 :: Text -> Text
encodeBase32 = ByteString -> Text
B32.encodeBase32 (ByteString -> Text) -> (Text -> ByteString) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
{-# INLINE encodeBase32 #-}
decodeBase32 :: Text -> Either Text 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
T.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
B32.decodeBase32
(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 decodeBase32 #-}
decodeBase32With
:: (ByteString -> Either err Text)
-> ByteString
-> Either (Base32Error err) Text
decodeBase32With :: (ByteString -> Either err Text)
-> ByteString -> Either (Base32Error err) Text
decodeBase32With ByteString -> Either err Text
f ByteString
t = case ByteString -> Either Text ByteString
B32.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 :: Text -> Text
encodeBase32Unpadded :: Text -> Text
encodeBase32Unpadded = ByteString -> Text
B32.encodeBase32Unpadded (ByteString -> Text) -> (Text -> ByteString) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
{-# INLINE encodeBase32Unpadded #-}
decodeBase32Unpadded :: Text -> Either Text 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
T.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
B32.decodeBase32Unpadded
(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 decodeBase32Unpadded #-}
decodeBase32UnpaddedWith
:: (ByteString -> Either err Text)
-> ByteString
-> Either (Base32Error err) Text
decodeBase32UnpaddedWith :: (ByteString -> Either err Text)
-> ByteString -> Either (Base32Error err) Text
decodeBase32UnpaddedWith ByteString -> Either err Text
f ByteString
t = case ByteString -> Either Text ByteString
B32.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 :: Text -> Either Text 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
T.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
B32.decodeBase32Padded
(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 decodeBase32Padded #-}
decodeBase32PaddedWith
:: (ByteString -> Either err Text)
-> ByteString
-> Either (Base32Error err) Text
decodeBase32PaddedWith :: (ByteString -> Either err Text)
-> ByteString -> Either (Base32Error err) Text
decodeBase32PaddedWith ByteString -> Either err Text
f ByteString
t = case ByteString -> Either Text ByteString
B32.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 :: Text -> Bool
isBase32 :: Text -> Bool
isBase32 = ByteString -> Bool
B32.isBase32 (ByteString -> Bool) -> (Text -> ByteString) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
{-# INLINE isBase32 #-}
isValidBase32 :: Text -> Bool
isValidBase32 :: Text -> Bool
isValidBase32 = ByteString -> Bool
B32.isValidBase32 (ByteString -> Bool) -> (Text -> ByteString) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8
{-# INLINE isValidBase32 #-}