{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric      #-}
module Distribution.Types.PackageVersionConstraint (
    PackageVersionConstraint(..),
    thisPackageVersionConstraint,
    simplifyPackageVersionConstraint,
) where

import Distribution.Compat.Prelude
import Prelude ()

import Distribution.Parsec
import Distribution.Pretty
import Distribution.Types.PackageId
import Distribution.Types.PackageName
import Distribution.Types.Version
import Distribution.Types.VersionRange.Internal
import Distribution.Version                     (simplifyVersionRange)

import qualified Distribution.Compat.CharParsing as P

-- | A version constraint on a package. Different from 'ExeDependency' and
-- 'Dependency' since it does not specify the need for a component, not even
-- the main library.
-- There are a few places in the codebase where 'Dependency' was used where
-- 'PackageVersionConstraint' is not used instead (#5570).
data PackageVersionConstraint = PackageVersionConstraint PackageName VersionRange
                  deriving (forall x.
Rep PackageVersionConstraint x -> PackageVersionConstraint
forall x.
PackageVersionConstraint -> Rep PackageVersionConstraint x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep PackageVersionConstraint x -> PackageVersionConstraint
$cfrom :: forall x.
PackageVersionConstraint -> Rep PackageVersionConstraint x
Generic, ReadPrec [PackageVersionConstraint]
ReadPrec PackageVersionConstraint
Int -> ReadS PackageVersionConstraint
ReadS [PackageVersionConstraint]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PackageVersionConstraint]
$creadListPrec :: ReadPrec [PackageVersionConstraint]
readPrec :: ReadPrec PackageVersionConstraint
$creadPrec :: ReadPrec PackageVersionConstraint
readList :: ReadS [PackageVersionConstraint]
$creadList :: ReadS [PackageVersionConstraint]
readsPrec :: Int -> ReadS PackageVersionConstraint
$creadsPrec :: Int -> ReadS PackageVersionConstraint
Read, Int -> PackageVersionConstraint -> ShowS
[PackageVersionConstraint] -> ShowS
PackageVersionConstraint -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PackageVersionConstraint] -> ShowS
$cshowList :: [PackageVersionConstraint] -> ShowS
show :: PackageVersionConstraint -> String
$cshow :: PackageVersionConstraint -> String
showsPrec :: Int -> PackageVersionConstraint -> ShowS
$cshowsPrec :: Int -> PackageVersionConstraint -> ShowS
Show, PackageVersionConstraint -> PackageVersionConstraint -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PackageVersionConstraint -> PackageVersionConstraint -> Bool
$c/= :: PackageVersionConstraint -> PackageVersionConstraint -> Bool
== :: PackageVersionConstraint -> PackageVersionConstraint -> Bool
$c== :: PackageVersionConstraint -> PackageVersionConstraint -> Bool
Eq, Typeable, Typeable PackageVersionConstraint
PackageVersionConstraint -> DataType
PackageVersionConstraint -> Constr
(forall b. Data b => b -> b)
-> PackageVersionConstraint -> PackageVersionConstraint
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int
-> (forall d. Data d => d -> u) -> PackageVersionConstraint -> u
forall u.
(forall d. Data d => d -> u) -> PackageVersionConstraint -> [u]
forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> PackageVersionConstraint
-> r
forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> PackageVersionConstraint
-> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> PackageVersionConstraint -> m PackageVersionConstraint
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> PackageVersionConstraint -> m PackageVersionConstraint
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PackageVersionConstraint
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> PackageVersionConstraint
-> c PackageVersionConstraint
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PackageVersionConstraint)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c PackageVersionConstraint)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> PackageVersionConstraint -> m PackageVersionConstraint
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> PackageVersionConstraint -> m PackageVersionConstraint
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> PackageVersionConstraint -> m PackageVersionConstraint
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> PackageVersionConstraint -> m PackageVersionConstraint
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> PackageVersionConstraint -> m PackageVersionConstraint
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> PackageVersionConstraint -> m PackageVersionConstraint
gmapQi :: forall u.
Int
-> (forall d. Data d => d -> u) -> PackageVersionConstraint -> u
$cgmapQi :: forall u.
Int
-> (forall d. Data d => d -> u) -> PackageVersionConstraint -> u
gmapQ :: forall u.
(forall d. Data d => d -> u) -> PackageVersionConstraint -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> PackageVersionConstraint -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> PackageVersionConstraint
-> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> PackageVersionConstraint
-> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> PackageVersionConstraint
-> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> PackageVersionConstraint
-> r
gmapT :: (forall b. Data b => b -> b)
-> PackageVersionConstraint -> PackageVersionConstraint
$cgmapT :: (forall b. Data b => b -> b)
-> PackageVersionConstraint -> PackageVersionConstraint
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c PackageVersionConstraint)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c PackageVersionConstraint)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PackageVersionConstraint)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PackageVersionConstraint)
dataTypeOf :: PackageVersionConstraint -> DataType
$cdataTypeOf :: PackageVersionConstraint -> DataType
toConstr :: PackageVersionConstraint -> Constr
$ctoConstr :: PackageVersionConstraint -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PackageVersionConstraint
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PackageVersionConstraint
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> PackageVersionConstraint
-> c PackageVersionConstraint
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> PackageVersionConstraint
-> c PackageVersionConstraint
Data)

instance Binary PackageVersionConstraint
instance Structured PackageVersionConstraint
instance NFData PackageVersionConstraint where rnf :: PackageVersionConstraint -> ()
rnf = forall a. (Generic a, GNFData (Rep a)) => a -> ()
genericRnf

instance Pretty PackageVersionConstraint where
  -- Cannot do: PackageVersionConstraint have to be parseable
  -- as Dependency, due roundtrip problems. (e.g. talking to old ./Setup).
  --
  -- pretty (PackageVersionConstraint name (ThisVersion ver)) =
  --     pretty (PackageIdentifier name ver)
  pretty :: PackageVersionConstraint -> Doc
pretty (PackageVersionConstraint PackageName
name VersionRange
ver) =
      forall a. Pretty a => a -> Doc
pretty PackageName
name Doc -> Doc -> Doc
<+> forall a. Pretty a => a -> Doc
pretty VersionRange
ver

-- |
--
-- >>> simpleParsec "foo" :: Maybe PackageVersionConstraint
-- Just (PackageVersionConstraint (PackageName "foo") (OrLaterVersion (mkVersion [0])))
--
-- >>> simpleParsec "foo >=2.0" :: Maybe PackageVersionConstraint
-- Just (PackageVersionConstraint (PackageName "foo") (OrLaterVersion (mkVersion [2,0])))
--
-- >>> simpleParsec "foo-2.0" :: Maybe PackageVersionConstraint
-- Just (PackageVersionConstraint (PackageName "foo") (ThisVersion (mkVersion [2,0])))
--
instance Parsec PackageVersionConstraint where
  parsec :: forall (m :: * -> *). CabalParsing m => m PackageVersionConstraint
parsec = do
      PackageIdentifier PackageName
name Version
ver <- forall a (m :: * -> *). (Parsec a, CabalParsing m) => m a
parsec
      if Version
ver forall a. Eq a => a -> a -> Bool
== Version
nullVersion
      then do
          forall (m :: * -> *). CharParsing m => m ()
P.spaces
          VersionRange
vr <- forall a (m :: * -> *). (Parsec a, CabalParsing m) => m a
parsec forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *) a. Monad m => a -> m a
return VersionRange
anyVersion
          forall (m :: * -> *). CharParsing m => m ()
P.spaces
          forall (m :: * -> *) a. Monad m => a -> m a
return (PackageName -> VersionRange -> PackageVersionConstraint
PackageVersionConstraint PackageName
name VersionRange
vr)
      else
          forall (f :: * -> *) a. Applicative f => a -> f a
pure (PackageName -> VersionRange -> PackageVersionConstraint
PackageVersionConstraint PackageName
name (Version -> VersionRange
thisVersion Version
ver))

-- | @since 3.4.0.0
thisPackageVersionConstraint :: PackageIdentifier -> PackageVersionConstraint
thisPackageVersionConstraint :: PackageIdentifier -> PackageVersionConstraint
thisPackageVersionConstraint (PackageIdentifier PackageName
pn Version
vr) =
    PackageName -> VersionRange -> PackageVersionConstraint
PackageVersionConstraint PackageName
pn (Version -> VersionRange
thisVersion Version
vr)

-- | @since 3.4.0.0
simplifyPackageVersionConstraint :: PackageVersionConstraint -> PackageVersionConstraint
simplifyPackageVersionConstraint :: PackageVersionConstraint -> PackageVersionConstraint
simplifyPackageVersionConstraint (PackageVersionConstraint PackageName
pn VersionRange
vr) =
    PackageName -> VersionRange -> PackageVersionConstraint
PackageVersionConstraint PackageName
pn (VersionRange -> VersionRange
simplifyVersionRange VersionRange
vr)