{-# LANGUAGE ConstraintKinds #-}

-- | This module extends "Data.Version" with extra utilities.
--   The package also exports the existing "Data.Version" functions.
module Data.Version.Extra(
    module Data.Version,
    readVersion
    ) where

import Partial
import Data.Version
import Data.List.Extra
import Text.ParserCombinators.ReadP


-- | Read a 'Version' or throw an exception.
--
-- > \x -> readVersion (showVersion x) == x
-- > readVersion "hello" == undefined
readVersion :: Partial => String -> Version
readVersion :: Partial => String -> Version
readVersion String
s =
    case [ Version
x | (Version
x,String
"") <- forall a. ReadP a -> ReadS a
readP_to_S ReadP Version
parseVersion forall a b. (a -> b) -> a -> b
$ String -> String
trimEnd String
s] of
        [Version
x] -> Version
x
        []  -> forall a. Partial => String -> a
error String
"Data.Version.Extra.readVersion: no parse"
        [Version]
_   -> forall a. Partial => String -> a
error String
"Data.Version.Extra.readVersion: ambiguous parse"