{-# LANGUAGE Safe #-}{- |Module : Relude.Extra.EnumCopyright : (c) 2018-2023 KowainikSPDX-License-Identifier : MITMaintainer : Kowainik <xrom.xkov@gmail.com>Stability : StablePortability : PortableMini @bounded-enum@ framework inside @relude@.@since 0.1.0-}module Relude.Extra.Enum ( next , prev , safeToEnum ) whereimport Relude{- | Like 'succ', but doesn't fail on 'maxBound'. Instead it returns 'minBound'.>>> next FalseTrue>>> next TrueFalse>>> succ True*** Exception: Prelude.Enum.Bool.succ: bad argument@since 0.1.0-}next :: (Eq a, Bounded a, Enum a) => a -> anext e | e == maxBound = minBound | otherwise = succ e{-# INLINE next #-}{- | Like 'pred', but doesn't fail on 'minBound'. Instead it returns 'maxBound'.>>> prev TrueFalse>>> prev FalseTrue>>> pred False*** Exception: Prelude.Enum.Bool.pred: bad argument@since 0.6.0.0-}prev :: (Eq a, Bounded a, Enum a) => a -> aprev e | e == minBound = maxBound | otherwise = pred e{-# INLINE prev #-}{- | Returns 'Nothing' if given 'Int' outside range.>>> safeToEnum @Bool 0Just False>>> safeToEnum @Bool 1Just True>>> safeToEnum @Bool 2Nothing>>> safeToEnum @Bool (-1)Nothing@since 0.1.0-}safeToEnum :: forall a . (Bounded a, Enum a) => Int -> Maybe asafeToEnum i = guard (fromEnum @a minBound <= i && i <= fromEnum @a maxBound) $> toEnum i{-# INLINE safeToEnum #-}