{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
module Dormouse.Uri
( module Dormouse.Uri.Types
, parseUri
, parseUriRef
) where
import Control.Exception.Safe (MonadThrow, throw)
import qualified Data.ByteString as SB
import Data.Attoparsec.ByteString.Char8 (parseOnly)
import qualified Data.Text as T
import Dormouse.Uri.Exception (UriException(..))
import Dormouse.Uri.Parser
import Dormouse.Uri.Types
parseUri :: MonadThrow m => SB.ByteString -> m Uri
parseUri :: ByteString -> m Uri
parseUri ByteString
bs = (String -> m Uri) -> (Uri -> m Uri) -> Either String Uri -> m Uri
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (UriException -> m Uri
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throw (UriException -> m Uri)
-> (String -> UriException) -> String -> m Uri
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> UriException
UriException (Text -> UriException)
-> (String -> Text) -> String -> UriException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack) Uri -> m Uri
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String Uri -> m Uri) -> Either String Uri -> m Uri
forall a b. (a -> b) -> a -> b
$ Parser Uri -> ByteString -> Either String Uri
forall a. Parser a -> ByteString -> Either String a
parseOnly Parser Uri
pUri ByteString
bs
parseUriRef :: MonadThrow m => SB.ByteString -> m UriReference
parseUriRef :: ByteString -> m UriReference
parseUriRef ByteString
bs = (String -> m UriReference)
-> (UriReference -> m UriReference)
-> Either String UriReference
-> m UriReference
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (UriException -> m UriReference
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throw (UriException -> m UriReference)
-> (String -> UriException) -> String -> m UriReference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> UriException
UriException (Text -> UriException)
-> (String -> Text) -> String -> UriException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack) UriReference -> m UriReference
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String UriReference -> m UriReference)
-> Either String UriReference -> m UriReference
forall a b. (a -> b) -> a -> b
$ Parser UriReference -> ByteString -> Either String UriReference
forall a. Parser a -> ByteString -> Either String a
parseOnly Parser UriReference
pUriRef ByteString
bs