module Distribution.Types.VersionRange (
VersionRange,
anyVersion, noVersion,
thisVersion, notThisVersion,
laterVersion, earlierVersion,
orLaterVersion, orEarlierVersion,
unionVersionRanges, intersectVersionRanges,
withinVersion,
majorBoundVersion,
withinRange,
foldVersionRange,
normaliseVersionRange,
stripParensVersionRange,
hasUpperBound,
hasLowerBound,
VersionRangeF (..),
cataVersionRange,
anaVersionRange,
hyloVersionRange,
projectVersionRange,
embedVersionRange,
wildcardUpperBound,
majorUpperBound,
isWildcardRange,
versionRangeParser,
) where
import Distribution.Compat.Prelude
import Distribution.Types.Version
import Distribution.Types.VersionRange.Internal
import Prelude ()
foldVersionRange :: a
-> (Version -> a)
-> (Version -> a)
-> (Version -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> VersionRange -> a
foldVersionRange anyv this later earlier union intersect = fold
where
fold = cataVersionRange alg
alg AnyVersionF = anyv
alg (ThisVersionF v) = this v
alg (LaterVersionF v) = later v
alg (OrLaterVersionF v) = union (this v) (later v)
alg (EarlierVersionF v) = earlier v
alg (OrEarlierVersionF v) = union (this v) (earlier v)
alg (WildcardVersionF v) = fold (wildcard v)
alg (MajorBoundVersionF v) = fold (majorBound v)
alg (UnionVersionRangesF v1 v2) = union v1 v2
alg (IntersectVersionRangesF v1 v2) = intersect v1 v2
alg (VersionRangeParensF v) = v
wildcard v = intersectVersionRanges
(orLaterVersion v)
(earlierVersion (wildcardUpperBound v))
majorBound v = intersectVersionRanges
(orLaterVersion v)
(earlierVersion (majorUpperBound v))
normaliseVersionRange :: VersionRange -> VersionRange
normaliseVersionRange = hyloVersionRange embed projectVersionRange
where
embed (UnionVersionRangesF (ThisVersion v) (LaterVersion v')) | v == v' =
orLaterVersion v
embed (UnionVersionRangesF (LaterVersion v) (ThisVersion v')) | v == v' =
orLaterVersion v
embed (UnionVersionRangesF (ThisVersion v) (EarlierVersion v')) | v == v' =
orEarlierVersion v
embed (UnionVersionRangesF (EarlierVersion v) (ThisVersion v')) | v == v' =
orEarlierVersion v
embed vr = embedVersionRange vr
stripParensVersionRange :: VersionRange -> VersionRange
stripParensVersionRange = hyloVersionRange embed projectVersionRange
where
embed (VersionRangeParensF vr) = vr
embed vr = embedVersionRange vr
withinRange :: Version -> VersionRange -> Bool
withinRange v = foldVersionRange
True
(\v' -> v == v')
(\v' -> v > v')
(\v' -> v < v')
(||)
(&&)
wildcardUpperBound :: Version -> Version
wildcardUpperBound = alterVersion $
\lowerBound -> init lowerBound ++ [last lowerBound + 1]
isWildcardRange :: Version -> Version -> Bool
isWildcardRange ver1 ver2 = check (versionNumbers ver1) (versionNumbers ver2)
where check (n:[]) (m:[]) | n+1 == m = True
check (n:ns) (m:ms) | n == m = check ns ms
check _ _ = False
hasUpperBound :: VersionRange -> Bool
hasUpperBound = foldVersionRange
False
(const True)
(const False)
(const True)
(&&) (||)
hasLowerBound :: VersionRange -> Bool
hasLowerBound = foldVersionRange
False
(const True)
(const True)
(const False)
(&&) (||)