{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
#if MIN_VERSION_lens(5,0,0)
{-# LANGUAGE Safe #-}
#else
{-# LANGUAGE Trustworthy #-}
#endif
-- |
-- Module       : Data.Text.Lazy.Encoding.Base32.Lens
-- Copyright 	: (c) 2020 Emily Pillmore
-- License	: BSD-style
--
-- Maintainer	: Emily Pillmore <emilypi@cohomolo.gy>
-- Stability	: Experimental
-- Portability	: non-portable
--
-- This module contains 'Prism's Base32-encoding and
-- decoding 'Text' values.
--
module Data.Text.Lazy.Encoding.Base32.Lens
( -- * Prisms
  _Base32
, _Base32Unpadded
, _Base32Hex
, _Base32HexUnpadded
-- , _Base32Lenient
-- , _Base32HexLenient
  -- * Patterns
, pattern Base32
, pattern Base32Unpadded
, pattern Base32Hex
, pattern Base32HexUnpadded
-- , pattern Base32Lenient
-- , pattern Base32HexLenient
) where

import Control.Lens

import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy.Encoding.Base32 as B32TL
import qualified Data.Text.Lazy.Encoding.Base32.Hex as B32TLH


-- $setup
--
-- >>> import Control.Lens
-- >>> import Data.Text.Lazy.Encoding.Base32.Lens
--
-- >>> :set -XOverloadedStrings
-- >>> :set -XTypeApplications

-- -------------------------------------------------------------------------- --
-- Optics

-- | A 'Prism' into the Base32 encoding of a 'Text' value.
--
-- >>> _Base32 # "Sun"
-- "KN2W4==="
--
-- >>> "KN2W4===" ^? _Base32
-- Just "Sun"
--
_Base32 :: Prism' Text Text
_Base32 :: p Text (f Text) -> p Text (f Text)
_Base32 = (Text -> Text) -> (Text -> Maybe Text) -> Prism Text Text Text Text
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Text -> Text
B32TL.encodeBase32 ((Text -> Maybe Text) -> Prism Text Text Text Text)
-> (Text -> Maybe Text) -> Prism Text Text Text Text
forall a b. (a -> b) -> a -> b
$ \Text
s -> case Text -> Either Text Text
B32TL.decodeBase32 Text
s of
    Left Text
_ -> Maybe Text
forall a. Maybe a
Nothing
    Right Text
a -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
a
{-# INLINE _Base32 #-}

-- | A 'Prism' into the Base32 encoding of a 'Text' value.
--
-- Please note that unpadded variants should only be used
-- when assumptions about the data can be made. In particular, if the length of
-- the input is divisible by 3, then this is a safe function to call.
--
-- >>> _Base32Unpadded # "Sun"
-- "KN2W4"
--
-- >>> "KN2W4" ^? _Base32Unpadded
-- Just "Sun"
--
_Base32Unpadded :: Prism' Text Text
_Base32Unpadded :: p Text (f Text) -> p Text (f Text)
_Base32Unpadded = (Text -> Text) -> (Text -> Maybe Text) -> Prism Text Text Text Text
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Text -> Text
B32TL.encodeBase32Unpadded ((Text -> Maybe Text) -> Prism Text Text Text Text)
-> (Text -> Maybe Text) -> Prism Text Text Text Text
forall a b. (a -> b) -> a -> b
$ \Text
s -> case Text -> Either Text Text
B32TL.decodeBase32Unpadded Text
s of
    Left Text
_ -> Maybe Text
forall a. Maybe a
Nothing
    Right Text
a -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
a
{-# INLINE _Base32Unpadded #-}

-- | A 'Prism' into the Base32-hex encoding of a 'Text' value.
--
-- >>> _Base32Hex # "Sun"
-- "ADQMS==="
--
-- >>> "ADQMS===" ^? _Base32Hex
-- Just "Sun"
--
_Base32Hex :: Prism' Text Text
_Base32Hex :: p Text (f Text) -> p Text (f Text)
_Base32Hex = (Text -> Text) -> (Text -> Maybe Text) -> Prism Text Text Text Text
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Text -> Text
B32TLH.encodeBase32 ((Text -> Maybe Text) -> Prism Text Text Text Text)
-> (Text -> Maybe Text) -> Prism Text Text Text Text
forall a b. (a -> b) -> a -> b
$ \Text
s -> case Text -> Either Text Text
B32TLH.decodeBase32 Text
s of
    Left Text
_ -> Maybe Text
forall a. Maybe a
Nothing
    Right Text
a -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
a
{-# INLINE _Base32Hex #-}

-- | A 'Prism' into the Base32-hex encoding of a 'Text' value.
--
-- Please note that unpadded variants should only be used
-- when assumptions about the data can be made. In particular, if the length of
-- the input is divisible by 3, then this is a safe function to call.
--
-- >>> _Base32HexUnpadded # "Sun"
-- "ADQMS"
--
-- >>> "ADQMS" ^? _Base32HexUnpadded
-- Just "Sun"
--
_Base32HexUnpadded :: Prism' Text Text
_Base32HexUnpadded :: p Text (f Text) -> p Text (f Text)
_Base32HexUnpadded = (Text -> Text) -> (Text -> Maybe Text) -> Prism Text Text Text Text
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' Text -> Text
B32TLH.encodeBase32Unpadded ((Text -> Maybe Text) -> Prism Text Text Text Text)
-> (Text -> Maybe Text) -> Prism Text Text Text Text
forall a b. (a -> b) -> a -> b
$ \Text
s -> case Text -> Either Text Text
B32TLH.decodeBase32Unpadded Text
s of
    Left Text
_ -> Maybe Text
forall a. Maybe a
Nothing
    Right Text
a -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
a
{-# INLINE _Base32HexUnpadded #-}

-- -- | An 'Iso'' into the Base32 encoding of a 'Text' value
-- -- using lenient decoding.
-- --
-- --
-- -- _Note:_ This is not a lawful 'Iso'.
-- --
-- -- >>> "Sun" ^. _Base32Lenient
-- -- "U3Vu"
-- --
-- -- >>> "U3Vu" ^. from _Base32Lenient
-- -- "Sun"
-- --
-- _Base32Lenient :: Iso' Text Text
-- _Base32Lenient = iso B32TL.encodeBase32 B32TL.decodeBase32Lenient

-- -- | An 'Iso'' into the Base32hex encoding of a 'Text' value
-- -- using lenient decoding.
-- --
-- --
-- -- _Note:_ This is not a lawful 'Iso'.
-- --
-- -- >>> "<<??>>" ^. _Base32HexLenient
-- -- "PDw_Pz4-"
-- --
-- -- >>> "PDw_Pz4-" ^. from _Base32HexLenient
-- -- "<<??>>"
-- --
-- _Base32HexLenient :: Iso' Text Text
-- _Base32HexLenient = iso B32TLH.encodeBase32 B32TLH.decodeBase32Lenient

-- -------------------------------------------------------------------------- --
-- Patterns

-- | Unidirectional pattern synonym for Base32-encoded 'Text' values.
--
pattern Base32 :: Text -> Text
pattern $bBase32 :: Text -> Text
$mBase32 :: forall r. Text -> (Text -> r) -> (Void# -> r) -> r
Base32 a <- (preview _Base32 -> Just a) where
    Base32 Text
a = Tagged Text (Identity Text) -> Tagged Text (Identity Text)
Prism Text Text Text Text
_Base32 (Tagged Text (Identity Text) -> Tagged Text (Identity Text))
-> Text -> Text
forall t b. AReview t b -> b -> t
# Text
a

-- | Unidirectional pattern synonym for unpadded Base32-encoded 'Text' values.
--
pattern Base32Unpadded :: Text -> Text
pattern $bBase32Unpadded :: Text -> Text
$mBase32Unpadded :: forall r. Text -> (Text -> r) -> (Void# -> r) -> r
Base32Unpadded a <- (preview _Base32Unpadded -> Just a) where
    Base32Unpadded Text
a = Tagged Text (Identity Text) -> Tagged Text (Identity Text)
Prism Text Text Text Text
_Base32Unpadded (Tagged Text (Identity Text) -> Tagged Text (Identity Text))
-> Text -> Text
forall t b. AReview t b -> b -> t
# Text
a

-- | Unidirectional pattern synonym for Base32hex-encoded 'Text' values.
--
pattern Base32Hex :: Text -> Text
pattern $bBase32Hex :: Text -> Text
$mBase32Hex :: forall r. Text -> (Text -> r) -> (Void# -> r) -> r
Base32Hex a <- (preview _Base32Hex -> Just a) where
    Base32Hex Text
a = Tagged Text (Identity Text) -> Tagged Text (Identity Text)
Prism Text Text Text Text
_Base32Hex (Tagged Text (Identity Text) -> Tagged Text (Identity Text))
-> Text -> Text
forall t b. AReview t b -> b -> t
# Text
a

-- | Unidirectional pattern synonym for unpadded Base32hex-encoded 'Text' values.
--
pattern Base32HexUnpadded :: Text -> Text
pattern $bBase32HexUnpadded :: Text -> Text
$mBase32HexUnpadded :: forall r. Text -> (Text -> r) -> (Void# -> r) -> r
Base32HexUnpadded a <- (preview _Base32HexUnpadded -> Just a) where
    Base32HexUnpadded Text
a = Tagged Text (Identity Text) -> Tagged Text (Identity Text)
Prism Text Text Text Text
_Base32HexUnpadded (Tagged Text (Identity Text) -> Tagged Text (Identity Text))
-> Text -> Text
forall t b. AReview t b -> b -> t
# Text
a

-- -- | Bidirectional pattern synonym for leniently Base32-encoded 'Text' values
-- --
-- pattern Base32Lenient :: Text -> Text
-- pattern Base32Lenient a <- (view (from _Base32Lenient) -> a) where
--     Base32Lenient a = view _Base32Lenient a
-- {-# COMPLETE Base32Lenient #-}

-- -- | Bidirectional pattern synonym for leniently Base32-encoded 'Text' values
-- --
-- pattern Base32HexLenient :: Text -> Text
-- pattern Base32HexLenient a <- (view (from _Base32HexLenient) -> a) where
--     Base32HexLenient a = view _Base32HexLenient a
-- {-# COMPLETE Base32HexLenient #-}