{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
#if MIN_VERSION_lens(5,0,0)
{-# LANGUAGE Safe #-}
#else
{-# LANGUAGE Trustworthy #-}
#endif
-- |
-- Module       : Data.ByteString.Short.Base16.Lens
-- Copyright    : (c) 2019-2020 Emily Pillmore
-- License      : BSD-style
--
-- Maintainer   : Emily Pillmore <emilypi@cohomolo.gy>
-- Stability    : Experimental
-- Portability  : non-portable
--
-- This module contains 'Prism''s and 'Iso''s for Base16-encoding and
-- decoding 'ShortByteString' values.
--
module Data.ByteString.Short.Base16.Lens
( -- * Prisms
  _Hex
, _Base16
  -- * Isos
, _Base16Lenient
  -- * Patterns
, pattern Hex
, pattern Base16
, pattern Base16Lenient
) where


import Control.Lens

import Data.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short.Base16 as B16S


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


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

-- | A 'Prism'' into the Base16 encoding of a 'ShortByteString' value.
--
-- >>> _Base16 # "Sun"
-- "53756e"
--
-- >>> "53756e" ^? _Base16
-- Just "Sun"
--
_Base16 :: Prism' ShortByteString ShortByteString
_Base16 :: p ShortByteString (f ShortByteString)
-> p ShortByteString (f ShortByteString)
_Base16 = (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
B16S.encodeBase16' ((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
B16S.decodeBase16 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 _Base16 #-}

-- | A 'Prism'' into the Base16 encoding of a lazy 'ShortByteString' value. This function
-- is an alias of '_Base16'.
--
-- >>> _Hex # "Sun"
-- "53756e"
--
-- >>> "53756e" ^? _Hex
-- Just "Sun"
--
_Hex :: Prism' ShortByteString ShortByteString
_Hex :: p ShortByteString (f ShortByteString)
-> p ShortByteString (f ShortByteString)
_Hex = (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
B16S.encodeBase16' ((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
B16S.decodeBase16 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 _Hex #-}

-- | A 'Iso'' into the Base16 encoding of a leniently decoded 'ShortByteString' value.
--
-- >>> _Base16Lenient # "Sun"
-- "53756e"
--
-- >>> "53756e" ^. _Base16
-- "Sun"
--
_Base16Lenient :: Iso' ShortByteString ShortByteString
_Base16Lenient :: p ShortByteString (f ShortByteString)
-> p ShortByteString (f ShortByteString)
_Base16Lenient = (ShortByteString -> ShortByteString)
-> (ShortByteString -> ShortByteString)
-> Iso
     ShortByteString ShortByteString ShortByteString ShortByteString
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ShortByteString -> ShortByteString
B16S.decodeBase16Lenient ShortByteString -> ShortByteString
B16S.encodeBase16'
{-# INLINE _Base16Lenient #-}

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

-- | Bidirectional pattern synonym for Base16-encoded lazy 'ShortByteString' values.
--
pattern Hex :: ShortByteString -> ShortByteString
pattern $bHex :: ShortByteString -> ShortByteString
$mHex :: forall r.
ShortByteString -> (ShortByteString -> r) -> (Void# -> r) -> r
Hex a <- (preview _Hex -> Just a) where
    Hex ShortByteString
a = Tagged ShortByteString (Identity ShortByteString)
-> Tagged ShortByteString (Identity ShortByteString)
Prism
  ShortByteString ShortByteString ShortByteString ShortByteString
_Hex (Tagged ShortByteString (Identity ShortByteString)
 -> Tagged ShortByteString (Identity ShortByteString))
-> ShortByteString -> ShortByteString
forall t b. AReview t b -> b -> t
# ShortByteString
a

-- | Bidirectional pattern synonym for Base16-encoded lazy 'ShortByteString' values.
--
pattern Base16 :: ShortByteString -> ShortByteString
pattern $bBase16 :: ShortByteString -> ShortByteString
$mBase16 :: forall r.
ShortByteString -> (ShortByteString -> r) -> (Void# -> r) -> r
Base16 a <- (preview _Base16 -> Just a) where
    Base16 ShortByteString
a = Tagged ShortByteString (Identity ShortByteString)
-> Tagged ShortByteString (Identity ShortByteString)
Prism
  ShortByteString ShortByteString ShortByteString ShortByteString
_Base16 (Tagged ShortByteString (Identity ShortByteString)
 -> Tagged ShortByteString (Identity ShortByteString))
-> ShortByteString -> ShortByteString
forall t b. AReview t b -> b -> t
# ShortByteString
a

-- | Bidirectional pattern synonym for Base16-encoded 'ShortByteString' values.
--
pattern Base16Lenient :: ShortByteString -> ShortByteString
pattern $bBase16Lenient :: ShortByteString -> ShortByteString
$mBase16Lenient :: forall r.
ShortByteString -> (ShortByteString -> r) -> (Void# -> r) -> r
Base16Lenient a <- (view _Base16Lenient -> a) where
    Base16Lenient ShortByteString
a = Tagged ShortByteString (Identity ShortByteString)
-> Tagged ShortByteString (Identity ShortByteString)
Iso ShortByteString ShortByteString ShortByteString ShortByteString
_Base16Lenient (Tagged ShortByteString (Identity ShortByteString)
 -> Tagged ShortByteString (Identity ShortByteString))
-> ShortByteString -> ShortByteString
forall t b. AReview t b -> b -> t
# ShortByteString
a
{-# COMPLETE Base16Lenient #-}