    {-# 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
    $cfrom :: forall x. PackageIdentifier -> Rep PackageIdentifier x
    from :: forall x. PackageIdentifier -> Rep PackageIdentifier x
    $cto :: forall x. Rep PackageIdentifier x -> PackageIdentifier
    to :: forall x. Rep PackageIdentifier x -> PackageIdentifier
    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
    $creadsPrec :: Int -> ReadS PackageIdentifier
    readsPrec :: Int -> ReadS PackageIdentifier
    $creadList :: ReadS [PackageIdentifier]
    readList :: ReadS [PackageIdentifier]
    $creadPrec :: ReadPrec PackageIdentifier
    readPrec :: ReadPrec PackageIdentifier
    $creadListPrec :: ReadPrec [PackageIdentifier]
    readListPrec :: ReadPrec [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
    $cshowsPrec :: Int -> PackageIdentifier -> ShowS
    showsPrec :: Int -> PackageIdentifier -> ShowS
    $cshow :: PackageIdentifier -> String
    show :: PackageIdentifier -> String
    $cshowList :: [PackageIdentifier] -> ShowS
    showList :: [PackageIdentifier] -> ShowS
    Show, PackageIdentifier -> PackageIdentifier -> Bool
    (PackageIdentifier -> PackageIdentifier -> Bool)
    -> (PackageIdentifier -> PackageIdentifier -> Bool)
    -> Eq PackageIdentifier
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: PackageIdentifier -> PackageIdentifier -> Bool
    == :: PackageIdentifier -> PackageIdentifier -> Bool
    $c/= :: PackageIdentifier -> PackageIdentifier -> Bool
    /= :: 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
    $ccompare :: PackageIdentifier -> PackageIdentifier -> Ordering
    compare :: PackageIdentifier -> PackageIdentifier -> Ordering
    $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
    >= :: PackageIdentifier -> PackageIdentifier -> Bool
    $cmax :: PackageIdentifier -> PackageIdentifier -> PackageIdentifier
    max :: PackageIdentifier -> PackageIdentifier -> PackageIdentifier
    $cmin :: PackageIdentifier -> PackageIdentifier -> PackageIdentifier
    min :: PackageIdentifier -> PackageIdentifier -> PackageIdentifier
    Ord, Typeable, Typeable PackageIdentifier
    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 -> Constr
    PackageIdentifier -> DataType
    (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)
    $cgfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> PackageIdentifier -> c PackageIdentifier
    gfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> PackageIdentifier -> c PackageIdentifier
    $cgunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c PackageIdentifier
    gunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c PackageIdentifier
    $ctoConstr :: PackageIdentifier -> Constr
    toConstr :: PackageIdentifier -> Constr
    $cdataTypeOf :: PackageIdentifier -> DataType
    dataTypeOf :: PackageIdentifier -> DataType
    $cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c PackageIdentifier)
    dataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c PackageIdentifier)
    $cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c PackageIdentifier)
    dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c PackageIdentifier)
    $cgmapT :: (forall b. Data b => b -> b)
    -> PackageIdentifier -> PackageIdentifier
    gmapT :: (forall b. Data b => b -> b)
    -> PackageIdentifier -> PackageIdentifier
    $cgmapQl :: 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
    $cgmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r
    gmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> PackageIdentifier -> r
    $cgmapQ :: forall u. (forall d. Data d => d -> u) -> PackageIdentifier -> [u]
    gmapQ :: forall u. (forall d. Data d => d -> u) -> PackageIdentifier -> [u]
    $cgmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> PackageIdentifier -> u
    gmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> PackageIdentifier -> u
    $cgmapM :: forall (m :: * -> *).
    Monad 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
    $cgmapMp :: 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
    $cgmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> PackageIdentifier -> m PackageIdentifier
    gmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> PackageIdentifier -> m 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 :: forall (m :: * -> *). CabalParsing m => m PackageIdentifier
    parsec = do
        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
    '-')
        (v, xs) <- case simpleParsec (NE.last xs') of
          Maybe Version
    Nothing -> (Version, [String]) -> m (Version, [String])
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Version
    nullVersion, NonEmpty String -> [String]
    forall a. NonEmpty a -> [a]
    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 a. a -> m a
    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 not (null xs) && 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)) xs
          then return $ PackageIdentifier (mkPackageName (intercalate "-" xs)) v
          else fail "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 () -> () -> ()
    forall a b. a -> b -> b
    `seq` Version -> ()
    forall a. NFData a => a -> ()
    rnf Version
    version