    {-# LANGUAGE UnicodeSyntax, Safe #-}

    -- | The data type definitions for the HTTP Link header.
    module Network.HTTP.Link.Types where

    import           Data.Text (Text, pack, unpack)
    import           Network.URI (URI, parseURIReference)

    -- | The link attribute key.
    data LinkParam = Rel | Anchor | Rev | Hreflang | Media | Title | Title' | ContentType | Other Text
      deriving (LinkParam -> LinkParam -> Bool
    (LinkParam -> LinkParam -> Bool)
    -> (LinkParam -> LinkParam -> Bool) -> Eq LinkParam
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: LinkParam -> LinkParam -> Bool
    == :: LinkParam -> LinkParam -> Bool
    $c/= :: LinkParam -> LinkParam -> Bool
    /= :: LinkParam -> LinkParam -> Bool
    Eq, Int -> LinkParam -> ShowS
    [LinkParam] -> ShowS
    LinkParam -> String
    (Int -> LinkParam -> ShowS)
    -> (LinkParam -> String)
    -> ([LinkParam] -> ShowS)
    -> Show LinkParam
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> LinkParam -> ShowS
    showsPrec :: Int -> LinkParam -> ShowS
    $cshow :: LinkParam -> String
    show :: LinkParam -> String
    $cshowList :: [LinkParam] -> ShowS
    showList :: [LinkParam] -> ShowS
    Show)

    -- | A single link containing some representation of a URL.
    data Link uri = Link uri [(LinkParam, Text)]
        deriving (Link uri -> Link uri -> Bool
    (Link uri -> Link uri -> Bool)
    -> (Link uri -> Link uri -> Bool) -> Eq (Link uri)
    forall uri. Eq uri => Link uri -> Link uri -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: forall uri. Eq uri => Link uri -> Link uri -> Bool
    == :: Link uri -> Link uri -> Bool
    $c/= :: forall uri. Eq uri => Link uri -> Link uri -> Bool
    /= :: Link uri -> Link uri -> Bool
    Eq, Int -> Link uri -> ShowS
    [Link uri] -> ShowS
    Link uri -> String
    (Int -> Link uri -> ShowS)
    -> (Link uri -> String) -> ([Link uri] -> ShowS) -> Show (Link uri)
    forall uri. Show uri => Int -> Link uri -> ShowS
    forall uri. Show uri => [Link uri] -> ShowS
    forall uri. Show uri => Link uri -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall uri. Show uri => Int -> Link uri -> ShowS
    showsPrec :: Int -> Link uri -> ShowS
    $cshow :: forall uri. Show uri => Link uri -> String
    show :: Link uri -> String
    $cshowList :: forall uri. Show uri => [Link uri] -> ShowS
    showList :: [Link uri] -> ShowS
    Show)

    -- | Types that can represent URLs.
    --
    -- For example, to parse links containing @Text.URI.URI@ from the
    -- [modern-uri](https://hackage.haskell.org/package/modern-uri-0.3.2.0/docs/Text-URI.html#t:URI)
    -- package, simply define the orphan instance:
    --
    -- @
    --    instance IsURI Modern.URI where
    --        uriFromText = left displayException . mkURI
    --        uriToText = render
    -- @
    --
    -- @since 1.1.0
    class IsURI uri where
        uriFromText ∷ Text → Either String uri
        uriToText ∷ uri → Text

    instance IsURI URI where
        uriFromText :: Text -> Either String URI
    uriFromText = Either String URI
    -> (URI -> Either String URI) -> Maybe URI -> Either String URI
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe (String -> Either String URI
    forall a b. a -> Either a b
    Left String
    "") URI -> Either String URI
    forall a b. b -> Either a b
    Right (Maybe URI -> Either String URI)
    -> (Text -> Maybe URI) -> Text -> Either String URI
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Maybe URI
    parseURIReference (String -> Maybe URI) -> (Text -> String) -> Text -> Maybe URI
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> String
    unpack
        uriToText :: URI -> Text
    uriToText = String -> Text
    pack (String -> Text) -> (URI -> String) -> URI -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . URI -> String
    forall a. Show a => a -> String
    show

    instance IsURI Text where
        uriFromText :: Text -> Either String Text
    uriFromText = Text -> Either String Text
    forall a b. b -> Either a b
    Right
        uriToText :: Text -> Text
    uriToText = Text -> Text
    forall a. a -> a
    id

    -- | Extracts the URI from the link.
    href ∷ (IsURI uri) ⇒ Link uri → uri
    href :: forall uri. IsURI uri => Link uri -> uri
    href (Link uri
    h [(LinkParam, Text)]
    _) = uri
    h

    -- | Extracts the parameters from the link.
    linkParams ∷ Link uri → [(LinkParam, Text)]
    linkParams :: forall uri. Link uri -> [(LinkParam, Text)]
    linkParams (Link uri
    _ [(LinkParam, Text)]
    ps) = [(LinkParam, Text)]
    ps