{-# 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 Base16-encoding and
-- decoding 'Text' values.
--
module Data.Text.Encoding.Base16.Lens
( -- * Prisms
  _Hex
, _Base16
  -- * Patterns
, pattern Hex
, pattern Base16
) where

import Control.Lens

import Data.Text (Text)
import qualified Data.Text.Encoding.Base16 as B16T


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

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

-- | A 'Prism'' into the Base16 encoding of a 'Text' value. This 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
B16T.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
B16T.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 'Prism'' into the Base16 encoding of a '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
B16T.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
B16T.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 #-}

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

-- | Bidirectional pattern synonym for Base16-encoded '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 '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