aboutsummaryrefslogtreecommitdiffstats
path: root/common/src
diff options
context:
space:
mode:
Diffstat (limited to 'common/src')
-rw-r--r--common/src/Version.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/common/src/Version.hs b/common/src/Version.hs
new file mode 100644
index 0000000..cb568e6
--- /dev/null
+++ b/common/src/Version.hs
@@ -0,0 +1,36 @@
+module Version
+ ( Version (..),
+ versionToString,
+ versionFromText,
+ versionFromString,
+ )
+where
+
+import Data.Aeson qualified as A
+import Data.Aeson.Types qualified as A
+import Data.List
+import Data.List.Split
+import Data.Text qualified as T
+
+data Version = Version Int Int Int
+ deriving (Show, Eq)
+
+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