{-# 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
"") <- ReadP Version -> ReadS Version
forall a. ReadP a -> ReadS a
readP_to_S ReadP Version
parseVersion ReadS Version -> ReadS Version
forall a b. (a -> b) -> a -> b
$ String -> String
trimEnd String
s] of
        [Version
x] -> Version
x
        []  -> String -> Version
forall a. Partial => String -> a
error String
"Data.Version.Extra.readVersion: no parse"
        [Version]
_   -> String -> Version
forall a. Partial => String -> a
error String
"Data.Version.Extra.readVersion: ambiguous parse"