make LTS branch pre-releases
[cabal.git] / Cabal-syntax / src / Distribution / Types / LibraryName.hs
blob2b8f53f4f89e1bb5b3072df6f41f907aa7d4bd3e
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE DeriveGeneric #-}
4 module Distribution.Types.LibraryName
5 ( LibraryName (..)
6 , defaultLibName
7 , maybeToLibraryName
8 , showLibraryName
9 , libraryNameStanza
10 , libraryNameString
12 -- * Pretty & Parse
13 , prettyLibraryNameComponent
14 , parsecLibraryNameComponent
15 ) where
17 import Distribution.Compat.Prelude
18 import Prelude ()
20 import Distribution.Parsec
21 import Distribution.Pretty
22 import Distribution.Types.UnqualComponentName
24 import qualified Distribution.Compat.CharParsing as P
25 import qualified Text.PrettyPrint as Disp
27 data LibraryName
28 = LMainLibName
29 | LSubLibName UnqualComponentName
30 deriving (Eq, Generic, Ord, Read, Show, Typeable, Data)
32 instance Binary LibraryName
33 instance Structured LibraryName
34 instance NFData LibraryName where rnf = genericRnf
36 -- | Pretty print 'LibraryName' in build-target-ish syntax.
38 -- /Note:/ there are no 'Pretty' or 'Parsec' instances,
39 -- as there's other way to represent 'LibraryName', namely as bare
40 -- 'UnqualComponentName'.
41 prettyLibraryNameComponent :: LibraryName -> Disp.Doc
42 prettyLibraryNameComponent LMainLibName = Disp.text "lib"
43 prettyLibraryNameComponent (LSubLibName str) = Disp.text "lib:" <<>> pretty str
45 parsecLibraryNameComponent :: CabalParsing m => m LibraryName
46 parsecLibraryNameComponent = do
47 _ <- P.string "lib"
48 parseComposite <|> parseSingle
49 where
50 parseSingle = return LMainLibName
51 parseComposite = do
52 _ <- P.char ':'
53 LSubLibName <$> parsec
55 defaultLibName :: LibraryName
56 defaultLibName = LMainLibName
58 showLibraryName :: LibraryName -> String
59 showLibraryName LMainLibName = "library"
60 showLibraryName (LSubLibName name) = "library '" ++ prettyShow name ++ "'"
62 libraryNameStanza :: LibraryName -> String
63 libraryNameStanza LMainLibName = "library"
64 libraryNameStanza (LSubLibName name) = "library " ++ prettyShow name
66 libraryNameString :: LibraryName -> Maybe UnqualComponentName
67 libraryNameString LMainLibName = Nothing
68 libraryNameString (LSubLibName n) = Just n
70 -- | Convert the 'UnqualComponentName' of a library into a
71 -- 'LibraryName'.
72 maybeToLibraryName :: Maybe UnqualComponentName -> LibraryName
73 maybeToLibraryName Nothing = LMainLibName
74 maybeToLibraryName (Just n) = LSubLibName n