Skip to content

Commit 2142a6a

Browse files
committed
Add Pretty/Parsec/Described FlagAssignment instances
The +/- prefix is now mandatory.
1 parent b9dbc12 commit 2142a6a

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

Cabal/Distribution/Types/Flag.hs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Distribution.Types.Flag (
1919
dispFlagAssignment,
2020
parsecFlagAssignment,
2121
parsecFlagAssignmentNonEmpty,
22-
describeFlagAssignment,
22+
describeFlagAssignmentNonEmpty,
2323
) where
2424

2525
import Prelude ()
@@ -240,6 +240,36 @@ showFlagValue :: (FlagName, Bool) -> String
240240
showFlagValue (f, True) = '+' : unFlagName f
241241
showFlagValue (f, False) = '-' : unFlagName f
242242

243+
-- | @since 3.4.0.0
244+
instance Pretty FlagAssignment where
245+
pretty = dispFlagAssignment
246+
247+
-- |
248+
--
249+
-- >>> simpleParsec "" :: Maybe FlagAssignment
250+
-- Just (fromList [])
251+
--
252+
-- >>> simpleParsec "+foo -bar" :: Maybe FlagAssignment
253+
-- Just (fromList [(FlagName "bar",(1,False)),(FlagName "foo",(1,True))])
254+
--
255+
-- >>> simpleParsec "-none -any" :: Maybe FlagAssignment
256+
-- Just (fromList [(FlagName "any",(1,False)),(FlagName "none",(1,False))])
257+
--
258+
-- >>> simpleParsec "+foo -foo +foo +foo" :: Maybe FlagAssignment
259+
-- Just (fromList [(FlagName "foo",(4,True))])
260+
--
261+
-- >>> simpleParsec "+foo -bar baz" :: Maybe FlagAssignment
262+
-- Nothing
263+
--
264+
-- @since 3.4.0.0
265+
--
266+
instance Parsec FlagAssignment where
267+
parsec = parsecFlagAssignment
268+
269+
instance Described FlagAssignment where
270+
describe _ = REMunch RESpaces1 $
271+
REUnion [fromString "+", fromString "-"] <> describe (Proxy :: Proxy FlagName)
272+
243273
-- | Pretty-prints a flag assignment.
244274
dispFlagAssignment :: FlagAssignment -> Disp.Doc
245275
dispFlagAssignment = Disp.hsep . map (Disp.text . showFlagValue) . unFlagAssignment
@@ -250,7 +280,7 @@ parsecFlagAssignment = mkFlagAssignment <$>
250280
P.sepBy (onFlag <|> offFlag) P.skipSpaces1
251281
where
252282
onFlag = do
253-
_ <- P.optional (P.char '+')
283+
_ <- P.char '+'
254284
f <- parsec
255285
return (f, True)
256286
offFlag = do
@@ -276,6 +306,6 @@ parsecFlagAssignmentNonEmpty = mkFlagAssignment . toList <$>
276306
f <- parsec
277307
return (f, False)
278308

279-
describeFlagAssignment :: GrammarRegex void
280-
describeFlagAssignment = REMunch1 RESpaces1 $
309+
describeFlagAssignmentNonEmpty :: GrammarRegex void
310+
describeFlagAssignmentNonEmpty = REMunch1 RESpaces1 $
281311
REUnion [fromString "+", fromString "-"] <> describe (Proxy :: Proxy FlagName)

Cabal/tests/UnitTests/Distribution/Described.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import qualified Distribution.Utils.CharSet as CS
2020

2121
import Distribution.ModuleName (ModuleName)
2222
import Distribution.Types.Dependency (Dependency)
23-
import Distribution.Types.Flag (FlagName)
23+
import Distribution.Types.Flag (FlagName, FlagAssignment)
2424
import Distribution.Types.PackageId (PackageIdentifier)
2525
import Distribution.Types.PackageName (PackageName)
2626
import Distribution.Types.PackageVersionConstraint (PackageVersionConstraint)
@@ -43,6 +43,7 @@ tests = testGroup "Described"
4343
, testDescribed (Proxy :: Proxy Version)
4444
, testDescribed (Proxy :: Proxy VersionRange)
4545
, testDescribed (Proxy :: Proxy FlagName)
46+
, testDescribed (Proxy :: Proxy FlagAssignment)
4647
, testDescribed (Proxy :: Proxy ModuleName)
4748
, testDescribed (Proxy :: Proxy OS)
4849
, testDescribed (Proxy :: Proxy Arch)

cabal-install/Distribution/Client/Targets.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import Distribution.Types.PackageVersionConstraint
7979
import Distribution.PackageDescription
8080
( GenericPackageDescription )
8181
import Distribution.Types.Flag
82-
( nullFlagAssignment, parsecFlagAssignmentNonEmpty, describeFlagAssignment )
82+
( nullFlagAssignment, parsecFlagAssignmentNonEmpty, describeFlagAssignmentNonEmpty )
8383
import Distribution.Version
8484
( VersionRange, anyVersion, isAnyVersion )
8585
import Distribution.Pretty (Pretty (..), prettyShow)
@@ -745,7 +745,7 @@ instance Described UserConstraint where
745745
, fromString "source"
746746
, fromString "test"
747747
, fromString "bench"
748-
, describeFlagAssignment
748+
, describeFlagAssignmentNonEmpty
749749
]
750750

751751
describePN :: GrammarRegex void

0 commit comments

Comments
 (0)