module Docs.CLI.Types where

import Data.Text (Text)

import qualified Data.Text as Text

type Url = String

-- | Appears at the end of a url after a pound sign, pointing the element to
-- focus on.
--    https://hackage.haskell.org/package/hoogle-5.0.18.1/docs/Hoogle.html#t:Target
--                                                                         ^^^^^^^^
type Anchor = Text

dropAnchor :: Url -> Url
dropAnchor :: Url -> Url
dropAnchor = forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Eq a => a -> a -> Bool
/= Char
'#')

takeAnchor :: MonadFail m => Url -> m Anchor
takeAnchor :: forall (m :: * -> *). MonadFail m => Url -> m Anchor
takeAnchor Url
url = case forall a. Int -> [a] -> [a]
drop Int
1 forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall a. Eq a => a -> a -> Bool
/= Char
'#') Url
url of
  [] -> forall (m :: * -> *) a. MonadFail m => Url -> m a
fail Url
"no anchor"
  Url
xs -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Url -> Anchor
Text.pack Url
xs

data DeclUrl = DeclUrl ModuleUrl Anchor
  deriving (DeclUrl -> DeclUrl -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DeclUrl -> DeclUrl -> Bool
$c/= :: DeclUrl -> DeclUrl -> Bool
== :: DeclUrl -> DeclUrl -> Bool
$c== :: DeclUrl -> DeclUrl -> Bool
Eq, Int -> DeclUrl -> Url -> Url
[DeclUrl] -> Url -> Url
DeclUrl -> Url
forall a.
(Int -> a -> Url -> Url)
-> (a -> Url) -> ([a] -> Url -> Url) -> Show a
showList :: [DeclUrl] -> Url -> Url
$cshowList :: [DeclUrl] -> Url -> Url
show :: DeclUrl -> Url
$cshow :: DeclUrl -> Url
showsPrec :: Int -> DeclUrl -> Url -> Url
$cshowsPrec :: Int -> DeclUrl -> Url -> Url
Show)

-- | Link to an item in a src page
data SourceLink = SourceLink Url Anchor
  deriving (SourceLink -> SourceLink -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SourceLink -> SourceLink -> Bool
$c/= :: SourceLink -> SourceLink -> Bool
== :: SourceLink -> SourceLink -> Bool
$c== :: SourceLink -> SourceLink -> Bool
Eq, Int -> SourceLink -> Url -> Url
[SourceLink] -> Url -> Url
SourceLink -> Url
forall a.
(Int -> a -> Url -> Url)
-> (a -> Url) -> ([a] -> Url -> Url) -> Show a
showList :: [SourceLink] -> Url -> Url
$cshowList :: [SourceLink] -> Url -> Url
show :: SourceLink -> Url
$cshow :: SourceLink -> Url
showsPrec :: Int -> SourceLink -> Url -> Url
$cshowsPrec :: Int -> SourceLink -> Url -> Url
Show)

newtype ModuleUrl = ModuleUrl Url
  deriving (ModuleUrl -> ModuleUrl -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ModuleUrl -> ModuleUrl -> Bool
$c/= :: ModuleUrl -> ModuleUrl -> Bool
== :: ModuleUrl -> ModuleUrl -> Bool
$c== :: ModuleUrl -> ModuleUrl -> Bool
Eq, Int -> ModuleUrl -> Url -> Url
[ModuleUrl] -> Url -> Url
ModuleUrl -> Url
forall a.
(Int -> a -> Url -> Url)
-> (a -> Url) -> ([a] -> Url -> Url) -> Show a
showList :: [ModuleUrl] -> Url -> Url
$cshowList :: [ModuleUrl] -> Url -> Url
show :: ModuleUrl -> Url
$cshow :: ModuleUrl -> Url
showsPrec :: Int -> ModuleUrl -> Url -> Url
$cshowsPrec :: Int -> ModuleUrl -> Url -> Url
Show)

-- | Url to a Haddock package page
newtype PackageUrl = PackageUrl Url
  deriving (PackageUrl -> PackageUrl -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PackageUrl -> PackageUrl -> Bool
$c/= :: PackageUrl -> PackageUrl -> Bool
== :: PackageUrl -> PackageUrl -> Bool
$c== :: PackageUrl -> PackageUrl -> Bool
Eq, Int -> PackageUrl -> Url -> Url
[PackageUrl] -> Url -> Url
PackageUrl -> Url
forall a.
(Int -> a -> Url -> Url)
-> (a -> Url) -> ([a] -> Url -> Url) -> Show a
showList :: [PackageUrl] -> Url -> Url
$cshowList :: [PackageUrl] -> Url -> Url
show :: PackageUrl -> Url
$cshow :: PackageUrl -> Url
showsPrec :: Int -> PackageUrl -> Url -> Url
$cshowsPrec :: Int -> PackageUrl -> Url -> Url
Show)

type FileName = String

type FileContent = Text

newtype RelativeUrl = RelativeUrl Text

data FileInfo = FileInfo
  { FileInfo -> Url
fName :: FileName
  , FileInfo -> Maybe Int
fLine :: Maybe Int
  , FileInfo -> Anchor
fContent :: FileContent
  }

class HasUrl a where
  getUrl :: a -> Url

instance HasUrl DeclUrl where
  getUrl :: DeclUrl -> Url
getUrl (DeclUrl ModuleUrl
url Anchor
anchor) = forall a. HasUrl a => a -> Url
getUrl ModuleUrl
url forall a. [a] -> [a] -> [a]
++ Url
"#" forall a. [a] -> [a] -> [a]
++ Anchor -> Url
Text.unpack Anchor
anchor
instance HasUrl ModuleUrl where
  getUrl :: ModuleUrl -> Url
getUrl (ModuleUrl Url
url) = Url
url
instance HasUrl SourceLink where
  getUrl :: SourceLink -> Url
getUrl (SourceLink Url
url Anchor
_) = Url
url
instance HasUrl PackageUrl where
  getUrl :: PackageUrl -> Url
getUrl (PackageUrl Url
url) = Url
url
instance HasUrl Url where
  getUrl :: Url -> Url
getUrl Url
url = Url
url