{-# 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 PackageId = PackageIdentifier
data PackageIdentifier
= PackageIdentifier {
PackageIdentifier -> PackageName
pkgName :: PackageName,
PackageIdentifier -> Version
pkgVersion :: Version
}
deriving (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]
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
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
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
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
Ord, Typeable, Typeable PackageIdentifier
PackageIdentifier -> DataType
PackageIdentifier -> Constr
(forall b. Data b => b -> b)
-> PackageIdentifier -> 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)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
Monad m =>
(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 :: forall u.
Int -> (forall d. Data d => d -> u) -> PackageIdentifier -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> PackageIdentifier -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> PackageIdentifier -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> PackageIdentifier -> [u]
gmapQr :: forall r r'.
(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 :: forall r r'.
(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 (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(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 (t :: * -> *) (c :: * -> *).
Typeable t =>
(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 (c :: * -> *).
(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 (c :: * -> *).
(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
Data)
instance Binary PackageIdentifier
instance Structured PackageIdentifier
instance Pretty PackageIdentifier where
pretty :: PackageIdentifier -> Doc
pretty (PackageIdentifier PackageName
n Version
v)
| Version
v forall a. Eq a => a -> a -> Bool
== Version
nullVersion = forall a. Pretty a => a -> Doc
pretty PackageName
n
| Bool
otherwise = forall a. Pretty a => a -> Doc
pretty PackageName
n Doc -> Doc -> Doc
<<>> Char -> Doc
Disp.char Char
'-' Doc -> Doc -> Doc
<<>> forall a. Pretty a => a -> Doc
pretty Version
v
instance Parsec PackageIdentifier where
parsec :: forall (m :: * -> *). CabalParsing m => m PackageIdentifier
parsec = do
NonEmpty String
xs' <- forall (m :: * -> *) a sep.
Alternative m =>
m a -> m sep -> m (NonEmpty a)
P.sepByNonEmpty m String
component (forall (m :: * -> *). CharParsing m => Char -> m Char
P.char Char
'-')
(Version
v, [String]
xs) <- case forall a. Parsec a => String -> Maybe a
simpleParsec (forall a. NonEmpty a -> a
NE.last NonEmpty String
xs') of
Maybe Version
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return (Version
nullVersion, forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty String
xs')
Just Version
v -> forall (m :: * -> *) a. Monad m => a -> m a
return (Version
v, forall a. NonEmpty a -> [a]
NE.init NonEmpty String
xs')
if Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
xs) Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\String
c -> forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall a. Eq a => a -> a -> Bool
/= Char
'.') String
c Bool -> Bool -> Bool
&& Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isDigit String
c)) [String]
xs
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ PackageName -> Version -> PackageIdentifier
PackageIdentifier (String -> PackageName
mkPackageName (forall a. [a] -> [[a]] -> [a]
intercalate String
"-" [String]
xs)) Version
v
else 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 = forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m String
P.munch1 (\Char
c -> Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'.')
instance NFData PackageIdentifier where
rnf :: PackageIdentifier -> ()
rnf (PackageIdentifier PackageName
name Version
version) = forall a. NFData a => a -> ()
rnf PackageName
name seq :: forall a b. a -> b -> b
`seq` forall a. NFData a => a -> ()
rnf Version
version