Support text based placeholders
All checks were successful
Build / audit (push) Successful in 9s
Build / test (push) Successful in 14m17s

This commit is contained in:
2024-09-01 17:34:24 +02:00
parent 14cc805dcf
commit 4f74c2ec10
3 changed files with 21 additions and 80 deletions

View File

@ -9,12 +9,10 @@ module SlackBuilder.Package
, DownloadTemplate(..)
, PackageDescription(..)
, PackageUpdateData(..)
, Maintainer(..)
, Updater(..)
, renderDownloadWithVersion
) where
import Data.List.NonEmpty (NonEmpty(..))
import Data.Text (Text)
import qualified Data.Text as Text
import Text.URI (URI(..))
@ -43,12 +41,6 @@ data Download = Download
, md5sum :: Digest MD5
} deriving (Eq, Show)
-- | Package maintainer information.
data Maintainer = Maintainer
{ name :: Text
, email :: Text
} deriving (Eq, Show)
-- | Appears in the download URI template and specifies which part of the URI
-- should be replaced with the package version.
data DownloadPlaceholder
@ -62,20 +54,18 @@ instance Show DownloadPlaceholder
show VersionPlaceholder = "{version}"
-- | List of URI components, including version placeholders.
newtype DownloadTemplate = DownloadTemplate (NonEmpty DownloadPlaceholder)
deriving Eq
newtype DownloadTemplate = DownloadTemplate
{ unDownloadTemplate :: Text
} deriving Eq
instance Show DownloadTemplate
where
show (DownloadTemplate components) = concatMap show components
show = Text.unpack . unDownloadTemplate
-- | Replaces placeholders in the URL template with the given version.
renderDownloadWithVersion :: MonadThrow m => DownloadTemplate -> Text -> m URI
renderDownloadWithVersion (DownloadTemplate components) version =
URI.mkURI $ foldr f "" components
where
f (StaticPlaceholder staticPlaceholder) = (staticPlaceholder <>)
f VersionPlaceholder = (version <>)
renderDownloadWithVersion (DownloadTemplate template) version =
URI.mkURI $ Text.replace "{version}" version template
-- | Function used to get the latest version of a source.
data Updater = Updater