module Network.URI.Template
( Template
, TemplateError (..)
, templateErrorPretty
, processTemplate
, module Network.URI.Template.VarName
, module Network.URI.Template.VarValue
) where
import Prelude
import Data.Bifunctor (first)
import Data.Map.Strict (Map)
import Data.Text (Text)
import Network.URI.Template.Expand
import Network.URI.Template.Internal
import Network.URI.Template.Internal.Parse
import Network.URI.Template.Parse
import Network.URI.Template.VarName
import Network.URI.Template.VarValue
newtype TemplateError = TemplateParseError ParseError
templateErrorPretty :: TemplateError -> String
templateErrorPretty :: TemplateError -> String
templateErrorPretty = \case
TemplateParseError ParseError
pe -> ParseError -> String
errorBundlePretty ParseError
pe
processTemplate :: Map VarName VarValue -> Text -> Either TemplateError Text
processTemplate :: Map VarName VarValue -> Text -> Either TemplateError Text
processTemplate Map VarName VarValue
env Text
t = do
Template
template <- (ParseError -> TemplateError)
-> Either ParseError Template -> Either TemplateError Template
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ParseError -> TemplateError
TemplateParseError (Either ParseError Template -> Either TemplateError Template)
-> Either ParseError Template -> Either TemplateError Template
forall a b. (a -> b) -> a -> b
$ Text -> Either ParseError Template
parseTemplate Text
t
Text -> Either TemplateError Text
forall a. a -> Either TemplateError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Either TemplateError Text)
-> Text -> Either TemplateError Text
forall a b. (a -> b) -> a -> b
$ Map VarName VarValue -> Template -> Text
expandTemplate Map VarName VarValue
env Template
template