Skip to content

Commit c82be5b

Browse files
authored
Merge pull request #4229 from phadej/is-string
Add IsString instance for ShortText newtypes
2 parents cdc2f91 + 988eec2 commit c82be5b

File tree

9 files changed

+57
-15
lines changed

9 files changed

+57
-15
lines changed

Cabal/Distribution/Compat/Prelude.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module Distribution.Compat.Prelude (
4040
Binary (..),
4141
Alternative (..),
4242
MonadPlus (..),
43+
IsString (..),
4344

4445
-- * Some types
4546
IO, NoCallStackIO,
@@ -131,6 +132,7 @@ import Data.List (intercalate, intersperse, isPrefixOf,
131132
isSuffixOf, nub, nubBy, sort, sortBy,
132133
unfoldr)
133134
import Data.Maybe
135+
import Data.String (IsString (..))
134136
import Data.Int
135137
import Data.Word
136138

Cabal/Distribution/ModuleName.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ simple str = ModuleName (stlFromStrings [str])
7676
-- an error if it is used with a string that is not a valid module name. If you
7777
-- are parsing user input then use 'Distribution.Text.simpleParse' instead.
7878
--
79-
fromString :: String -> ModuleName
80-
fromString string = fromComponents (split string)
81-
where
82-
split cs = case break (=='.') cs of
83-
(chunk,[]) -> chunk : []
84-
(chunk,_:rest) -> chunk : split rest
79+
instance IsString ModuleName where
80+
fromString string = fromComponents (split string)
81+
where
82+
split cs = case break (=='.') cs of
83+
(chunk,[]) -> chunk : []
84+
(chunk,_:rest) -> chunk : split rest
8585

8686
-- | Construct a 'ModuleName' from valid module components, i.e. parts
8787
-- separated by dots.

Cabal/Distribution/Package.hs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ unPackageName (PackageName s) = fromShortText s
8989
mkPackageName :: String -> PackageName
9090
mkPackageName = PackageName . toShortText
9191

92+
-- | 'mkPackageName'
93+
--
94+
-- @since 2.0
95+
instance IsString PackageName where
96+
fromString = mkPackageName
97+
9298
instance Binary PackageName
9399

94100
instance Text PackageName where
@@ -123,6 +129,12 @@ unPkgconfigName (PkgconfigName s) = fromShortText s
123129
mkPkgconfigName :: String -> PkgconfigName
124130
mkPkgconfigName = PkgconfigName . toShortText
125131

132+
-- | 'mkPkgconfigName'
133+
--
134+
-- @since 2.0
135+
instance IsString PkgconfigName where
136+
fromString = mkPkgconfigName
137+
126138
instance Binary PkgconfigName
127139

128140
-- pkg-config allows versions and other letters in package names, eg
@@ -215,6 +227,12 @@ newtype ComponentId = ComponentId ShortText
215227
mkComponentId :: String -> ComponentId
216228
mkComponentId = ComponentId . toShortText
217229

230+
-- | 'mkComponentId'
231+
--
232+
-- @since 2.0
233+
instance IsString ComponentId where
234+
fromString = mkComponentId
235+
218236
-- | Convert 'ComponentId' to 'String'
219237
--
220238
-- @since 2.0
@@ -288,15 +306,21 @@ instance Text UnitId where
288306
disp = text . unUnitId
289307
parse = mkUnitId <$> Parse.munch1 (\c -> isAlphaNum c || c `elem` "-_.+")
290308

291-
unUnitId :: UnitId -> String
292-
unUnitId (UnitId s) = fromShortText s
293-
294309
-- | If you need backwards compatibility, consider using 'display'
295310
-- instead, which is supported by all versions of Cabal.
296311
--
312+
unUnitId :: UnitId -> String
313+
unUnitId (UnitId s) = fromShortText s
314+
297315
mkUnitId :: String -> UnitId
298316
mkUnitId = UnitId . toShortText
299317

318+
-- | 'mkUnitId'
319+
--
320+
-- @since 2.0
321+
instance IsString UnitId where
322+
fromString = mkUnitId
323+
300324
-- | A 'UnitId' for a definite package. The 'DefUnitId' invariant says
301325
-- that a 'UnitId' identified this way is definite; i.e., it has no
302326
-- unfilled holes.
@@ -388,6 +412,12 @@ unAbiHash (AbiHash h) = fromShortText h
388412
mkAbiHash :: String -> AbiHash
389413
mkAbiHash = AbiHash . toShortText
390414

415+
-- | 'mkAbiHash'
416+
--
417+
-- @since 2.0
418+
instance IsString AbiHash where
419+
fromString = mkAbiHash
420+
391421
instance Binary AbiHash
392422

393423
instance Text AbiHash where

Cabal/Distribution/Types/GenericPackageDescription.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ newtype FlagName = FlagName ShortText
102102
mkFlagName :: String -> FlagName
103103
mkFlagName = FlagName . toShortText
104104

105+
-- | 'mkFlagName'
106+
--
107+
-- @since 2.0
108+
instance IsString FlagName where
109+
fromString = mkFlagName
110+
105111
-- | Convert 'FlagName' to 'String'
106112
--
107113
-- @since 2.0

Cabal/Distribution/Types/UnqualComponentName.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ unUnqualComponentName (UnqualComponentName s) = fromShortText s
4444
mkUnqualComponentName :: String -> UnqualComponentName
4545
mkUnqualComponentName = UnqualComponentName . toShortText
4646

47+
-- | 'mkUnqualComponentName'
48+
--
49+
-- @since 2.0
50+
instance IsString UnqualComponentName where
51+
fromString = mkUnqualComponentName
52+
4753
instance Binary UnqualComponentName
4854

4955
instance Text UnqualComponentName where

Cabal/Distribution/Utils/ShortText.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import Prelude ()
1717
import Distribution.Compat.Prelude
1818
import Distribution.Utils.String
1919

20-
import Data.String (IsString(..))
21-
2220
#if defined(MIN_VERSION_bytestring)
2321
# if MIN_VERSION_bytestring(0,10,4)
2422
# define HAVE_SHORTBYTESTRING 1

cabal-install/Distribution/Client/Init.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import Distribution.Version
5353
import Distribution.Verbosity
5454
( Verbosity )
5555
import Distribution.ModuleName
56-
( ModuleName, fromString ) -- And for the Text instance
56+
( ModuleName ) -- And for the Text instance
5757
import Distribution.InstalledPackageInfo
5858
( InstalledPackageInfo, sourcePackageId, exposed )
5959
import qualified Distribution.Package as P

cabal-testsuite/cabal-testsuite.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ library
3232
Test.Cabal.Server
3333
Test.Cabal.Monad
3434
build-depends:
35-
aeson,
35+
aeson == 1.1.*,
3636
attoparsec,
3737
async,
3838
base,
3939
bytestring,
4040
transformers,
41-
optparse-applicative,
41+
optparse-applicative ==0.12.*,
4242
process,
4343
directory,
4444
filepath,

stack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extra-deps:
4040
- stm-2.4.4.1
4141
- tagged-0.8.4
4242
- tar-0.5.0.3
43-
- tasty-0.11.0.3
43+
- tasty-0.11.0.4
4444
- tasty-hunit-0.9.2
4545
- tasty-quickcheck-0.8.4
4646
- text-1.2.2.1

0 commit comments

Comments
 (0)