-- |
-- Module      : Amazonka.Data.Base64
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : provisional
-- Portability : non-portable (GHC extensions)
module Amazonka.Data.Base64
  ( Base64 (..),
    _Base64,
  )
where

import qualified Amazonka.Bytes as Bytes
import Amazonka.Data.Body
import Amazonka.Data.ByteString
import Amazonka.Data.JSON
import Amazonka.Data.Query
import Amazonka.Data.Text
import Amazonka.Data.XML
import Amazonka.Lens (iso)
import Amazonka.Prelude
import qualified Data.Text.Encoding as Text

-- | Base64 encoded binary data.
--
-- Encoding\/decoding is automatically deferred to serialisation and deserialisation
-- respectively.
newtype Base64 = Base64 {Base64 -> ByteString
unBase64 :: ByteString}
  deriving stock (Base64 -> Base64 -> Bool
(Base64 -> Base64 -> Bool)
-> (Base64 -> Base64 -> Bool) -> Eq Base64
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Base64 -> Base64 -> Bool
$c/= :: Base64 -> Base64 -> Bool
== :: Base64 -> Base64 -> Bool
$c== :: Base64 -> Base64 -> Bool
Eq, ReadPrec [Base64]
ReadPrec Base64
Int -> ReadS Base64
ReadS [Base64]
(Int -> ReadS Base64)
-> ReadS [Base64]
-> ReadPrec Base64
-> ReadPrec [Base64]
-> Read Base64
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Base64]
$creadListPrec :: ReadPrec [Base64]
readPrec :: ReadPrec Base64
$creadPrec :: ReadPrec Base64
readList :: ReadS [Base64]
$creadList :: ReadS [Base64]
readsPrec :: Int -> ReadS Base64
$creadsPrec :: Int -> ReadS Base64
Read, Eq Base64
Eq Base64
-> (Base64 -> Base64 -> Ordering)
-> (Base64 -> Base64 -> Bool)
-> (Base64 -> Base64 -> Bool)
-> (Base64 -> Base64 -> Bool)
-> (Base64 -> Base64 -> Bool)
-> (Base64 -> Base64 -> Base64)
-> (Base64 -> Base64 -> Base64)
-> Ord Base64
Base64 -> Base64 -> Bool
Base64 -> Base64 -> Ordering
Base64 -> Base64 -> Base64
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Base64 -> Base64 -> Base64
$cmin :: Base64 -> Base64 -> Base64
max :: Base64 -> Base64 -> Base64
$cmax :: Base64 -> Base64 -> Base64
>= :: Base64 -> Base64 -> Bool
$c>= :: Base64 -> Base64 -> Bool
> :: Base64 -> Base64 -> Bool
$c> :: Base64 -> Base64 -> Bool
<= :: Base64 -> Base64 -> Bool
$c<= :: Base64 -> Base64 -> Bool
< :: Base64 -> Base64 -> Bool
$c< :: Base64 -> Base64 -> Bool
compare :: Base64 -> Base64 -> Ordering
$ccompare :: Base64 -> Base64 -> Ordering
$cp1Ord :: Eq Base64
Ord, (forall x. Base64 -> Rep Base64 x)
-> (forall x. Rep Base64 x -> Base64) -> Generic Base64
forall x. Rep Base64 x -> Base64
forall x. Base64 -> Rep Base64 x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Base64 x -> Base64
$cfrom :: forall x. Base64 -> Rep Base64 x
Generic)

instance Hashable Base64

instance NFData Base64

_Base64 :: Iso' Base64 ByteString
_Base64 :: p ByteString (f ByteString) -> p Base64 (f Base64)
_Base64 = (Base64 -> ByteString)
-> (ByteString -> Base64)
-> Iso Base64 Base64 ByteString ByteString
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Base64 -> ByteString
unBase64 ByteString -> Base64
Base64

-- FIXME: a mistake to wrap a ByteString since the underlying serialisers
-- (JSON, XML) use Text internally.
instance FromText Base64 where
  fromText :: Text -> Either String Base64
fromText = (ByteString -> Base64)
-> Either String ByteString -> Either String Base64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Base64
Base64 (Either String ByteString -> Either String Base64)
-> (Text -> Either String ByteString)
-> Text
-> Either String Base64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either String ByteString
forall a. ByteArrayAccess a => a -> Either String ByteString
Bytes.decodeBase64 (ByteString -> Either String ByteString)
-> (Text -> ByteString) -> Text -> Either String ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
Text.encodeUtf8

instance ToByteString Base64 where
  toBS :: Base64 -> ByteString
toBS = ByteString -> ByteString
forall a. ByteArrayAccess a => a -> ByteString
Bytes.encodeBase64 (ByteString -> ByteString)
-> (Base64 -> ByteString) -> Base64 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base64 -> ByteString
unBase64

instance Show Base64 where
  show :: Base64 -> String
show = ByteString -> String
forall a. Show a => a -> String
show (ByteString -> String)
-> (Base64 -> ByteString) -> Base64 -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base64 -> ByteString
forall a. ToByteString a => a -> ByteString
toBS

instance ToText Base64 where
  toText :: Base64 -> Text
toText = ByteString -> Text
Text.decodeUtf8 (ByteString -> Text) -> (Base64 -> ByteString) -> Base64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base64 -> ByteString
forall a. ToByteString a => a -> ByteString
toBS

instance ToQuery Base64 where
  toQuery :: Base64 -> QueryString
toQuery = ByteString -> QueryString
forall a. ToQuery a => a -> QueryString
toQuery (ByteString -> QueryString)
-> (Base64 -> ByteString) -> Base64 -> QueryString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base64 -> ByteString
forall a. ToByteString a => a -> ByteString
toBS

instance FromXML Base64 where
  parseXML :: [Node] -> Either String Base64
parseXML = String -> [Node] -> Either String Base64
forall a. FromText a => String -> [Node] -> Either String a
parseXMLText String
"Base64"

instance ToXML Base64 where
  toXML :: Base64 -> XML
toXML = Base64 -> XML
forall a. ToText a => a -> XML
toXMLText

instance FromJSON Base64 where
  parseJSON :: Value -> Parser Base64
parseJSON = String -> Value -> Parser Base64
forall a. FromText a => String -> Value -> Parser a
parseJSONText String
"Base64"

instance ToJSON Base64 where
  toJSON :: Base64 -> Value
toJSON = Base64 -> Value
forall a. ToText a => a -> Value
toJSONText

instance ToHashedBody Base64 where
  toHashed :: Base64 -> HashedBody
toHashed = ByteString -> HashedBody
forall a. ToHashedBody a => a -> HashedBody
toHashed (ByteString -> HashedBody)
-> (Base64 -> ByteString) -> Base64 -> HashedBody
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Base64 -> ByteString
forall a. ToByteString a => a -> ByteString
toBS

instance ToBody Base64