haskell / haskell/cabal

Function to convert from `Cabal` version to `cabal-version`

Open
#9,067 1 comment 1 reaction 0 assignees View on GitHub
re: API
Dominant language
Haskell
Stars
1.7k
Forks
750
Avg merge
4d 3h
Merged PRs (30d)
28

Description

Does the API have a function that gives the `cabal-version` for a certain version of `Cabal`?
E.g. `Cabal-3.10.1.0` would give `cabal-version: 3.8`.

I wrote the following function to compute it, but maybe it would be nice to have it in the API:
```haskell
-- | Robustly convert a Cabal version number into a 'CabalSpecVersion'.
--
-- Picks the greatest spec version less or equal than the given Cabal version.
-- If none such exists, returns the minimal 'CabalSpecVersion'.
--
cabalVersionToSpecVersion :: Version -> CabalSpecVersion
cabalVersionToSpecVersion version
= headWithDefault minBound
. map fst
. dropWhile ((versionNumbers version <) . snd)
. map (\ v -> (v, cabalSpecToVersionDigits v))
$ specVersionsDesc
where
specVersionsDesc :: [CabalSpecVersion]
specVersionsDesc = [maxBound, pred maxBound .. minBound]

headWithDefault :: a -> [a] -> a
headWithDefault d = \case
[] -> d
h:_ -> h
```
This simple implementation is worst case O(number of spec versions), but I guess with tabulating the first two digits of the `Cabal` version there could be a quasilinear one.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.