{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric      #-}
module Distribution.Types.MungedPackageId
  ( MungedPackageId(..)
  , computeCompatPackageId
  ) where

import Distribution.Compat.Prelude
import Prelude ()

import Distribution.Parsec
import Distribution.Pretty
import Distribution.Types.LibraryName
import Distribution.Types.MungedPackageName
import Distribution.Types.PackageId
import Distribution.Version                 (Version, nullVersion)

import qualified Text.PrettyPrint as Disp

-- | A simple pair of a 'MungedPackageName' and 'Version'. 'MungedPackageName' is to
-- 'MungedPackageId' as 'PackageName' is to 'PackageId'. See 'MungedPackageName' for more
-- info.
data MungedPackageId
    = MungedPackageId {
        -- | The combined package and component name. see documentation for
        -- 'MungedPackageName'.
        MungedPackageId -> MungedPackageName
mungedName    :: MungedPackageName,
        -- | The version of this package / component, eg 1.2
        MungedPackageId -> Version
mungedVersion :: Version
     }
     deriving ((forall x. MungedPackageId -> Rep MungedPackageId x)
-> (forall x. Rep MungedPackageId x -> MungedPackageId)
-> Generic MungedPackageId
forall x. Rep MungedPackageId x -> MungedPackageId
forall x. MungedPackageId -> Rep MungedPackageId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MungedPackageId -> Rep MungedPackageId x
from :: forall x. MungedPackageId -> Rep MungedPackageId x
$cto :: forall x. Rep MungedPackageId x -> MungedPackageId
to :: forall x. Rep MungedPackageId x -> MungedPackageId
Generic, ReadPrec [MungedPackageId]
ReadPrec MungedPackageId
Int -> ReadS MungedPackageId
ReadS [MungedPackageId]
(Int -> ReadS MungedPackageId)
-> ReadS [MungedPackageId]
-> ReadPrec MungedPackageId
-> ReadPrec [MungedPackageId]
-> Read MungedPackageId
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS MungedPackageId
readsPrec :: Int -> ReadS MungedPackageId
$creadList :: ReadS [MungedPackageId]
readList :: ReadS [MungedPackageId]
$creadPrec :: ReadPrec MungedPackageId
readPrec :: ReadPrec MungedPackageId
$creadListPrec :: ReadPrec [MungedPackageId]
readListPrec :: ReadPrec [MungedPackageId]
Read, Int -> MungedPackageId -> ShowS
[MungedPackageId] -> ShowS
MungedPackageId -> String
(Int -> MungedPackageId -> ShowS)
-> (MungedPackageId -> String)
-> ([MungedPackageId] -> ShowS)
-> Show MungedPackageId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MungedPackageId -> ShowS
showsPrec :: Int -> MungedPackageId -> ShowS
$cshow :: MungedPackageId -> String
show :: MungedPackageId -> String
$cshowList :: [MungedPackageId] -> ShowS
showList :: [MungedPackageId] -> ShowS
Show, MungedPackageId -> MungedPackageId -> Bool
(MungedPackageId -> MungedPackageId -> Bool)
-> (MungedPackageId -> MungedPackageId -> Bool)
-> Eq MungedPackageId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MungedPackageId -> MungedPackageId -> Bool
== :: MungedPackageId -> MungedPackageId -> Bool
$c/= :: MungedPackageId -> MungedPackageId -> Bool
/= :: MungedPackageId -> MungedPackageId -> Bool
Eq, Eq MungedPackageId
Eq MungedPackageId =>
(MungedPackageId -> MungedPackageId -> Ordering)
-> (MungedPackageId -> MungedPackageId -> Bool)
-> (MungedPackageId -> MungedPackageId -> Bool)
-> (MungedPackageId -> MungedPackageId -> Bool)
-> (MungedPackageId -> MungedPackageId -> Bool)
-> (MungedPackageId -> MungedPackageId -> MungedPackageId)
-> (MungedPackageId -> MungedPackageId -> MungedPackageId)
-> Ord MungedPackageId
MungedPackageId -> MungedPackageId -> Bool
MungedPackageId -> MungedPackageId -> Ordering
MungedPackageId -> MungedPackageId -> MungedPackageId
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
$ccompare :: MungedPackageId -> MungedPackageId -> Ordering
compare :: MungedPackageId -> MungedPackageId -> Ordering
$c< :: MungedPackageId -> MungedPackageId -> Bool
< :: MungedPackageId -> MungedPackageId -> Bool
$c<= :: MungedPackageId -> MungedPackageId -> Bool
<= :: MungedPackageId -> MungedPackageId -> Bool
$c> :: MungedPackageId -> MungedPackageId -> Bool
> :: MungedPackageId -> MungedPackageId -> Bool
$c>= :: MungedPackageId -> MungedPackageId -> Bool
>= :: MungedPackageId -> MungedPackageId -> Bool
$cmax :: MungedPackageId -> MungedPackageId -> MungedPackageId
max :: MungedPackageId -> MungedPackageId -> MungedPackageId
$cmin :: MungedPackageId -> MungedPackageId -> MungedPackageId
min :: MungedPackageId -> MungedPackageId -> MungedPackageId
Ord, Typeable, Typeable MungedPackageId
Typeable MungedPackageId =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> MungedPackageId -> c MungedPackageId)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c MungedPackageId)
-> (MungedPackageId -> Constr)
-> (MungedPackageId -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c MungedPackageId))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c MungedPackageId))
-> ((forall b. Data b => b -> b)
    -> MungedPackageId -> MungedPackageId)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> MungedPackageId -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> MungedPackageId -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> MungedPackageId -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> MungedPackageId -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> MungedPackageId -> m MungedPackageId)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> MungedPackageId -> m MungedPackageId)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> MungedPackageId -> m MungedPackageId)
-> Data MungedPackageId
MungedPackageId -> Constr
MungedPackageId -> DataType
(forall b. Data b => b -> b) -> MungedPackageId -> MungedPackageId
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) -> MungedPackageId -> u
forall u. (forall d. Data d => d -> u) -> MungedPackageId -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageId -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageId -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> MungedPackageId -> m MungedPackageId
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageId -> m MungedPackageId
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MungedPackageId
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MungedPackageId -> c MungedPackageId
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MungedPackageId)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MungedPackageId)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MungedPackageId -> c MungedPackageId
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MungedPackageId -> c MungedPackageId
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MungedPackageId
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MungedPackageId
$ctoConstr :: MungedPackageId -> Constr
toConstr :: MungedPackageId -> Constr
$cdataTypeOf :: MungedPackageId -> DataType
dataTypeOf :: MungedPackageId -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MungedPackageId)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MungedPackageId)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MungedPackageId)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MungedPackageId)
$cgmapT :: (forall b. Data b => b -> b) -> MungedPackageId -> MungedPackageId
gmapT :: (forall b. Data b => b -> b) -> MungedPackageId -> MungedPackageId
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageId -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageId -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageId -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MungedPackageId -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> MungedPackageId -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> MungedPackageId -> [u]
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> MungedPackageId -> u
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> MungedPackageId -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> MungedPackageId -> m MungedPackageId
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> MungedPackageId -> m MungedPackageId
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageId -> m MungedPackageId
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageId -> m MungedPackageId
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageId -> m MungedPackageId
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> MungedPackageId -> m MungedPackageId
Data)

instance Binary MungedPackageId
instance Structured MungedPackageId

-- |
--
-- >>> prettyShow $ MungedPackageId (MungedPackageName "servant" LMainLibName) (mkVersion [1,2,3])
-- "servant-1.2.3"
--
-- >>> prettyShow $ MungedPackageId (MungedPackageName "servant" (LSubLibName "lackey")) (mkVersion [0,1,2])
-- "z-servant-z-lackey-0.1.2"
--
instance Pretty MungedPackageId where
    pretty :: MungedPackageId -> Doc
pretty (MungedPackageId MungedPackageName
n Version
v)
        | Version
v Version -> Version -> Bool
forall a. Eq a => a -> a -> Bool
== Version
nullVersion = MungedPackageName -> Doc
forall a. Pretty a => a -> Doc
pretty MungedPackageName
n -- if no version, don't show version.
        | Bool
otherwise        = MungedPackageName -> Doc
forall a. Pretty a => a -> Doc
pretty MungedPackageName
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 MungedPackageId
-- Just (MungedPackageId {mungedName = MungedPackageName (PackageName "foo-bar") LMainLibName, mungedVersion = mkVersion [0]})
--
-- >>> simpleParsec "foo-bar" :: Maybe MungedPackageId
-- Just (MungedPackageId {mungedName = MungedPackageName (PackageName "foo-bar") LMainLibName, mungedVersion = mkVersion []})
--
-- >>> simpleParsec "z-foo-bar-z-baz-0" :: Maybe MungedPackageId
-- Just (MungedPackageId {mungedName = MungedPackageName (PackageName "foo-bar") (LSubLibName (UnqualComponentName "baz")), mungedVersion = mkVersion [0]})
--
-- >>> simpleParsec "foo-bar-0-0" :: Maybe MungedPackageId
-- Nothing
--
-- >>> simpleParsec "foo-bar.0" :: Maybe MungedPackageId
-- Nothing
--
-- >>> simpleParsec "foo-bar.4-2" :: Maybe MungedPackageId
-- Nothing
--
instance Parsec MungedPackageId where
    parsec :: forall (m :: * -> *). CabalParsing m => m MungedPackageId
parsec = do
        PackageIdentifier PackageName
pn Version
v <- m PackageIdentifier
forall a (m :: * -> *). (Parsec a, CabalParsing m) => m a
forall (m :: * -> *). CabalParsing m => m PackageIdentifier
parsec
        MungedPackageId -> m MungedPackageId
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (MungedPackageId -> m MungedPackageId)
-> MungedPackageId -> m MungedPackageId
forall a b. (a -> b) -> a -> b
$ MungedPackageName -> Version -> MungedPackageId
MungedPackageId (PackageName -> MungedPackageName
decodeCompatPackageName PackageName
pn) Version
v

instance NFData MungedPackageId where
    rnf :: MungedPackageId -> ()
rnf (MungedPackageId MungedPackageName
name Version
version) = MungedPackageName -> ()
forall a. NFData a => a -> ()
rnf MungedPackageName
name () -> () -> ()
forall a b. a -> b -> b
`seq` Version -> ()
forall a. NFData a => a -> ()
rnf Version
version

computeCompatPackageId :: PackageId -> LibraryName -> MungedPackageId
computeCompatPackageId :: PackageIdentifier -> LibraryName -> MungedPackageId
computeCompatPackageId (PackageIdentifier PackageName
pn Version
vr) LibraryName
ln =
    MungedPackageName -> Version -> MungedPackageId
MungedPackageId (PackageName -> LibraryName -> MungedPackageName
MungedPackageName PackageName
pn LibraryName
ln) Version
vr

-- $setup
-- >>> :seti -XOverloadedStrings
-- >>> import Distribution.Types.Version