module Data.RPM.VerRel (
VerRel(..),
showVerRel,
readVerRel,
eitherVerRel,
maybeVerRel
)
where
import Data.Either.Extra
import Data.List.Extra
import Data.RPM.VerCmp
data VerRel = VerRel String String
deriving (VerRel -> VerRel -> Bool
(VerRel -> VerRel -> Bool)
-> (VerRel -> VerRel -> Bool) -> Eq VerRel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VerRel -> VerRel -> Bool
$c/= :: VerRel -> VerRel -> Bool
== :: VerRel -> VerRel -> Bool
$c== :: VerRel -> VerRel -> Bool
Eq)
showVerRel :: VerRel -> String
showVerRel :: VerRel -> String
showVerRel (VerRel ver :: String
ver rel :: String
rel) = String
ver String -> String -> String
forall a. [a] -> [a] -> [a]
++ "-" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
rel
eitherVerRel :: String -> Either String VerRel
eitherVerRel :: String -> Either String VerRel
eitherVerRel s :: String
s =
case String -> String -> Maybe (String, String)
forall a. Eq a => [a] -> [a] -> Maybe ([a], [a])
stripInfixEnd "-" String
s of
Just (v :: String
v,r :: String
r) ->
if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
v Bool -> Bool -> Bool
|| String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
r
then String -> Either String VerRel
forall a. HasCallStack => String -> a
error (String -> Either String VerRel) -> String -> Either String VerRel
forall a b. (a -> b) -> a -> b
$ "VerRel cannot start or end with '-': " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
else VerRel -> Either String VerRel
forall a b. b -> Either a b
Right (String -> String -> VerRel
VerRel String
v String
r)
Nothing -> String -> Either String VerRel
forall a b. a -> Either a b
Left (String -> Either String VerRel) -> String -> Either String VerRel
forall a b. (a -> b) -> a -> b
$ "VerRel must contain a '-': " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
maybeVerRel :: String -> Maybe VerRel
maybeVerRel :: String -> Maybe VerRel
maybeVerRel = Either String VerRel -> Maybe VerRel
forall a b. Either a b -> Maybe b
eitherToMaybe (Either String VerRel -> Maybe VerRel)
-> (String -> Either String VerRel) -> String -> Maybe VerRel
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String VerRel
eitherVerRel
readVerRel :: String -> VerRel
readVerRel :: String -> VerRel
readVerRel = (String -> VerRel)
-> (VerRel -> VerRel) -> Either String VerRel -> VerRel
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> VerRel
forall a. HasCallStack => String -> a
error VerRel -> VerRel
forall a. a -> a
id (Either String VerRel -> VerRel)
-> (String -> Either String VerRel) -> String -> VerRel
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String VerRel
eitherVerRel
instance Ord VerRel where
compare :: VerRel -> VerRel -> Ordering
compare (VerRel v1 :: String
v1 r1 :: String
r1) (VerRel v2 :: String
v2 r2 :: String
r2) =
case String -> String -> Ordering
rpmVerCompare String
v1 String
v2 of
EQ -> String -> String -> Ordering
rpmVerCompare String
r1 String
r2
o :: Ordering
o -> Ordering
o