refactor: make ExamOccurrenceCapacity a newtype

use pattern synonyms for convenience, so usage doesn't change
This commit is contained in:
Wolfgang Witt 2021-03-16 12:34:03 +01:00 committed by Gregor Kleen
parent ae3e1b6266
commit b5ee9f2c05

View File

@ -19,7 +19,7 @@ module Model.Types.Exam
, _examOccurrenceMappingRule
, _examOccurrenceMappingMapping
, traverseExamOccurrenceMapping
, ExamOccurrenceCapacity(..)
, ExamOccurrenceCapacity(.., Unrestricted, Restricted)
, _examOccurrenceCapacityIso
, ExamGrade(..)
, numberGrade
@ -231,15 +231,16 @@ traverseExamOccurrenceMapping = _examOccurrenceMappingMapping . iso Map.toList (
--
-- Maybe doesn't work, because the 'Ord' instance puts 'Nothing' below 0
-- instead of above every other number.
data ExamOccurrenceCapacity = Unrestricted | Restricted Natural
deriving (Show, Eq)
newtype ExamOccurrenceCapacity = EOCapacity (Maybe Natural)
deriving stock (Show)
deriving (Eq, Ord) via (NTop (Maybe Natural))
-- | Unrestricted is bigger then everything else, otherwise use instance from 'Natural'.
instance Ord ExamOccurrenceCapacity where
compare Unrestricted Unrestricted = EQ
compare Unrestricted (Restricted _n) = GT
compare (Restricted _n) Unrestricted = LT
compare (Restricted a) (Restricted b) = compare a b
pattern Unrestricted :: ExamOccurrenceCapacity
pattern Unrestricted = EOCapacity Nothing
pattern Restricted :: Natural -> ExamOccurrenceCapacity
pattern Restricted n = EOCapacity (Just n)
{-# COMPLETE Unrestricted, Restricted #-}
-- | Addition monoid with 'Unrestricted' interpreted as infinity.
instance Semigroup ExamOccurrenceCapacity where