Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsString instance for ShortText newtypes #4229

Merged
merged 2 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cabal/Distribution/Compat/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module Distribution.Compat.Prelude (
Binary (..),
Alternative (..),
MonadPlus (..),
IsString (..),

-- * Some types
IO, NoCallStackIO,
Expand Down Expand Up @@ -131,6 +132,7 @@ import Data.List (intercalate, intersperse, isPrefixOf,
isSuffixOf, nub, nubBy, sort, sortBy,
unfoldr)
import Data.Maybe
import Data.String (IsString (..))
import Data.Int
import Data.Word

Expand Down
12 changes: 6 additions & 6 deletions Cabal/Distribution/ModuleName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ simple str = ModuleName (stlFromStrings [str])
-- an error if it is used with a string that is not a valid module name. If you
-- are parsing user input then use 'Distribution.Text.simpleParse' instead.
--
fromString :: String -> ModuleName
fromString string = fromComponents (split string)
where
split cs = case break (=='.') cs of
(chunk,[]) -> chunk : []
(chunk,_:rest) -> chunk : split rest
instance IsString ModuleName where
fromString string = fromComponents (split string)
where
split cs = case break (=='.') cs of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth copying splitOn from Data.List.Split to D.S.Utils.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it later. This commit just moves as little code as possible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Collaborator Author

@phadej phadej Jan 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now i remember, splitOn is quite general reusing other combinators in split. Also this is less general splitOn' :: a -> [a] -> [[a]], where Data.List.Split.splitOn :: [a] -> [a] -> [[a]]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, this is more like Data.List.wordsBy.

(chunk,[]) -> chunk : []
(chunk,_:rest) -> chunk : split rest

-- | Construct a 'ModuleName' from valid module components, i.e. parts
-- separated by dots.
Expand Down
36 changes: 33 additions & 3 deletions Cabal/Distribution/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ unPackageName (PackageName s) = fromShortText s
mkPackageName :: String -> PackageName
mkPackageName = PackageName . toShortText

-- | 'mkPackageName'
--
-- @since 2.0
instance IsString PackageName where
fromString = mkPackageName

instance Binary PackageName

instance Text PackageName where
Expand Down Expand Up @@ -123,6 +129,12 @@ unPkgconfigName (PkgconfigName s) = fromShortText s
mkPkgconfigName :: String -> PkgconfigName
mkPkgconfigName = PkgconfigName . toShortText

-- | 'mkPkgconfigName'
--
-- @since 2.0
instance IsString PkgconfigName where
fromString = mkPkgconfigName

instance Binary PkgconfigName

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

-- | 'mkComponentId'
--
-- @since 2.0
instance IsString ComponentId where
fromString = mkComponentId

-- | Convert 'ComponentId' to 'String'
--
-- @since 2.0
Expand Down Expand Up @@ -288,15 +306,21 @@ instance Text UnitId where
disp = text . unUnitId
parse = mkUnitId <$> Parse.munch1 (\c -> isAlphaNum c || c `elem` "-_.+")

unUnitId :: UnitId -> String
unUnitId (UnitId s) = fromShortText s

-- | If you need backwards compatibility, consider using 'display'
-- instead, which is supported by all versions of Cabal.
--
unUnitId :: UnitId -> String
unUnitId (UnitId s) = fromShortText s

mkUnitId :: String -> UnitId
mkUnitId = UnitId . toShortText

-- | 'mkUnitId'
--
-- @since 2.0
instance IsString UnitId where
fromString = mkUnitId

-- | A 'UnitId' for a definite package. The 'DefUnitId' invariant says
-- that a 'UnitId' identified this way is definite; i.e., it has no
-- unfilled holes.
Expand Down Expand Up @@ -388,6 +412,12 @@ unAbiHash (AbiHash h) = fromShortText h
mkAbiHash :: String -> AbiHash
mkAbiHash = AbiHash . toShortText

-- | 'mkAbiHash'
--
-- @since 2.0
instance IsString AbiHash where
fromString = mkAbiHash

instance Binary AbiHash

instance Text AbiHash where
Expand Down
6 changes: 6 additions & 0 deletions Cabal/Distribution/Types/GenericPackageDescription.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ newtype FlagName = FlagName ShortText
mkFlagName :: String -> FlagName
mkFlagName = FlagName . toShortText

-- | 'mkFlagName'
--
-- @since 2.0
instance IsString FlagName where
fromString = mkFlagName

-- | Convert 'FlagName' to 'String'
--
-- @since 2.0
Expand Down
6 changes: 6 additions & 0 deletions Cabal/Distribution/Types/UnqualComponentName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ unUnqualComponentName (UnqualComponentName s) = fromShortText s
mkUnqualComponentName :: String -> UnqualComponentName
mkUnqualComponentName = UnqualComponentName . toShortText

-- | 'mkUnqualComponentName'
--
-- @since 2.0
instance IsString UnqualComponentName where
fromString = mkUnqualComponentName

instance Binary UnqualComponentName

instance Text UnqualComponentName where
Expand Down
2 changes: 0 additions & 2 deletions Cabal/Distribution/Utils/ShortText.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import Prelude ()
import Distribution.Compat.Prelude
import Distribution.Utils.String

import Data.String (IsString(..))

#if defined(MIN_VERSION_bytestring)
# if MIN_VERSION_bytestring(0,10,4)
# define HAVE_SHORTBYTESTRING 1
Expand Down
2 changes: 1 addition & 1 deletion cabal-install/Distribution/Client/Init.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import Distribution.Version
import Distribution.Verbosity
( Verbosity )
import Distribution.ModuleName
( ModuleName, fromString ) -- And for the Text instance
( ModuleName ) -- And for the Text instance
import Distribution.InstalledPackageInfo
( InstalledPackageInfo, sourcePackageId, exposed )
import qualified Distribution.Package as P
Expand Down
4 changes: 2 additions & 2 deletions cabal-testsuite/cabal-testsuite.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ library
Test.Cabal.Server
Test.Cabal.Monad
build-depends:
aeson,
aeson == 1.1.*,
attoparsec,
async,
base,
bytestring,
transformers,
optparse-applicative,
optparse-applicative ==0.12.*,
process,
directory,
filepath,
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extra-deps:
- stm-2.4.4.1
- tagged-0.8.4
- tar-0.5.0.3
- tasty-0.11.0.3
- tasty-0.11.0.4
- tasty-hunit-0.9.2
- tasty-quickcheck-0.8.4
- text-1.2.2.1
Expand Down