packages feed

cabal-flatpak-1.0.0: src/domain/Distribution/Flatpak/Location.hs

module Distribution.Flatpak.Location
  ( Location (..)
  , parseLocation
  ) where

import Data.Maybe
import Data.Text (Text)
import Data.Text qualified as T
import Network.URI

data Location = Location
  { host :: Text
  , segments :: [Text]
  }

parseLocation :: Text -> Maybe Location
parseLocation url = do
  uri <- parseURI (T.unpack url)
  auth <- uriAuthority uri
  let lowerHost = T.toLower (T.pack (uriRegName auth))
      host = fromMaybe lowerHost (T.stripPrefix "www." lowerHost)
      segments = dropGit (filter (not . T.null) (map T.pack (pathSegments uri)))
  if T.null host then Nothing else Just Location {host, segments}
  where
    dropGit segs = case reverse segs of
      [] -> []
      lastSegment : rest -> reverse (dropSuffix ".git" lastSegment : rest)
    dropSuffix suffix s = fromMaybe s (T.stripSuffix suffix s)