devforms-0.1.1.0: src/ParsedInteger.hs
module ParsedInteger (ParsedInteger, parseWithBounds, parsedToInteger)
where
newtype ParsedInteger = ParsedInteger Integer
parseWithBounds :: Maybe Integer -> Maybe Integer -> Integer -> Either Text ParsedInteger
parseWithBounds (Just lowerBoundInclusive) (Just upperBoundInclusive) n =
if lowerBoundInclusive <= n && upperBoundInclusive >= n
then pure $ ParsedInteger n
else Left "Integer is outside bounds"
parseWithBounds (Just lowerBoundInclusive) Nothing n =
if lowerBoundInclusive <= n
then pure $ ParsedInteger n
else Left "Integer is out of bounds"
parseWithBounds Nothing (Just upperBoundInclusive) n =
if upperBoundInclusive >= n
then pure $ ParsedInteger n
else Left "Integer is out of bounds"
parseWithBounds Nothing Nothing n = Right $ ParsedInteger n
parsedToInteger :: ParsedInteger -> Integer
parsedToInteger (ParsedInteger n) = n