Skip to content

Commit

Permalink
Add IsString instance for ShortText newtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Jan 12, 2017
1 parent 47884b3 commit fdc1df2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
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
(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

0 comments on commit fdc1df2

Please sign in to comment.