module Data.RPM.NV (
NV(..),
showNV,
readNV,
eitherNV,
maybeNV
)
where
import Data.Either.Extra
import Data.List.Extra
data NV = NV {NV -> String
name :: String,
NV -> String
version :: String}
deriving (NV -> NV -> Bool
(NV -> NV -> Bool) -> (NV -> NV -> Bool) -> Eq NV
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NV -> NV -> Bool
$c/= :: NV -> NV -> Bool
== :: NV -> NV -> Bool
$c== :: NV -> NV -> Bool
Eq)
showNV :: NV -> String
showNV :: NV -> String
showNV (NV String
nm String
ver) = String
nm String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"-" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ver
eitherNV :: String -> Either String NV
eitherNV :: String -> Either String NV
eitherNV String
"" = String -> Either String NV
forall a b. a -> Either a b
Left String
"NV cannot be empty string"
eitherNV String
s =
case String -> String -> Maybe (String, String)
forall a. Eq a => [a] -> [a] -> Maybe ([a], [a])
stripInfixEnd String
"-" String
s of
Just (String
n,String
v) ->
if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
n Bool -> Bool -> Bool
|| String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
v
then String -> Either String NV
forall a b. a -> Either a b
Left (String -> Either String NV) -> String -> Either String NV
forall a b. (a -> b) -> a -> b
$ String
"NV must not start or end with '-': " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
else NV -> Either String NV
forall a b. b -> Either a b
Right (String -> String -> NV
NV String
n String
v)
Maybe (String, String)
Nothing -> String -> Either String NV
forall a b. a -> Either a b
Left (String -> Either String NV) -> String -> Either String NV
forall a b. (a -> b) -> a -> b
$ String
"NV must contain '-': " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s
maybeNV :: String -> Maybe NV
maybeNV :: String -> Maybe NV
maybeNV = Either String NV -> Maybe NV
forall a b. Either a b -> Maybe b
eitherToMaybe (Either String NV -> Maybe NV)
-> (String -> Either String NV) -> String -> Maybe NV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String NV
eitherNV
readNV :: String -> NV
readNV :: String -> NV
readNV = (String -> NV) -> (NV -> NV) -> Either String NV -> NV
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> NV
forall a. HasCallStack => String -> a
error NV -> NV
forall a. a -> a
id (Either String NV -> NV)
-> (String -> Either String NV) -> String -> NV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String NV
eitherNV