Skip to content

Commit

Permalink
use Text instead of ByteString for PackageIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
kadoban committed Nov 19, 2015
1 parent 13fe3ba commit 357d5f2
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Stack/Types/PackageIdentifier.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,20 @@ import Control.DeepSeq
import Control.Exception (Exception)
import Control.Monad.Catch (MonadThrow, throwM)
import Data.Aeson.Extended
import Data.Attoparsec.ByteString.Char8
import Data.Attoparsec.Text
import Data.Binary.VersionTagged (Binary, HasStructuralInfo)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as S8
import Data.Data
import Data.Hashable
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding (encodeUtf8)
import GHC.Generics
import Prelude hiding (FilePath)
import Stack.Types.PackageName
import Stack.Types.Version

-- | A parse fail.
data PackageIdentifierParseFail
= PackageIdentifierParseFail ByteString
= PackageIdentifierParseFail Text
deriving (Typeable)
instance Show PackageIdentifierParseFail where
show (PackageIdentifierParseFail bs) = "Invalid package identifier: " ++ show bs
Expand Down Expand Up @@ -66,7 +63,7 @@ instance ToJSON PackageIdentifier where
toJSON = toJSON . packageIdentifierString
instance FromJSON PackageIdentifier where
parseJSON = withText "PackageIdentifier" $ \t ->
case parsePackageIdentifier $ encodeUtf8 t of
case parsePackageIdentifier t of
Left e -> fail $ show (e, t)
Right x -> return x

Expand All @@ -82,12 +79,12 @@ fromTuple (n,v) = PackageIdentifier n v
packageIdentifierParser :: Parser PackageIdentifier
packageIdentifierParser =
do name <- packageNameParser
char8 '-'
char '-'
version <- versionParser
return (PackageIdentifier name version)

-- | Convenient way to parse a package identifier from a bytestring.
parsePackageIdentifier :: MonadThrow m => ByteString -> m PackageIdentifier
parsePackageIdentifier :: MonadThrow m => Text -> m PackageIdentifier
parsePackageIdentifier x = go x
where go =
either (const (throwM (PackageIdentifierParseFail x))) return .
Expand All @@ -96,7 +93,7 @@ parsePackageIdentifier x = go x
-- | Migration function.
parsePackageIdentifierFromString :: MonadThrow m => String -> m PackageIdentifier
parsePackageIdentifierFromString =
parsePackageIdentifier . S8.pack
parsePackageIdentifier . T.pack

-- | Get a string representation of the package identifier; name-ver.
packageIdentifierString :: PackageIdentifier -> String
Expand Down

0 comments on commit 357d5f2

Please sign in to comment.