on 63243ca help sign in
elk/compiler/Elm/Internal/Version.hs
73 lines 2.2 kB history raw
1{-# LANGUAGE DeriveDataTypeable, TemplateHaskell #-}
2module Elm.Internal.Version where
4import Control.Applicative
5import Data.Aeson
6import Data.Binary
7import Data.Char (isDigit)
8import qualified Data.List as List
9import Data.Typeable
10import qualified Data.Text as T
11import qualified Paths_Elm as This
12import qualified Data.Version as Version
14elmVersion :: Version
15elmVersion = V ns ""
16 where (Version.Version ns _) = This.version
18-- Data representation
20data Version = V [Int] String
21 deriving (Typeable,Eq)
23instance Ord Version where
24 compare (V ns tag) (V ns' tag') =
25 case compare ns ns' of
26 EQ -> compare tag' tag -- reverse comparison to favor ""
27 cmp -> cmp
29instance Show Version where
30 show (V ns tag) =
31 List.intercalate "." (map show ns) ++ if null tag then "" else "-" ++ tag
33instance Binary Version where
34 get = V <$> get <*> get
35 put (V ns tag) = do put ns
36 put tag
38tagless :: Version -> Bool
39tagless (V _ tag) = null tag
41fromString :: String -> Maybe Version
42fromString version = V <$> splitNumbers possibleNumbers <*> tag
43 where
44 (possibleNumbers, possibleTag) = break (=='-') version
46 tag = case possibleTag of
47 "" -> Just ""
48 '-':rest -> Just rest
49 _ -> Nothing
51 splitNumbers :: String -> Maybe [Int]
52 splitNumbers ns =
53 case span isDigit ns of
54 ("", _) -> Nothing
55 (number, []) -> Just [read number]
56 (number, '.':rest) -> (read number :) <$> splitNumbers rest
57 _ -> Nothing
59instance FromJSON Version where
60 parseJSON (String text) =
61 let string = T.unpack text in
62 case fromString string of
63 Just v -> return v
64 Nothing -> fail $ unlines
65 [ "Dependency file has an invalid version number: " ++ string
66 , "Must have format 0.1.2 or 0.1.2-tag"
67 ]
69 parseJSON _ = fail "Version number must be stored as a string."
71instance ToJSON Version where
72 toJSON version = toJSON (show version)