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