packages feed

phino-0.0.0.49 / src / Must.hs

-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Must (Must (..), inRange, exceedsUpperBound) whereimport Data.List (isInfixOf)import Text.Read (readMaybe)data Must  = MtDisabled  | MtExact Integer  | MtRange (Maybe Integer) (Maybe Integer)  deriving (Eq)instance Show Must where  show MtDisabled = "0"  show (MtExact n) = show n  show (MtRange Nothing Nothing) = ".."  show (MtRange Nothing (Just max)) = ".." ++ show max  show (MtRange (Just min) Nothing) = show min ++ ".."  show (MtRange (Just min) (Just max)) = show min ++ ".." ++ show maxinstance Read Must where  readsPrec _ "0" = [(MtDisabled, "")]  readsPrec _ s    | ".." `isInfixOf` s = parseRange s    | otherwise = parseExact s    where      parseRange :: String -> [(Must, String)]      parseRange str = case break (== '.') str of        (minStr, '.' : '.' : maxStr) ->          let minPart = if null minStr then Nothing else readMaybe minStr              maxPart = if null maxStr then Nothing else readMaybe maxStr           in case (minPart, maxPart, null minStr, null maxStr) of                (Nothing, Nothing, False, False) -> [] -- Invalid range: non-numeric values                (Nothing, Nothing, True, True) -> [] -- Invalid range: empty range '..'                (Nothing, Just max, True, False) ->                  [(MtRange Nothing (Just max), "") | max >= 0]                (Just min, Nothing, False, True) ->                  [(MtRange (Just min) Nothing, "") | min >= 0]                (Just min, Just max, False, False) ->                  [(MtRange (Just min) (Just max), "") | min >= 0 && max >= 0 && min <= max]                _ -> [] -- Invalid range format        _ -> [] -- Invalid range: expected format like '3..5', '3..', or '..5'      parseExact :: String -> [(Must, String)]      parseExact str = case readMaybe str of        Just n | n >= 0 -> [(if n == 0 then MtDisabled else MtExact n, "")]        Just _ -> [] -- Invalid value: must be non-negative        Nothing -> [] -- Invalid value: expected integerinRange :: Must -> Integer -> BoolinRange MtDisabled _ = TrueinRange (MtExact expected) actual = actual == expectedinRange (MtRange minVal maxVal) actual =  checkMin && checkMax  where    checkMin = maybe True (<= actual) minVal    checkMax = maybe True (>= actual) maxVal-- | Check if a value exceeds the upper bound of the rangeexceedsUpperBound :: Must -> Integer -> BoolexceedsUpperBound MtDisabled _ = FalseexceedsUpperBound (MtExact n) current = current > nexceedsUpperBound (MtRange _ (Just max)) current = current > maxexceedsUpperBound (MtRange _ Nothing) _ = False