{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
#if MIN_VERSION_lens(5,0,0)
{-# LANGUAGE Safe #-}
#else
{-# LANGUAGE Trustworthy #-}
#endif
-- |
-- Module       : Data.ByteString.Short.Base32.Lens
-- Copyright 	: (c) 2019 Emily Pillmore
-- License	: BSD-style
--
-- Maintainer	: Emily Pillmore <emilypi@cohomolo.gy>
-- Stability	: Experimental
-- Portability	: non-portable
--
-- This module contains 'Prism''s for Base32-encoding and
-- decoding 'ShortByteString' values.
--
module Data.ByteString.Short.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.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short.Base32 as SB32
import qualified Data.ByteString.Short.Base32.Hex as SB32H


-- $setup
--
-- >>> import Control.Lens
-- >>> import Data.ByteString.Short.Base32.Lens
--
-- >>> :set -XOverloadedStrings
-- >>> :set -XTypeApplications


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

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

-- | A 'Prism'' into the Base32 encoding of a 'ShortByteString' 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' ShortByteString ShortByteString
_Base32Unpadded :: p ShortByteString (f ShortByteString)
-> p ShortByteString (f ShortByteString)
_Base32Unpadded = (ShortByteString -> ShortByteString)
-> (ShortByteString -> Maybe ShortByteString)
-> Prism
     ShortByteString ShortByteString ShortByteString ShortByteString
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' ShortByteString -> ShortByteString
SB32.encodeBase32Unpadded' ((ShortByteString -> Maybe ShortByteString)
 -> Prism
      ShortByteString ShortByteString ShortByteString ShortByteString)
-> (ShortByteString -> Maybe ShortByteString)
-> Prism
     ShortByteString ShortByteString ShortByteString ShortByteString
forall a b. (a -> b) -> a -> b
$ \ShortByteString
s -> case ShortByteString -> Either Text ShortByteString
SB32.decodeBase32Unpadded ShortByteString
s of
    Left Text
_ -> Maybe ShortByteString
forall a. Maybe a
Nothing
    Right ShortByteString
a -> ShortByteString -> Maybe ShortByteString
forall a. a -> Maybe a
Just ShortByteString
a
{-# INLINE _Base32Unpadded #-}

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

-- | A 'Prism'' into the Base32hex encoding of a 'ShortByteString' 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' ShortByteString ShortByteString
_Base32HexUnpadded :: p ShortByteString (f ShortByteString)
-> p ShortByteString (f ShortByteString)
_Base32HexUnpadded = (ShortByteString -> ShortByteString)
-> (ShortByteString -> Maybe ShortByteString)
-> Prism
     ShortByteString ShortByteString ShortByteString ShortByteString
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' ShortByteString -> ShortByteString
SB32H.encodeBase32Unpadded' ((ShortByteString -> Maybe ShortByteString)
 -> Prism
      ShortByteString ShortByteString ShortByteString ShortByteString)
-> (ShortByteString -> Maybe ShortByteString)
-> Prism
     ShortByteString ShortByteString ShortByteString ShortByteString
forall a b. (a -> b) -> a -> b
$ \ShortByteString
s -> case ShortByteString -> Either Text ShortByteString
SB32H.decodeBase32Unpadded ShortByteString
s of
    Left Text
_ -> Maybe ShortByteString
forall a. Maybe a
Nothing
    Right ShortByteString
a -> ShortByteString -> Maybe ShortByteString
forall a. a -> Maybe a
Just ShortByteString
a
{-# INLINE _Base32HexUnpadded #-}

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

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

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

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

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

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

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

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

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