{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}

module Dhall.URL where

import Data.Text (Text)

import Dhall.Syntax (Directory (..), File (..), Scheme (..), URL (..))

renderComponent :: Text -> Text
renderComponent :: Text -> Text
renderComponent Text
component = Text
"/" forall a. Semigroup a => a -> a -> a
<> Text
component

renderQuery :: Text -> Text
renderQuery :: Text -> Text
renderQuery Text
query = Text
"?" forall a. Semigroup a => a -> a -> a
<> Text
query

renderURL :: URL -> Text
renderURL :: URL -> Text
renderURL URL
url =
        Text
schemeText
    forall a. Semigroup a => a -> a -> a
<>  Text
authority
    forall a. Semigroup a => a -> a -> a
<>  Text
pathText
    forall a. Semigroup a => a -> a -> a
<>  Text
queryText
  where
    URL {Maybe Text
Maybe (Expr Src Import)
Text
Scheme
File
headers :: URL -> Maybe (Expr Src Import)
query :: URL -> Maybe Text
path :: URL -> File
authority :: URL -> Text
scheme :: URL -> Scheme
headers :: Maybe (Expr Src Import)
query :: Maybe Text
path :: File
scheme :: Scheme
authority :: Text
..} = URL
url

    File {Text
Directory
file :: File -> Text
directory :: File -> Directory
file :: Text
directory :: Directory
..} = File
path

    Directory {[Text]
components :: Directory -> [Text]
components :: [Text]
..} = Directory
directory

    schemeText :: Text
schemeText = case Scheme
scheme of
        Scheme
HTTP  -> Text
"http://"
        Scheme
HTTPS -> Text
"https://"

    pathText :: Text
pathText =
            forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Text -> Text
renderComponent (forall a. [a] -> [a]
reverse [Text]
components)
        forall a. Semigroup a => a -> a -> a
<>  Text -> Text
renderComponent Text
file

    queryText :: Text
queryText = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Text -> Text
renderQuery Maybe Text
query