module Network.HTTP.Media.Encoding.Internal
( Encoding (..)
) where
import qualified Data.ByteString.Char8 as BS
import qualified Data.CaseInsensitive as CI
import Control.Monad (guard)
import Data.ByteString (ByteString)
import Data.CaseInsensitive (CI, original)
import Data.Maybe (fromMaybe)
import Data.String (IsString (..))
import Network.HTTP.Media.Accept (Accept (..))
import Network.HTTP.Media.RenderHeader (RenderHeader (..))
import Network.HTTP.Media.Utils (isValidToken)
newtype Encoding = Encoding (CI ByteString)
deriving (Eq, Ord)
instance Show Encoding where
show = BS.unpack . renderHeader
instance IsString Encoding where
fromString str = flip fromMaybe (parseAccept $ BS.pack str) $
error $ "Invalid encoding literal " ++ str
instance Accept Encoding where
parseAccept "" = Just $ Encoding "identity"
parseAccept bs = do
guard $ isValidToken bs
return $ Encoding (CI.mk bs)
matches _ (Encoding "*") = True
matches a b = a == b
moreSpecificThan _ (Encoding "*") = True
moreSpecificThan _ _ = False
instance RenderHeader Encoding where
renderHeader (Encoding e) = original e