Skip to content

Commit 82bd399

Browse files
committed
Use some lens in D.PD.Parsec
1 parent a62b78f commit 82bd399

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

Cabal/Distribution/Compat/Lens.hs

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Distribution.Compat.Lens (
2626
-- * Operators
2727
(&),
2828
(.~), (%~),
29+
(?~),
2930
-- * Cabal developer info
3031
-- $development
3132
) where
@@ -110,12 +111,16 @@ _2 f (c, a) = (,) c <$> f a
110111
{-# INLINE (&) #-}
111112
infixl 1 &
112113

113-
infixr 4 .~, %~
114+
infixr 4 .~, %~, ?~
114115

115116
(.~) :: ASetter s t a b -> b -> s -> t
116117
(.~) = set
117118
{-# INLINE (.~) #-}
118119

120+
(?~) :: ASetter s t a (Maybe b) -> b -> s -> t
121+
l ?~ b = set l (Just b)
122+
{-# INLINE (?~) #-}
123+
119124
(%~) :: ASetter s t a b -> (a -> b) -> s -> t
120125
(%~) = over
121126
{-# INLINE (%~) #-}

Cabal/Distribution/PackageDescription/Parsec.hs

+9-9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ import System.Directory
6565
import qualified Text.Parsec as P
6666
import qualified Text.Parsec.Error as P
6767

68+
import Distribution.Compat.Lens
69+
import qualified Distribution.Types.GenericPackageDescription.Lens as L
70+
import qualified Distribution.Types.PackageDescription.Lens as L
71+
6872
-- ---------------------------------------------------------------
6973
-- Parsing
7074

@@ -282,10 +286,7 @@ parseGenericPackageDescription' lexWarnings fs = do
282286

283287
| name == "custom-setup" && null args = do
284288
sbi <- parseFields setupBInfoFieldDescrs warnUnrec mempty fields
285-
let pd = packageDescription gpd
286-
-- TODO: what if already defined?
287-
let gpd' = gpd { packageDescription = pd { setupBuildInfo = Just sbi } }
288-
pure gpd'
289+
pure $ gpd & L.packageDescription . L.setupBuildInfo ?~ sbi
289290

290291
| name == "source-repository" = do
291292
kind <- case args of
@@ -298,16 +299,15 @@ parseGenericPackageDescription' lexWarnings fs = do
298299
parseFailure pos $ "Invalid source-repository kind " ++ show args
299300
pure RepoHead
300301
sr <- parseFields sourceRepoFieldDescrs warnUnrec (emptySourceRepo kind) fields
301-
-- I want lens
302-
let pd = packageDescription gpd
303-
let srs = sourceRepos pd
304-
let gpd' = gpd { packageDescription = pd { sourceRepos = srs ++ [sr] } }
305-
pure gpd'
302+
303+
pure $ gpd & L.packageDescription . L.sourceRepos %~ snoc sr
306304

307305
| otherwise = do
308306
parseWarning pos PWTUnknownSection $ "Ignoring section: " ++ show name
309307
pure gpd
310308

309+
snoc x xs = xs ++ [x]
310+
311311
newSyntaxVersion :: Version
312312
newSyntaxVersion = mkVersion [1, 2]
313313

Cabal/Distribution/Types/PackageDescription/Lens.hs

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import Distribution.Compat.Lens
1010
import Distribution.Types.PackageDescription (PackageDescription)
1111
import qualified Distribution.Types.PackageDescription as T
1212

13+
import Distribution.Types.SetupBuildInfo (SetupBuildInfo)
14+
import Distribution.Types.SourceRepo (SourceRepo)
15+
1316
customFieldsPD :: Lens' PackageDescription [(String,String)]
1417
customFieldsPD f pd = fmap (\x -> pd { T.customFieldsPD = x }) (f (T.customFieldsPD pd))
1518

@@ -21,3 +24,9 @@ synopsis f pd = fmap (\x -> pd { T.synopsis = x }) (f (T.synopsis pd))
2124

2225
maintainer :: Lens' PackageDescription String
2326
maintainer f pd = fmap (\x -> pd { T.maintainer = x }) (f (T.maintainer pd))
27+
28+
setupBuildInfo :: Lens' PackageDescription (Maybe SetupBuildInfo)
29+
setupBuildInfo f pd = fmap (\x -> pd { T.setupBuildInfo = x }) (f (T.setupBuildInfo pd))
30+
31+
sourceRepos :: Lens' PackageDescription [SourceRepo]
32+
sourceRepos f pd = fmap (\x -> pd { T.sourceRepos = x }) (f (T.sourceRepos pd))

0 commit comments

Comments
 (0)