--

-- |
-- Module      : Amazonka.Data.ByteString
-- 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.ByteString
  ( -- * ByteString
    ByteString,
    ByteStringLazy,
    ToByteString (..),
    showBS,
    stripBS,
  )
where

import Amazonka.Data.Text
import Amazonka.Prelude
import qualified Data.ByteString.Builder as Build
import qualified Data.ByteString.Char8 as BS8
import qualified Data.ByteString.Lazy as LBS
import qualified Data.CaseInsensitive as CI
import qualified Data.Char as Char
import qualified Data.Text.Encoding as Text
import qualified Network.HTTP.Types as HTTP
import qualified Numeric

showBS :: ToByteString a => a -> String
showBS :: a -> String
showBS = ByteString -> String
BS8.unpack (ByteString -> String) -> (a -> ByteString) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a. ToByteString a => a -> ByteString
toBS

stripBS :: ByteString -> ByteString
stripBS :: ByteString -> ByteString
stripBS = (Char -> Bool) -> ByteString -> ByteString
BS8.dropWhile Char -> Bool
Char.isSpace (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString, ByteString) -> ByteString
forall a b. (a, b) -> a
fst ((ByteString, ByteString) -> ByteString)
-> (ByteString -> (ByteString, ByteString))
-> ByteString
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> ByteString -> (ByteString, ByteString)
BS8.spanEnd Char -> Bool
Char.isSpace

class ToByteString a where
  toBS :: a -> ByteString
  default toBS :: ToText a => a -> ByteString
  toBS = Text -> ByteString
Text.encodeUtf8 (Text -> ByteString) -> (a -> Text) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Text
forall a. ToText a => a -> Text
toText

instance ToByteString ByteString where
  toBS :: ByteString -> ByteString
toBS = ByteString -> ByteString
forall a. a -> a
id

instance ToByteString ByteStringBuilder where
  toBS :: ByteStringBuilder -> ByteString
toBS = ByteString -> ByteString
forall a. ToByteString a => a -> ByteString
toBS (ByteString -> ByteString)
-> (ByteStringBuilder -> ByteString)
-> ByteStringBuilder
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteStringBuilder -> ByteString
Build.toLazyByteString

instance ToByteString ByteStringLazy where
  toBS :: ByteString -> ByteString
toBS = ByteString -> ByteString
LBS.toStrict

instance ToByteString Text where
  toBS :: Text -> ByteString
toBS = Text -> ByteString
Text.encodeUtf8

instance ToByteString String where
  toBS :: String -> ByteString
toBS = String -> ByteString
BS8.pack

instance ToByteString Int where
  toBS :: Int -> ByteString
toBS = ByteStringBuilder -> ByteString
forall a. ToByteString a => a -> ByteString
toBS (ByteStringBuilder -> ByteString)
-> (Int -> ByteStringBuilder) -> Int -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteStringBuilder
Build.intDec

instance ToByteString Integer where
  toBS :: Integer -> ByteString
toBS = ByteStringBuilder -> ByteString
forall a. ToByteString a => a -> ByteString
toBS (ByteStringBuilder -> ByteString)
-> (Integer -> ByteStringBuilder) -> Integer -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> ByteStringBuilder
Build.integerDec

instance ToByteString Natural where
  toBS :: Natural -> ByteString
toBS = Integer -> ByteString
forall a. ToByteString a => a -> ByteString
toBS (Integer -> ByteString)
-> (Natural -> Integer) -> Natural -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Natural -> Integer
forall a. Integral a => a -> Integer
toInteger

instance ToByteString Double where
  toBS :: Double -> ByteString
toBS = String -> ByteString
forall a. ToByteString a => a -> ByteString
toBS (String -> ByteString)
-> (Double -> String) -> Double -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"") ((String -> String) -> String)
-> (Double -> String -> String) -> Double -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Int -> Double -> String -> String
forall a. RealFloat a => Maybe Int -> a -> String -> String
Numeric.showFFloat Maybe Int
forall a. Maybe a
Nothing

instance ToByteString HTTP.StdMethod where
  toBS :: StdMethod -> ByteString
toBS = StdMethod -> ByteString
HTTP.renderStdMethod

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

instance ToByteString a => ToByteString (CI a) where
  toBS :: CI a -> ByteString
toBS = a -> ByteString
forall a. ToByteString a => a -> ByteString
toBS (a -> ByteString) -> (CI a -> a) -> CI a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CI a -> a
forall s. CI s -> s
CI.original