{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
-- |
-- Module       : Data.Text.Encoding.Base16.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 and 'Iso''s Base16-encoding and
-- decoding lazy 'Text' values.
--
module Data.Text.Lazy.Encoding.Base16.Lens
( -- * Prisms
  _Base16
, _Hex
  -- * Isos
, _Base16Lenient
  -- * Patterns
, pattern Hex
, pattern Base16
, pattern Base16Lenient
) where

import Control.Lens

import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy.Encoding.Base16 as B16TL


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

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

-- | A 'Prism'' into the Base16 encoding of a lazy 'Text' value.
--
-- >>> _Base16 # "Sun"
-- "53756e"
--
-- >>> "53756e" ^? _Base16
-- Just "Sun"
--
_Base16 :: Prism' Text Text
_Base16 :: p Text (f Text) -> p Text (f Text)
_Base16 = (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
B16TL.encodeBase16 ((Text -> Maybe Text) -> p Text (f Text) -> p Text (f Text))
-> (Text -> Maybe Text) -> p Text (f Text) -> p Text (f Text)
forall a b. (a -> b) -> a -> b
$ \s :: Text
s -> case Text -> Either Text Text
B16TL.decodeBase16 Text
s of
    Left _ -> Maybe Text
forall a. Maybe a
Nothing
    Right a :: Text
a -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
a
{-# INLINE _Base16 #-}

-- | A 'Prism'' into the Base16 encoding of a lazy 'Text' value. This
-- function is an alias for '_Base16'.
--
-- >>> _Hex # "Sun"
-- "53756e"
--
-- >>> "53756e" ^? _Hex
-- Just "Sun"
--
_Hex :: Prism' Text Text
_Hex :: p Text (f Text) -> p Text (f Text)
_Hex = (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
B16TL.encodeBase16 ((Text -> Maybe Text) -> p Text (f Text) -> p Text (f Text))
-> (Text -> Maybe Text) -> p Text (f Text) -> p Text (f Text)
forall a b. (a -> b) -> a -> b
$ \s :: Text
s -> case Text -> Either Text Text
B16TL.decodeBase16 Text
s of
    Left _ -> Maybe Text
forall a. Maybe a
Nothing
    Right a :: Text
a -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
a
{-# INLINE _Hex #-}

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

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

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

-- | Bidirectional pattern synonym for Base16-encoded lazy 'Text' values.
--
pattern Base16 :: Text -> Text
pattern $bBase16 :: Text -> Text
$mBase16 :: forall r. Text -> (Text -> r) -> (Void# -> r) -> r
Base16 a <- (preview _Base16 -> Just a) where
    Base16 a :: Text
a = Tagged Text (Identity Text) -> Tagged Text (Identity Text)
Prism Text Text Text Text
_Base16 (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 decoded,
-- Base16-encoded 'ByteString' values.
--
pattern Base16Lenient :: Text -> Text
pattern $bBase16Lenient :: Text -> Text
$mBase16Lenient :: forall r. Text -> (Text -> r) -> (Void# -> r) -> r
Base16Lenient a <- (view (from _Base16Lenient) -> a) where
    Base16Lenient a :: Text
a = Getting Text Text Text -> Text -> Text
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Text Text Text
Prism Text Text Text Text
_Base16 Text
a