{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}

module Dormouse.Uri
  ( module Dormouse.Uri.Types
  , parseUri
  ) 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

-- | Parse an ascii 'ByteString' as an absolute uri, throwing a 'UriException' in @m@ if this fails
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