{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}
module Text.URI.QQ
( uri,
scheme,
host,
username,
password,
pathPiece,
queryKey,
queryValue,
fragment,
)
where
import Control.Exception (Exception (..), SomeException)
import Data.Text (Text)
import qualified Data.Text as T
import Language.Haskell.TH.Lib (appE, viewP)
import Language.Haskell.TH.Quote (QuasiQuoter (..))
import Language.Haskell.TH.Syntax (Lift (..))
import Text.URI.Parser.Text
import Text.URI.Types
uri :: QuasiQuoter
uri :: QuasiQuoter
uri = (Text -> Either SomeException URI) -> QuasiQuoter
forall a.
(Eq a, Lift a) =>
(Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException URI
forall (m :: * -> *). MonadThrow m => Text -> m URI
mkURI
scheme :: QuasiQuoter
scheme :: QuasiQuoter
scheme = (Text -> Either SomeException (RText 'Scheme)) -> QuasiQuoter
forall a.
(Eq a, Lift a) =>
(Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException (RText 'Scheme)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'Scheme)
mkScheme
host :: QuasiQuoter
host :: QuasiQuoter
host = (Text -> Either SomeException (RText 'Host)) -> QuasiQuoter
forall a.
(Eq a, Lift a) =>
(Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException (RText 'Host)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'Host)
mkHost
username :: QuasiQuoter
username :: QuasiQuoter
username = (Text -> Either SomeException (RText 'Username)) -> QuasiQuoter
forall a.
(Eq a, Lift a) =>
(Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException (RText 'Username)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'Username)
mkUsername
password :: QuasiQuoter
password :: QuasiQuoter
password = (Text -> Either SomeException (RText 'Password)) -> QuasiQuoter
forall a.
(Eq a, Lift a) =>
(Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException (RText 'Password)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'Password)
mkPassword
pathPiece :: QuasiQuoter
pathPiece :: QuasiQuoter
pathPiece = (Text -> Either SomeException (RText 'PathPiece)) -> QuasiQuoter
forall a.
(Eq a, Lift a) =>
(Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException (RText 'PathPiece)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'PathPiece)
mkPathPiece
queryKey :: QuasiQuoter
queryKey :: QuasiQuoter
queryKey = (Text -> Either SomeException (RText 'QueryKey)) -> QuasiQuoter
forall a.
(Eq a, Lift a) =>
(Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException (RText 'QueryKey)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'QueryKey)
mkQueryKey
queryValue :: QuasiQuoter
queryValue :: QuasiQuoter
queryValue = (Text -> Either SomeException (RText 'QueryValue)) -> QuasiQuoter
forall a.
(Eq a, Lift a) =>
(Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException (RText 'QueryValue)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'QueryValue)
mkQueryValue
fragment :: QuasiQuoter
fragment :: QuasiQuoter
fragment = (Text -> Either SomeException (RText 'Fragment)) -> QuasiQuoter
forall a.
(Eq a, Lift a) =>
(Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException (RText 'Fragment)
forall (m :: * -> *). MonadThrow m => Text -> m (RText 'Fragment)
mkFragment
liftToQQ :: (Eq a, Lift a) => (Text -> Either SomeException a) -> QuasiQuoter
liftToQQ :: (Text -> Either SomeException a) -> QuasiQuoter
liftToQQ Text -> Either SomeException a
f =
QuasiQuoter :: (String -> Q Exp)
-> (String -> Q Pat)
-> (String -> Q Type)
-> (String -> Q [Dec])
-> QuasiQuoter
QuasiQuoter
{ quoteExp :: String -> Q Exp
quoteExp = \String
str ->
case Text -> Either SomeException a
f (String -> Text
T.pack String
str) of
Left SomeException
err -> String -> Q Exp
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (SomeException -> String
forall e. Exception e => e -> String
displayException SomeException
err)
Right a
x -> a -> Q Exp
forall t. Lift t => t -> Q Exp
lift a
x,
quotePat :: String -> Q Pat
quotePat = \String
str ->
case Text -> Either SomeException a
f (String -> Text
T.pack String
str) of
Left SomeException
err -> String -> Q Pat
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (SomeException -> String
forall e. Exception e => e -> String
displayException SomeException
err)
Right a
x -> Q Exp -> Q Exp -> Q Exp
appE [|(==)|] (a -> Q Exp
forall t. Lift t => t -> Q Exp
lift a
x) Q Exp -> Q Pat -> Q Pat
`viewP` [p|True|],
quoteType :: String -> Q Type
quoteType = String -> String -> Q Type
forall a. HasCallStack => String -> a
error String
"This usage is not supported",
quoteDec :: String -> Q [Dec]
quoteDec = String -> String -> Q [Dec]
forall a. HasCallStack => String -> a
error String
"This usage is not supported"
}