{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
#if MIN_VERSION_lens(5,0,0)
{-# LANGUAGE Safe #-}
#else
{-# LANGUAGE Trustworthy #-}
#endif
-- |
-- Module       : Data.Text.Short.Encoding.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 Base16-encoding and
-- decoding 'ShortText' values.
--
module Data.Text.Short.Encoding.Base16.Lens
( -- * Prisms
  _Hex
, _Base16
, _Base16Lenient
  -- * Patterns
, pattern Hex
, pattern Base16
, pattern Base16Lenient
) where

import Control.Lens

import Data.Text.Short (ShortText)
import qualified Data.Text.Short.Encoding.Base16 as B16TS

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

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

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

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

-- | A 'Iso'' into the Base16 encoding of a 'ShortText' value
--
-- >>> _Base16Lenient # "Sun"
-- "53756e"
--
-- >>> "53756e" ^. _Base16Lenient
-- "Sun"
--
_Base16Lenient :: Iso' ShortText ShortText
_Base16Lenient :: p ShortText (f ShortText) -> p ShortText (f ShortText)
_Base16Lenient = (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
B16TS.decodeBase16Lenient ShortText -> ShortText
B16TS.encodeBase16
{-# INLINE _Base16Lenient #-}

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

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

-- | Bidirectional pattern synonym for Base16-encoded 'ShortText' values.
--
pattern Base16 :: ShortText -> ShortText
pattern $bBase16 :: ShortText -> ShortText
$mBase16 :: forall r. ShortText -> (ShortText -> r) -> (Void# -> r) -> r
Base16 a <- (preview _Base16 -> Just a) where
    Base16 ShortText
a = Tagged ShortText (Identity ShortText)
-> Tagged ShortText (Identity ShortText)
Prism ShortText ShortText ShortText ShortText
_Base16 (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 decoded,
-- Base16-encoded 'ShortText' values.
--
pattern Base16Lenient :: ShortText -> ShortText
pattern $bBase16Lenient :: ShortText -> ShortText
$mBase16Lenient :: forall r. ShortText -> (ShortText -> r) -> (Void# -> r) -> r
Base16Lenient a <- (view _Base16Lenient -> a) where
    Base16Lenient ShortText
a = Tagged ShortText (Identity ShortText)
-> Tagged ShortText (Identity ShortText)
Iso ShortText ShortText ShortText ShortText
_Base16Lenient (Tagged ShortText (Identity ShortText)
 -> Tagged ShortText (Identity ShortText))
-> ShortText -> ShortText
forall t b. AReview t b -> b -> t
# ShortText
a
{-# COMPLETE Base16Lenient #-}