module Network.HTTP.Kinder.URI (
URIEncode (..)
, URIDecode (..)
, uriDecodeBS
) where
import qualified Data.ByteString as S
import Data.Int
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Enc
import Data.Text.Read (decimal)
import Network.HTTP.Kinder.Common
class URIEncode a where
uriEncode :: a -> Text
class URIDecode a where
uriDecode :: Text -> Either String a
uriDecodeBS :: URIDecode a => S.ByteString -> Either String a
uriDecodeBS s = case Enc.decodeUtf8' s of
Left _err -> Left "could not parse UTF8 string"
Right a -> uriDecode a
instance URIDecode (Raw Text) where
uriDecode text = Right (Raw text)
instance URIDecode Text where
uriDecode text = Right text
uriDecodeDecimal :: (Show a, Integral a) => Text -> Either String a
uriDecodeDecimal txt =
case decimal txt of
Left err -> Left err
Right (a, t)
| Text.null t -> Right a
| otherwise -> Left ("incomplete parse: " ++ show (a, t))
instance URIDecode Int where uriDecode = uriDecodeDecimal
instance URIDecode Int8 where uriDecode = uriDecodeDecimal
instance URIDecode Int16 where uriDecode = uriDecodeDecimal
instance URIDecode Int32 where uriDecode = uriDecodeDecimal
instance URIDecode Int64 where uriDecode = uriDecodeDecimal
instance URIDecode Integer where uriDecode = uriDecodeDecimal