{-# LANGUAGE DeriveDataTypeable         #-}
{-# LANGUAGE DeriveGeneric              #-}
module Distribution.Types.PackageId
  ( PackageIdentifier(..)
  , PackageId
  ) where

import Distribution.Compat.Prelude
import Prelude ()

import Distribution.Parsec      (Parsec (..), simpleParsec)
import Distribution.Pretty
import Distribution.Types.PackageName
import Distribution.Version           (Version, nullVersion)

import qualified Data.List.NonEmpty              as NE
import qualified Distribution.Compat.CharParsing as P
import qualified Text.PrettyPrint                as Disp

-- | Type alias so we can use the shorter name PackageId.
type PackageId = PackageIdentifier

-- | The name and version of a package.
data PackageIdentifier
    = PackageIdentifier {
        PackageIdentifier -> PackageName
pkgName    :: PackageName, -- ^The name of this package, eg. foo
        PackageIdentifier -> Version
pkgVersion :: Version -- ^the version of this package, eg 1.2
     }
     deriving ((forall x. PackageIdentifier -> Rep PackageIdentifier x)
-> (forall x. Rep PackageIdentifier x -> PackageIdentifier)
-> Generic PackageIdentifier
forall x. Rep PackageIdentifier x -> PackageIdentifier
forall x. PackageIdentifier -> Rep PackageIdentifier x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PackageIdentifier x -> PackageIdentifier
$cfrom :: forall x. PackageIdentifier -> Rep PackageIdentifier x
Generic, ReadPrec [PackageIdentifier]
ReadPrec PackageIdentifier
Int -> ReadS PackageIdentifier
ReadS [PackageIdentifier]
(Int -> ReadS PackageIdentifier)
-> ReadS [PackageIdentifier]
-> ReadPrec PackageIdentifier
-> ReadPrec [PackageIdentifier]
-> Read PackageIdentifier
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PackageIdentifier]
$creadListPrec :: ReadPrec [PackageIdentifier]
readPrec :: ReadPrec PackageIdentifier
$creadPrec :: ReadPrec PackageIdentifier
readList :: ReadS [PackageIdentifier]
$creadList :: ReadS [PackageIdentifier]
readsPrec :: Int -> ReadS PackageIdentifier
$creadsPrec :: Int -> ReadS PackageIdentifier
Read, Int -> PackageIdentifier -> ShowS
[PackageIdentifier] -> ShowS
PackageIdentifier -> String
(Int -> PackageIdentifier -> ShowS)
-> (PackageIdentifier -> String)
-> ([PackageIdentifier] -> ShowS)
-> Show PackageIdentifier
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PackageIdentifier] -> ShowS
$cshowList :: [PackageIdentifier] -> ShowS
show :: PackageIdentifier -> String
$cshow :: PackageIdentifier -> String
showsPrec :: Int -> PackageIdentifier -> ShowS
$cshowsPrec :: Int -> PackageIdentifier -> ShowS
Show, PackageIdentifier -> PackageIdentifier -> Bool
(PackageIdentifier -> PackageIdentifier -> Bool)
-> (PackageIdentifier -> PackageIdentifier -> Bool)
-> Eq PackageIdentifier
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PackageIdentifier -> PackageIdentifier -> Bool
$c/= :: PackageIdentifier -> PackageIdentifier -> Bool
== :: PackageIdentifier -> PackageIdentifier -> Bool
$c== :: PackageIdentifier -> PackageIdentifier -> Bool
Eq, Eq PackageIdentifier
Eq PackageIdentifier
-> (PackageIdentifier -> PackageIdentifier -> Ordering)
-> (PackageIdentifier -> PackageIdentifier -> Bool)
-> (PackageIdentifier -> PackageIdentifier -> Bool)
-> (PackageIdentifier -> PackageIdentifier -> Bool)
-> (PackageIdentifier -> PackageIdentifier -> Bool)
-> (PackageIdentifier -> PackageIdentifier -> PackageIdentifier)
-> (PackageIdentifier -> PackageIdentifier -> PackageIdentifier)
-> Ord PackageIdentifier
PackageIdentifier -> PackageIdentifier -> Bool
PackageIdentifier -> PackageIdentifier -> Ordering
PackageIdentifier -> PackageIdentifier -> PackageIdentifier
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PackageIdentifier -> PackageIdentifier -> PackageIdentifier
$cmin :: PackageIdentifier -> PackageIdentifier -> PackageIdentifier
max :: PackageIdentifier -> PackageIdentifier -> PackageIdentifier
$cmax :: PackageIdentifier -> PackageIdentifier -> PackageIdentifier
>= :: PackageIdentifier -> PackageIdentifier -> Bool
$c>= :: PackageIdentifier -> PackageIdentifier -> Bool
> :: PackageIdentifier -> PackageIdentifier -> Bool
$c> :: PackageIdentifier -> PackageIdentifier -> Bool
<= :: PackageIdentifier -> PackageIdentifier -> Bool
$c<= :: PackageIdentifier -> PackageIdentifier -> Bool
< :: PackageIdentifier -> PackageIdentifier -> Bool
$c< :: PackageIdentifier -> PackageIdentifier -> Bool
compare :: PackageIdentifier -> PackageIdentifier -> Ordering
$ccompare :: PackageIdentifier -> PackageIdentifier -> Ordering
$cp1Ord :: Eq PackageIdentifier
Ord, Typeable, Typeable PackageIdentifier
DataType
Constr
Typeable PackageIdentifier
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> PackageIdentifier
    -> c PackageIdentifier)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c PackageIdentifier)
-> (PackageIdentifier -> Constr)
-> (PackageIdentifier -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c PackageIdentifier))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c PackageIdentifier))
-> ((forall b. Data b => b -> b)
    -> PackageIdentifier -> PackageIdentifier)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> PackageIdentifier -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> PackageIdentifier -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> PackageIdentifier -> m PackageIdentifier)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> PackageIdentifier -> m PackageIdentifier)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> PackageIdentifier -> m PackageIdentifier)
-> Data PackageIdentifier
PackageIdentifier -> DataType
PackageIdentifier -> Constr
(forall b. Data b => b -> b)
-> PackageIdentifier -> PackageIdentifier
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PackageIdentifier -> c PackageIdentifier
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PackageIdentifier
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) -> PackageIdentifier -> u
forall u. (forall d. Data d => d -> u) -> PackageIdentifier -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> PackageIdentifier -> m PackageIdentifier
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> PackageIdentifier -> m PackageIdentifier
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PackageIdentifier
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PackageIdentifier -> c PackageIdentifier
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PackageIdentifier)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c PackageIdentifier)
$cPackageIdentifier :: Constr
$tPackageIdentifier :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> PackageIdentifier -> m PackageIdentifier
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> PackageIdentifier -> m PackageIdentifier
gmapMp :: (forall d. Data d => d -> m d)
-> PackageIdentifier -> m PackageIdentifier
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> PackageIdentifier -> m PackageIdentifier
gmapM :: (forall d. Data d => d -> m d)
-> PackageIdentifier -> m PackageIdentifier
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> PackageIdentifier -> m PackageIdentifier
gmapQi :: Int -> (forall d. Data d => d -> u) -> PackageIdentifier -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> PackageIdentifier -> u
gmapQ :: (forall d. Data d => d -> u) -> PackageIdentifier -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> PackageIdentifier -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r
gmapT :: (forall b. Data b => b -> b)
-> PackageIdentifier -> PackageIdentifier
$cgmapT :: (forall b. Data b => b -> b)
-> PackageIdentifier -> PackageIdentifier
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c PackageIdentifier)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c PackageIdentifier)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c PackageIdentifier)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c PackageIdentifier)
dataTypeOf :: PackageIdentifier -> DataType
$cdataTypeOf :: PackageIdentifier -> DataType
toConstr :: PackageIdentifier -> Constr
$ctoConstr :: PackageIdentifier -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PackageIdentifier
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c PackageIdentifier
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PackageIdentifier -> c PackageIdentifier
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> PackageIdentifier -> c PackageIdentifier
$cp1Data :: Typeable PackageIdentifier
Data)

instance Binary PackageIdentifier
instance Structured PackageIdentifier

instance Pretty PackageIdentifier where
  pretty :: PackageIdentifier -> Doc
pretty (PackageIdentifier PackageName
n Version
v)
    | Version
v Version -> Version -> Bool
forall a. Eq a => a -> a -> Bool
== Version
nullVersion = PackageName -> Doc
forall a. Pretty a => a -> Doc
pretty PackageName
n -- if no version, don't show version.
    | Bool
otherwise        = PackageName -> Doc
forall a. Pretty a => a -> Doc
pretty PackageName
n Doc -> Doc -> Doc
<<>> Char -> Doc
Disp.char Char
'-' Doc -> Doc -> Doc
<<>> Version -> Doc
forall a. Pretty a => a -> Doc
pretty Version
v

-- |
--
-- >>> simpleParsec "foo-bar-0" :: Maybe PackageIdentifier
-- Just (PackageIdentifier {pkgName = PackageName "foo-bar", pkgVersion = mkVersion [0]})
--
-- >>> simpleParsec "foo-bar" :: Maybe PackageIdentifier
-- Just (PackageIdentifier {pkgName = PackageName "foo-bar", pkgVersion = mkVersion []})
--
-- /Note:/ Stricter than 'Text' instance
--
-- >>> simpleParsec "foo-bar-0-0" :: Maybe PackageIdentifier
-- Nothing
--
-- >>> simpleParsec "foo-bar.0" :: Maybe PackageIdentifier
-- Nothing
--
-- >>> simpleParsec "foo-bar.4-2" :: Maybe PackageIdentifier
-- Nothing
--
-- >>> simpleParsec "1.2.3" :: Maybe PackageIdentifier
-- Nothing
--
instance Parsec PackageIdentifier where
  parsec :: m PackageIdentifier
parsec = do
      NonEmpty String
xs' <- m String -> m Char -> m (NonEmpty String)
forall (m :: * -> *) a sep.
Alternative m =>
m a -> m sep -> m (NonEmpty a)
P.sepByNonEmpty m String
component (Char -> m Char
forall (m :: * -> *). CharParsing m => Char -> m Char
P.char Char
'-')
      (Version
v, [String]
xs) <- case String -> Maybe Version
forall a. Parsec a => String -> Maybe a
simpleParsec (NonEmpty String -> String
forall a. NonEmpty a -> a
NE.last NonEmpty String
xs') of
          Maybe Version
Nothing -> (Version, [String]) -> m (Version, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (Version
nullVersion, NonEmpty String -> [String]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty String
xs') -- all components are version
          Just Version
v  -> (Version, [String]) -> m (Version, [String])
forall (m :: * -> *) a. Monad m => a -> m a
return (Version
v, NonEmpty String -> [String]
forall a. NonEmpty a -> [a]
NE.init NonEmpty String
xs')
      if Bool -> Bool
not ([String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
xs) Bool -> Bool -> Bool
&& (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\String
c ->  (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'.') String
c Bool -> Bool -> Bool
&& Bool -> Bool
not ((Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit String
c)) [String]
xs
      then PackageIdentifier -> m PackageIdentifier
forall (m :: * -> *) a. Monad m => a -> m a
return (PackageIdentifier -> m PackageIdentifier)
-> PackageIdentifier -> m PackageIdentifier
forall a b. (a -> b) -> a -> b
$ PackageName -> Version -> PackageIdentifier
PackageIdentifier (String -> PackageName
mkPackageName (String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate  String
"-" [String]
xs)) Version
v
      else String -> m PackageIdentifier
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"all digits or a dot in a portion of package name"
    where
      component :: m String
component = (Char -> Bool) -> m String
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m String
P.munch1 (\Char
c ->  Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.')

instance NFData PackageIdentifier where
    rnf :: PackageIdentifier -> ()
rnf (PackageIdentifier PackageName
name Version
version) = PackageName -> ()
forall a. NFData a => a -> ()
rnf PackageName
name () -> () -> ()
`seq` Version -> ()
forall a. NFData a => a -> ()
rnf Version
version