mirror of
https://github.com/commercialhaskell/stackage-server.git
synced 2026-02-01 13:50:25 +01:00
Tweak GhcMajorVersion impl
This commit is contained in:
parent
f37f112e8f
commit
04f649b5da
24
Types.hs
24
Types.hs
@ -7,6 +7,10 @@ import Data.Hashable (hashUsing)
|
|||||||
import Text.Blaze (ToMarkup)
|
import Text.Blaze (ToMarkup)
|
||||||
import Database.Persist.Sql (PersistFieldSql (sqlType))
|
import Database.Persist.Sql (PersistFieldSql (sqlType))
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
import qualified Data.Text.Lazy.Builder.Int as Builder
|
||||||
|
import qualified Data.Text.Lazy.Builder as Builder
|
||||||
|
import qualified Data.Text.Lazy as LText
|
||||||
|
import qualified Data.Text.Read as Reader
|
||||||
|
|
||||||
newtype PackageName = PackageName { unPackageName :: Text }
|
newtype PackageName = PackageName { unPackageName :: Text }
|
||||||
deriving (Show, Read, Typeable, Eq, Ord, Hashable, PathPiece, ToMarkup, PersistField, IsString)
|
deriving (Show, Read, Typeable, Eq, Ord, Hashable, PathPiece, ToMarkup, PersistField, IsString)
|
||||||
@ -119,18 +123,21 @@ instance PathPiece StackageExecutable where
|
|||||||
fromPathPiece "stackage-setup.exe" = Just StackageWindowsExecutable
|
fromPathPiece "stackage-setup.exe" = Just StackageWindowsExecutable
|
||||||
fromPathPiece _ = Nothing
|
fromPathPiece _ = Nothing
|
||||||
|
|
||||||
data GhcMajorVersion = GhcMajorVersion Int Int
|
data GhcMajorVersion = GhcMajorVersion !Int !Int
|
||||||
deriving (Eq)
|
deriving (Eq)
|
||||||
|
|
||||||
ghcMajorVersionToText :: GhcMajorVersion -> Text
|
ghcMajorVersionToText :: GhcMajorVersion -> Text
|
||||||
ghcMajorVersionToText (GhcMajorVersion a b)
|
ghcMajorVersionToText (GhcMajorVersion a b)
|
||||||
= pack (show a) <> "." <> pack (show b)
|
= LText.toStrict
|
||||||
|
$ Builder.toLazyText
|
||||||
|
$ Builder.decimal a <> "." <> Builder.decimal b
|
||||||
|
|
||||||
ghcMajorVersionFromText :: Text -> Maybe GhcMajorVersion
|
ghcMajorVersionFromText :: MonadPlus m => Text -> m GhcMajorVersion
|
||||||
ghcMajorVersionFromText t = case T.splitOn "." t of
|
ghcMajorVersionFromText t = case Reader.decimal t of
|
||||||
[readMay -> Just a, readMay -> Just b] ->
|
Right (a, T.uncons -> Just ('.', t')) -> case Reader.decimal t' of
|
||||||
Just $ GhcMajorVersion a b
|
Right (b, t'') | T.null t'' -> return $ GhcMajorVersion a b
|
||||||
_ -> Nothing
|
_ -> mzero
|
||||||
|
_ -> mzero
|
||||||
|
|
||||||
instance PersistFieldSql GhcMajorVersion where
|
instance PersistFieldSql GhcMajorVersion where
|
||||||
sqlType = sqlType . liftM ghcMajorVersionToText
|
sqlType = sqlType . liftM ghcMajorVersionToText
|
||||||
@ -147,8 +154,7 @@ instance Hashable GhcMajorVersion where
|
|||||||
hashWithSalt = hashUsing ghcMajorVersionToText
|
hashWithSalt = hashUsing ghcMajorVersionToText
|
||||||
|
|
||||||
instance FromJSON GhcMajorVersion where
|
instance FromJSON GhcMajorVersion where
|
||||||
parseJSON = withText "GhcMajorVersion" $
|
parseJSON = withText "GhcMajorVersion" ghcMajorVersionFromText
|
||||||
maybe mzero return . ghcMajorVersionFromText
|
|
||||||
|
|
||||||
instance ToJSON GhcMajorVersion where
|
instance ToJSON GhcMajorVersion where
|
||||||
toJSON = toJSON . ghcMajorVersionToText
|
toJSON = toJSON . ghcMajorVersionToText
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user