blob: 6970968c82bba6ac5d3ef4b56dbdb974bd6e3a05 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
module Version
( Version (..),
versionToString,
versionFromText,
versionFromString,
)
where
import Data.Aeson qualified as A
import Data.Aeson.Types qualified as A
import Data.Function (on)
import Data.List
import Data.List.Split
import Data.Maybe (fromMaybe)
import Data.String (IsString (..))
import Data.Text qualified as T
data Version = Version Int Int Int
deriving (Show, Eq)
instance Ord Version where
compare = compare `on` toTriple
toTriple :: Version -> (Int, Int, Int)
toTriple (Version major minor patch) = (major, minor, patch)
instance A.ToJSON Version where
toJSON = A.toJSON . versionToString
instance A.FromJSON Version where
parseJSON (A.String (versionFromText -> Just version)) = pure version
parseJSON v = A.typeMismatch "version" v
versionToString :: Version -> String
versionToString (Version major minor patch) =
intercalate "." (map show [major, minor, patch])
versionFromString :: String -> Maybe Version
versionFromString (map read . splitOn "." -> [major, minor, patch]) =
Just (Version major minor patch)
versionFromString _ = Nothing
versionFromText :: T.Text -> Maybe Version
versionFromText = versionFromString . T.unpack
instance IsString Version where
fromString = fromMaybe (error "") . versionFromString
|