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