{-# LANGUAGE DeriveDataTypeable         #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Distribution.Types.UnqualComponentName
  ( UnqualComponentName, unUnqualComponentName, unUnqualComponentNameST, mkUnqualComponentName
  , packageNameToUnqualComponentName, unqualComponentNameToPackageName
  ) where

import Distribution.Compat.Prelude
import Distribution.Utils.ShortText
import Prelude ()

import Distribution.Parsec
import Distribution.Pretty
import Distribution.Types.PackageName

-- | An unqualified component name, for any kind of component.
--
-- This is distinguished from a 'ComponentName' and 'ComponentId'. The former
-- also states which of a library, executable, etc the name refers too. The
-- later uniquely identifiers a component and its closure.
--
-- @since 2.0.0.2
newtype UnqualComponentName = UnqualComponentName ShortText
  deriving ((forall x. UnqualComponentName -> Rep UnqualComponentName x)
-> (forall x. Rep UnqualComponentName x -> UnqualComponentName)
-> Generic UnqualComponentName
forall x. Rep UnqualComponentName x -> UnqualComponentName
forall x. UnqualComponentName -> Rep UnqualComponentName x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep UnqualComponentName x -> UnqualComponentName
$cfrom :: forall x. UnqualComponentName -> Rep UnqualComponentName x
Generic, ReadPrec [UnqualComponentName]
ReadPrec UnqualComponentName
Int -> ReadS UnqualComponentName
ReadS [UnqualComponentName]
(Int -> ReadS UnqualComponentName)
-> ReadS [UnqualComponentName]
-> ReadPrec UnqualComponentName
-> ReadPrec [UnqualComponentName]
-> Read UnqualComponentName
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [UnqualComponentName]
$creadListPrec :: ReadPrec [UnqualComponentName]
readPrec :: ReadPrec UnqualComponentName
$creadPrec :: ReadPrec UnqualComponentName
readList :: ReadS [UnqualComponentName]
$creadList :: ReadS [UnqualComponentName]
readsPrec :: Int -> ReadS UnqualComponentName
$creadsPrec :: Int -> ReadS UnqualComponentName
Read, Int -> UnqualComponentName -> ShowS
[UnqualComponentName] -> ShowS
UnqualComponentName -> String
(Int -> UnqualComponentName -> ShowS)
-> (UnqualComponentName -> String)
-> ([UnqualComponentName] -> ShowS)
-> Show UnqualComponentName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UnqualComponentName] -> ShowS
$cshowList :: [UnqualComponentName] -> ShowS
show :: UnqualComponentName -> String
$cshow :: UnqualComponentName -> String
showsPrec :: Int -> UnqualComponentName -> ShowS
$cshowsPrec :: Int -> UnqualComponentName -> ShowS
Show, UnqualComponentName -> UnqualComponentName -> Bool
(UnqualComponentName -> UnqualComponentName -> Bool)
-> (UnqualComponentName -> UnqualComponentName -> Bool)
-> Eq UnqualComponentName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UnqualComponentName -> UnqualComponentName -> Bool
$c/= :: UnqualComponentName -> UnqualComponentName -> Bool
== :: UnqualComponentName -> UnqualComponentName -> Bool
$c== :: UnqualComponentName -> UnqualComponentName -> Bool
Eq, Eq UnqualComponentName
Eq UnqualComponentName
-> (UnqualComponentName -> UnqualComponentName -> Ordering)
-> (UnqualComponentName -> UnqualComponentName -> Bool)
-> (UnqualComponentName -> UnqualComponentName -> Bool)
-> (UnqualComponentName -> UnqualComponentName -> Bool)
-> (UnqualComponentName -> UnqualComponentName -> Bool)
-> (UnqualComponentName
    -> UnqualComponentName -> UnqualComponentName)
-> (UnqualComponentName
    -> UnqualComponentName -> UnqualComponentName)
-> Ord UnqualComponentName
UnqualComponentName -> UnqualComponentName -> Bool
UnqualComponentName -> UnqualComponentName -> Ordering
UnqualComponentName -> UnqualComponentName -> UnqualComponentName
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 :: UnqualComponentName -> UnqualComponentName -> UnqualComponentName
$cmin :: UnqualComponentName -> UnqualComponentName -> UnqualComponentName
max :: UnqualComponentName -> UnqualComponentName -> UnqualComponentName
$cmax :: UnqualComponentName -> UnqualComponentName -> UnqualComponentName
>= :: UnqualComponentName -> UnqualComponentName -> Bool
$c>= :: UnqualComponentName -> UnqualComponentName -> Bool
> :: UnqualComponentName -> UnqualComponentName -> Bool
$c> :: UnqualComponentName -> UnqualComponentName -> Bool
<= :: UnqualComponentName -> UnqualComponentName -> Bool
$c<= :: UnqualComponentName -> UnqualComponentName -> Bool
< :: UnqualComponentName -> UnqualComponentName -> Bool
$c< :: UnqualComponentName -> UnqualComponentName -> Bool
compare :: UnqualComponentName -> UnqualComponentName -> Ordering
$ccompare :: UnqualComponentName -> UnqualComponentName -> Ordering
$cp1Ord :: Eq UnqualComponentName
Ord, Typeable, Typeable UnqualComponentName
DataType
Constr
Typeable UnqualComponentName
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> UnqualComponentName
    -> c UnqualComponentName)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c UnqualComponentName)
-> (UnqualComponentName -> Constr)
-> (UnqualComponentName -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c UnqualComponentName))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c UnqualComponentName))
-> ((forall b. Data b => b -> b)
    -> UnqualComponentName -> UnqualComponentName)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> UnqualComponentName -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> UnqualComponentName -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> UnqualComponentName -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> UnqualComponentName -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> UnqualComponentName -> m UnqualComponentName)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> UnqualComponentName -> m UnqualComponentName)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> UnqualComponentName -> m UnqualComponentName)
-> Data UnqualComponentName
UnqualComponentName -> DataType
UnqualComponentName -> Constr
(forall b. Data b => b -> b)
-> UnqualComponentName -> UnqualComponentName
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UnqualComponentName
-> c UnqualComponentName
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UnqualComponentName
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) -> UnqualComponentName -> u
forall u.
(forall d. Data d => d -> u) -> UnqualComponentName -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UnqualComponentName -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UnqualComponentName -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> UnqualComponentName -> m UnqualComponentName
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UnqualComponentName -> m UnqualComponentName
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UnqualComponentName
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UnqualComponentName
-> c UnqualComponentName
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c UnqualComponentName)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c UnqualComponentName)
$cUnqualComponentName :: Constr
$tUnqualComponentName :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> UnqualComponentName -> m UnqualComponentName
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UnqualComponentName -> m UnqualComponentName
gmapMp :: (forall d. Data d => d -> m d)
-> UnqualComponentName -> m UnqualComponentName
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> UnqualComponentName -> m UnqualComponentName
gmapM :: (forall d. Data d => d -> m d)
-> UnqualComponentName -> m UnqualComponentName
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> UnqualComponentName -> m UnqualComponentName
gmapQi :: Int -> (forall d. Data d => d -> u) -> UnqualComponentName -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> UnqualComponentName -> u
gmapQ :: (forall d. Data d => d -> u) -> UnqualComponentName -> [u]
$cgmapQ :: forall u.
(forall d. Data d => d -> u) -> UnqualComponentName -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UnqualComponentName -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> UnqualComponentName -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UnqualComponentName -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> UnqualComponentName -> r
gmapT :: (forall b. Data b => b -> b)
-> UnqualComponentName -> UnqualComponentName
$cgmapT :: (forall b. Data b => b -> b)
-> UnqualComponentName -> UnqualComponentName
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c UnqualComponentName)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c UnqualComponentName)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c UnqualComponentName)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c UnqualComponentName)
dataTypeOf :: UnqualComponentName -> DataType
$cdataTypeOf :: UnqualComponentName -> DataType
toConstr :: UnqualComponentName -> Constr
$ctoConstr :: UnqualComponentName -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UnqualComponentName
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c UnqualComponentName
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UnqualComponentName
-> c UnqualComponentName
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> UnqualComponentName
-> c UnqualComponentName
$cp1Data :: Typeable UnqualComponentName
Data,
            b -> UnqualComponentName -> UnqualComponentName
NonEmpty UnqualComponentName -> UnqualComponentName
UnqualComponentName -> UnqualComponentName -> UnqualComponentName
(UnqualComponentName -> UnqualComponentName -> UnqualComponentName)
-> (NonEmpty UnqualComponentName -> UnqualComponentName)
-> (forall b.
    Integral b =>
    b -> UnqualComponentName -> UnqualComponentName)
-> Semigroup UnqualComponentName
forall b.
Integral b =>
b -> UnqualComponentName -> UnqualComponentName
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: b -> UnqualComponentName -> UnqualComponentName
$cstimes :: forall b.
Integral b =>
b -> UnqualComponentName -> UnqualComponentName
sconcat :: NonEmpty UnqualComponentName -> UnqualComponentName
$csconcat :: NonEmpty UnqualComponentName -> UnqualComponentName
<> :: UnqualComponentName -> UnqualComponentName -> UnqualComponentName
$c<> :: UnqualComponentName -> UnqualComponentName -> UnqualComponentName
Semigroup, Semigroup UnqualComponentName
UnqualComponentName
Semigroup UnqualComponentName
-> UnqualComponentName
-> (UnqualComponentName
    -> UnqualComponentName -> UnqualComponentName)
-> ([UnqualComponentName] -> UnqualComponentName)
-> Monoid UnqualComponentName
[UnqualComponentName] -> UnqualComponentName
UnqualComponentName -> UnqualComponentName -> UnqualComponentName
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [UnqualComponentName] -> UnqualComponentName
$cmconcat :: [UnqualComponentName] -> UnqualComponentName
mappend :: UnqualComponentName -> UnqualComponentName -> UnqualComponentName
$cmappend :: UnqualComponentName -> UnqualComponentName -> UnqualComponentName
mempty :: UnqualComponentName
$cmempty :: UnqualComponentName
$cp1Monoid :: Semigroup UnqualComponentName
Monoid) -- TODO: bad enabler of bad monoids

-- | Convert 'UnqualComponentName' to 'String'
--
-- @since 2.0.0.2
unUnqualComponentName :: UnqualComponentName -> String
unUnqualComponentName :: UnqualComponentName -> String
unUnqualComponentName (UnqualComponentName ShortText
s) = ShortText -> String
fromShortText ShortText
s

-- | @since 3.4.0.0
unUnqualComponentNameST :: UnqualComponentName -> ShortText
unUnqualComponentNameST :: UnqualComponentName -> ShortText
unUnqualComponentNameST (UnqualComponentName ShortText
s) = ShortText
s

-- | Construct a 'UnqualComponentName' from a 'String'
--
-- 'mkUnqualComponentName' is the inverse to 'unUnqualComponentName'
--
-- Note: No validations are performed to ensure that the resulting
-- 'UnqualComponentName' is valid
--
-- @since 2.0.0.2
mkUnqualComponentName :: String -> UnqualComponentName
mkUnqualComponentName :: String -> UnqualComponentName
mkUnqualComponentName = ShortText -> UnqualComponentName
UnqualComponentName (ShortText -> UnqualComponentName)
-> (String -> ShortText) -> String -> UnqualComponentName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShortText
toShortText

-- | 'mkUnqualComponentName'
--
-- @since 2.0.0.2
instance IsString UnqualComponentName where
  fromString :: String -> UnqualComponentName
fromString = String -> UnqualComponentName
mkUnqualComponentName

instance Binary UnqualComponentName
instance Structured UnqualComponentName

instance Pretty UnqualComponentName where
  pretty :: UnqualComponentName -> Doc
pretty = String -> Doc
showToken (String -> Doc)
-> (UnqualComponentName -> String) -> UnqualComponentName -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnqualComponentName -> String
unUnqualComponentName

instance Parsec UnqualComponentName where
  parsec :: m UnqualComponentName
parsec = String -> UnqualComponentName
mkUnqualComponentName (String -> UnqualComponentName)
-> m String -> m UnqualComponentName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m String
forall (m :: * -> *). CabalParsing m => m String
parsecUnqualComponentName

instance NFData UnqualComponentName where
  rnf :: UnqualComponentName -> ()
rnf (UnqualComponentName ShortText
pkg) = ShortText -> ()
forall a. NFData a => a -> ()
rnf ShortText
pkg

-- TODO avoid String round trip with these PackageName <->
-- UnqualComponentName converters.

-- | Converts a package name to an unqualified component name
--
-- Useful in legacy situations where a package name may refer to an internal
-- component, if one is defined with that name.
--
-- 2018-12-21: These "legacy" situations are not legacy.
-- We can @build-depends@ on the internal library. However
-- Now dependency contains @Set LibraryName@, and we should use that.
--
-- @since 2.0.0.2
packageNameToUnqualComponentName :: PackageName -> UnqualComponentName
packageNameToUnqualComponentName :: PackageName -> UnqualComponentName
packageNameToUnqualComponentName = ShortText -> UnqualComponentName
UnqualComponentName (ShortText -> UnqualComponentName)
-> (PackageName -> ShortText) -> PackageName -> UnqualComponentName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PackageName -> ShortText
unPackageNameST

-- | Converts an unqualified component name to a package name
--
-- `packageNameToUnqualComponentName` is the inverse of
-- `unqualComponentNameToPackageName`.
--
-- Useful in legacy situations where a package name may refer to an internal
-- component, if one is defined with that name.
--
-- @since 2.0.0.2
unqualComponentNameToPackageName :: UnqualComponentName -> PackageName
unqualComponentNameToPackageName :: UnqualComponentName -> PackageName
unqualComponentNameToPackageName = ShortText -> PackageName
mkPackageNameST (ShortText -> PackageName)
-> (UnqualComponentName -> ShortText)
-> UnqualComponentName
-> PackageName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnqualComponentName -> ShortText
unUnqualComponentNameST