packages feed

xnobar-1.0.0.1: lib/Positive32/XNobar/Internal/Positive32.hs

module XNobar.Internal.Positive32 (Positive32, positive32, toWord32) where

import Data.Char (isDigit)
import Data.Word (Word32)
import Data.Semigroup (Max(Max))
import Text.ParserCombinators.ReadP

-- |Bare-bone non-zero version of [Data.Word.Word32](https://hackage.haskell.org/package/base-4.20.0.1/docs/Data-Word.html#t:Word32)
-- that wraps around back to 1 after `maxBound` and implements the
-- [Data.Semigroup.Max](https://hackage.haskell.org/package/base-4.19.1.0/docs/Data-Semigroup.html#t:Max) typeclass.
newtype Positive32 = Positive32 { toWord32 :: Max Word32 } deriving (Eq, Ord, Semigroup, Show)

instance Monoid Positive32 where
  mempty = Positive32 1

instance Bounded Positive32 where
  minBound = mempty
  maxBound = Positive32 (maxBound :: Max Word32)

instance Enum Positive32 where
  toEnum = error "toEnum is not meant to be used"
  fromEnum = error "fromEnum is not meant to be used"
  pred = error "pred is not meant to be used"
  succ p@(Positive32 i)
    | p == maxBound = Positive32 1
    | otherwise = Positive32 $ succ i

-- |Smart ctor from [Data.Word.Word32](https://hackage.haskell.org/package/base-4.20.0.1/docs/Data-Word.html#t:Word32).
positive32 :: Word32 -> Positive32
positive32 0 = error "Attempt to create `Positive32` from `0`"
positive32 i = Positive32 $ Max i