{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
#if MIN_VERSION_lens(5,0,0)
{-# LANGUAGE Safe #-}
#else
{-# LANGUAGE Trustworthy #-}
#endif
-- |
-- Module       : Data.Text.Short.Encoding.Base64.Lens
-- Copyright 	: (c) 2019-2021 Emily Pillmore
-- License	: BSD-style
--
-- Maintainer	: Emily Pillmore <emilypi@cohomolo.gy>
-- Stability	: Experimental
-- Portability	: non-portable
--
-- This module contains 'Prism's and 'Iso''s Base64-encoding and
-- decoding 'ShortText' values.
--
module Data.Text.Short.Encoding.Base64.Lens
( -- * Prisms
  _Base64
, _Base64Url
, _Base64UrlUnpadded
, _Base64Lenient
, _Base64UrlLenient
  -- * Patterns
, pattern Base64
, pattern Base64Url
, pattern Base64UrlUnpadded
, pattern Base64Lenient
, pattern Base64UrlLenient
) where

import Control.Lens

import Data.Text.Short (ShortText)
import qualified Data.Text.Short.Encoding.Base64 as TS64
import qualified Data.Text.Short.Encoding.Base64.URL as TS64U


-- $setup
--
-- >>> import Control.Lens
-- >>> import Data.Text.Short.Encoding.Base64.Lens
--
-- >>> :set -XOverloadedStrings
-- >>> :set -XTypeApplications

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

-- | A 'Prism' into the Base64 encoding of a 'ShortText' value.
--
-- >>> _Base64 # "Sun"
-- "U3Vu"
--
-- >>> "U3Vu" ^? _Base64
-- Just "Sun"
--
_Base64 :: Prism' ShortText ShortText
_Base64 :: p ShortText (f ShortText) -> p ShortText (f ShortText)
_Base64 = (ShortText -> ShortText)
-> (ShortText -> Maybe ShortText)
-> Prism ShortText ShortText ShortText ShortText
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' ShortText -> ShortText
TS64.encodeBase64 ((ShortText -> Maybe ShortText)
 -> Prism ShortText ShortText ShortText ShortText)
-> (ShortText -> Maybe ShortText)
-> Prism ShortText ShortText ShortText ShortText
forall a b. (a -> b) -> a -> b
$ \ShortText
s -> case ShortText -> Either Text ShortText
TS64.decodeBase64 ShortText
s of
    Left Text
_ -> Maybe ShortText
forall a. Maybe a
Nothing
    Right ShortText
a -> ShortText -> Maybe ShortText
forall a. a -> Maybe a
Just ShortText
a
{-# INLINE _Base64 #-}

-- | A 'Prism' into the Base64-url encoding of a 'ShortText' value.
--
-- >>> _Base64Url # "Sun"
-- "U3Vu"
--
-- >>> "PDw_Pz8-Pg==" ^? _Base64Url
-- Just "<<???>>"
--
_Base64Url :: Prism' ShortText ShortText
_Base64Url :: p ShortText (f ShortText) -> p ShortText (f ShortText)
_Base64Url = (ShortText -> ShortText)
-> (ShortText -> Maybe ShortText)
-> Prism ShortText ShortText ShortText ShortText
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' ShortText -> ShortText
TS64U.encodeBase64 ((ShortText -> Maybe ShortText)
 -> Prism ShortText ShortText ShortText ShortText)
-> (ShortText -> Maybe ShortText)
-> Prism ShortText ShortText ShortText ShortText
forall a b. (a -> b) -> a -> b
$ \ShortText
s -> case ShortText -> Either Text ShortText
TS64U.decodeBase64 ShortText
s of
    Left Text
_ -> Maybe ShortText
forall a. Maybe a
Nothing
    Right ShortText
a -> ShortText -> Maybe ShortText
forall a. a -> Maybe a
Just ShortText
a
{-# INLINE _Base64Url #-}

-- | A 'Prism' into the Base64-url encoding of a 'ShortText' 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.
--
-- >>> _Base64UrlUnpadded # "<<??>>"
-- "PDw_Pz4-"
--
-- >>> "PDw_Pz4-" ^? _Base64UrlUnpadded
-- Just "<<??>>"
--
_Base64UrlUnpadded :: Prism' ShortText ShortText
_Base64UrlUnpadded :: p ShortText (f ShortText) -> p ShortText (f ShortText)
_Base64UrlUnpadded = (ShortText -> ShortText)
-> (ShortText -> Maybe ShortText)
-> Prism ShortText ShortText ShortText ShortText
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' ShortText -> ShortText
TS64U.encodeBase64Unpadded ((ShortText -> Maybe ShortText)
 -> Prism ShortText ShortText ShortText ShortText)
-> (ShortText -> Maybe ShortText)
-> Prism ShortText ShortText ShortText ShortText
forall a b. (a -> b) -> a -> b
$ \ShortText
s -> case ShortText -> Either Text ShortText
TS64U.decodeBase64Unpadded ShortText
s of
    Left Text
_ -> Maybe ShortText
forall a. Maybe a
Nothing
    Right ShortText
a -> ShortText -> Maybe ShortText
forall a. a -> Maybe a
Just ShortText
a
{-# INLINE _Base64UrlUnpadded #-}

-- | An 'Iso'' into the Base64 encoding of a 'ShortText' value
-- using lenient decoding.
--
--
-- _Note:_ This is not a lawful 'Iso' in general. Please take care!
--
-- >>> _Base64Lenient # "Sun"
-- "U3Vu"
--
-- >>> "U3Vu" ^. _Base64Lenient
-- "Sun"
--
_Base64Lenient :: Iso' ShortText ShortText
_Base64Lenient :: p ShortText (f ShortText) -> p ShortText (f ShortText)
_Base64Lenient = (ShortText -> ShortText)
-> (ShortText -> ShortText)
-> Iso ShortText ShortText ShortText ShortText
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ShortText -> ShortText
TS64.decodeBase64Lenient ShortText -> ShortText
TS64.encodeBase64

-- | An 'Iso'' into the Base64url encoding of a 'ShortText' value
-- using lenient decoding.
--
--
-- _Note:_ This is not a lawful 'Iso' in general. Please take care!
--
-- >>> _Base64UrlLenient # "<<??>>"
-- "PDw_Pz4-"
--
-- >>> "PDw_Pz4-" ^. _Base64UrlLenient
-- "<<??>>"
--
_Base64UrlLenient :: Iso' ShortText ShortText
_Base64UrlLenient :: p ShortText (f ShortText) -> p ShortText (f ShortText)
_Base64UrlLenient = (ShortText -> ShortText)
-> (ShortText -> ShortText)
-> Iso ShortText ShortText ShortText ShortText
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ShortText -> ShortText
TS64U.decodeBase64Lenient ShortText -> ShortText
TS64U.encodeBase64

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

-- | Unidirectional pattern synonym for base64-encoded 'ShortText' values.
--
pattern Base64 :: ShortText -> ShortText
pattern $bBase64 :: ShortText -> ShortText
$mBase64 :: forall r. ShortText -> (ShortText -> r) -> (Void# -> r) -> r
Base64 a <- (preview _Base64 -> Just a) where
    Base64 ShortText
a = Tagged ShortText (Identity ShortText)
-> Tagged ShortText (Identity ShortText)
Prism ShortText ShortText ShortText ShortText
_Base64 (Tagged ShortText (Identity ShortText)
 -> Tagged ShortText (Identity ShortText))
-> ShortText -> ShortText
forall t b. AReview t b -> b -> t
# ShortText
a

-- | Unidirectional pattern synonym for base64url-encoded 'ShortText' values.
--
pattern Base64Url :: ShortText -> ShortText
pattern $bBase64Url :: ShortText -> ShortText
$mBase64Url :: forall r. ShortText -> (ShortText -> r) -> (Void# -> r) -> r
Base64Url a <- (preview _Base64Url -> Just a) where
    Base64Url ShortText
a = Tagged ShortText (Identity ShortText)
-> Tagged ShortText (Identity ShortText)
Prism ShortText ShortText ShortText ShortText
_Base64Url (Tagged ShortText (Identity ShortText)
 -> Tagged ShortText (Identity ShortText))
-> ShortText -> ShortText
forall t b. AReview t b -> b -> t
# ShortText
a

-- | Unidirectional pattern synonym for unpadded base64url-encoded 'ShortText' values.
--
pattern Base64UrlUnpadded :: ShortText -> ShortText
pattern $bBase64UrlUnpadded :: ShortText -> ShortText
$mBase64UrlUnpadded :: forall r. ShortText -> (ShortText -> r) -> (Void# -> r) -> r
Base64UrlUnpadded a <- (preview _Base64UrlUnpadded -> Just a) where
    Base64UrlUnpadded ShortText
a = Tagged ShortText (Identity ShortText)
-> Tagged ShortText (Identity ShortText)
Prism ShortText ShortText ShortText ShortText
_Base64UrlUnpadded (Tagged ShortText (Identity ShortText)
 -> Tagged ShortText (Identity ShortText))
-> ShortText -> ShortText
forall t b. AReview t b -> b -> t
# ShortText
a

-- | Bidirectional pattern synonym for leniently Base64-encoded 'ShortText' values
--
pattern Base64Lenient :: ShortText -> ShortText
pattern $bBase64Lenient :: ShortText -> ShortText
$mBase64Lenient :: forall r. ShortText -> (ShortText -> r) -> (Void# -> r) -> r
Base64Lenient a <- (view (from _Base64Lenient) -> a) where
    Base64Lenient ShortText
a = Getting ShortText ShortText ShortText -> ShortText -> ShortText
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting ShortText ShortText ShortText
Iso ShortText ShortText ShortText ShortText
_Base64Lenient ShortText
a
{-# COMPLETE Base64Lenient #-}

-- | Bidirectional pattern synonym for leniently Base64-encoded 'ShortText' values
--
pattern Base64UrlLenient :: ShortText -> ShortText
pattern $bBase64UrlLenient :: ShortText -> ShortText
$mBase64UrlLenient :: forall r. ShortText -> (ShortText -> r) -> (Void# -> r) -> r
Base64UrlLenient a <- (view (from _Base64UrlLenient) -> a) where
    Base64UrlLenient ShortText
a = Getting ShortText ShortText ShortText -> ShortText -> ShortText
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting ShortText ShortText ShortText
Iso ShortText ShortText ShortText ShortText
_Base64UrlLenient ShortText
a
{-# COMPLETE Base64UrlLenient #-}