module Data.RPM.NVR (
NVR(..),
showNVR,
readNVR,
eitherNVR,
maybeNVR,
VerRel(..),
)
where
import Data.Either.Extra
import Data.List.Extra
import Data.RPM.VerRel
data NVR = NVR {NVR -> String
nvrName :: String,
NVR -> VerRel
nvrVerRel :: VerRel}
deriving NVR -> NVR -> Bool
(NVR -> NVR -> Bool) -> (NVR -> NVR -> Bool) -> Eq NVR
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NVR -> NVR -> Bool
$c/= :: NVR -> NVR -> Bool
== :: NVR -> NVR -> Bool
$c== :: NVR -> NVR -> Bool
Eq
showNVR :: NVR -> String
showNVR :: NVR -> String
showNVR (NVR String
nm VerRel
verrel) = String
nm String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"-" String -> String -> String
forall a. [a] -> [a] -> [a]
++ VerRel -> String
showVerRel VerRel
verrel
eitherNVR :: String -> Either String NVR
eitherNVR :: String -> Either String NVR
eitherNVR String
s =
case [String] -> [String]
forall a. [a] -> [a]
reverse (String -> String -> [String]
forall a. (Partial, Eq a) => [a] -> [a] -> [[a]]
splitOn String
"-" String
s) of
String
rel:String
ver:[String]
emaN ->
if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
rel Bool -> Bool -> Bool
|| String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
ver Bool -> Bool -> Bool
|| [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
emaN
then String -> Either String NVR
forall a b. a -> Either a b
Left (String -> Either String NVR) -> String -> Either String NVR
forall a b. (a -> b) -> a -> b
$ String
"NVR cannot start or end with '-'s: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
else NVR -> Either String NVR
forall a b. b -> Either a b
Right (String -> VerRel -> NVR
NVR (String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"-" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ [String] -> [String]
forall a. [a] -> [a]
reverse [String]
emaN) (String -> String -> VerRel
VerRel String
ver String
rel))
[String]
_ ->
String -> Either String NVR
forall a b. a -> Either a b
Left (String -> Either String NVR) -> String -> Either String NVR
forall a b. (a -> b) -> a -> b
$ String
"malformed NVR string: '" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'"
maybeNVR :: String -> Maybe NVR
maybeNVR :: String -> Maybe NVR
maybeNVR = Either String NVR -> Maybe NVR
forall a b. Either a b -> Maybe b
eitherToMaybe (Either String NVR -> Maybe NVR)
-> (String -> Either String NVR) -> String -> Maybe NVR
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String NVR
eitherNVR
readNVR :: String -> NVR
readNVR :: String -> NVR
readNVR = (String -> NVR) -> (NVR -> NVR) -> Either String NVR -> NVR
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> NVR
forall a. Partial => String -> a
error NVR -> NVR
forall a. a -> a
id (Either String NVR -> NVR)
-> (String -> Either String NVR) -> String -> NVR
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String NVR
eitherNVR