{-# LANGUAGE Safe #-}

{-|
Module      : Data.Char.BallotBox
Description : Support for the ballot box characters in unicode.
Maintainer  : hapytexeu+gh@gmail.com
Stability   : experimental
Portability : POSIX

Unicode has a <https://www.unicode.org/charts/PDF/U2600.pdf block> named /Miscellaneous Symbols/ that includes unicode characters for boxes that are empty, contain a check or a cross, this module aims to make it more convenient to render these.
-}

module Data.Char.BallotBox (
      -- * Represent a ballot box.
      BallotBox(Empty, Check, Cross)
      -- * Convert a boolean to a ballot box.
    , toCheckBox, toCrossBox
  ) where

import Data.Char.Core(UnicodeCharacter(toUnicodeChar, fromUnicodeChar, fromUnicodeChar'), UnicodeText, mapFromEnum, mapToEnum, mapToEnumSafe)

import Test.QuickCheck.Arbitrary(Arbitrary(arbitrary), arbitraryBoundedEnum)

_ballotOffset :: Int
_ballotOffset :: Int
_ballotOffset = Int
0x2610

-- | A datatype that represents the different types of ballot boxes.
data BallotBox
  = Empty  -- ^ The box is /empty/, this is represented with ☐.
  | Check  -- ^ The box has a /check/, this is represented with ☑.
  | Cross  -- ^ The box has a /cross/, this is represented with ☒.
  deriving (BallotBox
BallotBox -> BallotBox -> Bounded BallotBox
forall a. a -> a -> Bounded a
maxBound :: BallotBox
$cmaxBound :: BallotBox
minBound :: BallotBox
$cminBound :: BallotBox
Bounded, Int -> BallotBox
BallotBox -> Int
BallotBox -> [BallotBox]
BallotBox -> BallotBox
BallotBox -> BallotBox -> [BallotBox]
BallotBox -> BallotBox -> BallotBox -> [BallotBox]
(BallotBox -> BallotBox)
-> (BallotBox -> BallotBox)
-> (Int -> BallotBox)
-> (BallotBox -> Int)
-> (BallotBox -> [BallotBox])
-> (BallotBox -> BallotBox -> [BallotBox])
-> (BallotBox -> BallotBox -> [BallotBox])
-> (BallotBox -> BallotBox -> BallotBox -> [BallotBox])
-> Enum BallotBox
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: BallotBox -> BallotBox -> BallotBox -> [BallotBox]
$cenumFromThenTo :: BallotBox -> BallotBox -> BallotBox -> [BallotBox]
enumFromTo :: BallotBox -> BallotBox -> [BallotBox]
$cenumFromTo :: BallotBox -> BallotBox -> [BallotBox]
enumFromThen :: BallotBox -> BallotBox -> [BallotBox]
$cenumFromThen :: BallotBox -> BallotBox -> [BallotBox]
enumFrom :: BallotBox -> [BallotBox]
$cenumFrom :: BallotBox -> [BallotBox]
fromEnum :: BallotBox -> Int
$cfromEnum :: BallotBox -> Int
toEnum :: Int -> BallotBox
$ctoEnum :: Int -> BallotBox
pred :: BallotBox -> BallotBox
$cpred :: BallotBox -> BallotBox
succ :: BallotBox -> BallotBox
$csucc :: BallotBox -> BallotBox
Enum, BallotBox -> BallotBox -> Bool
(BallotBox -> BallotBox -> Bool)
-> (BallotBox -> BallotBox -> Bool) -> Eq BallotBox
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BallotBox -> BallotBox -> Bool
$c/= :: BallotBox -> BallotBox -> Bool
== :: BallotBox -> BallotBox -> Bool
$c== :: BallotBox -> BallotBox -> Bool
Eq, Eq BallotBox
Eq BallotBox
-> (BallotBox -> BallotBox -> Ordering)
-> (BallotBox -> BallotBox -> Bool)
-> (BallotBox -> BallotBox -> Bool)
-> (BallotBox -> BallotBox -> Bool)
-> (BallotBox -> BallotBox -> Bool)
-> (BallotBox -> BallotBox -> BallotBox)
-> (BallotBox -> BallotBox -> BallotBox)
-> Ord BallotBox
BallotBox -> BallotBox -> Bool
BallotBox -> BallotBox -> Ordering
BallotBox -> BallotBox -> BallotBox
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BallotBox -> BallotBox -> BallotBox
$cmin :: BallotBox -> BallotBox -> BallotBox
max :: BallotBox -> BallotBox -> BallotBox
$cmax :: BallotBox -> BallotBox -> BallotBox
>= :: BallotBox -> BallotBox -> Bool
$c>= :: BallotBox -> BallotBox -> Bool
> :: BallotBox -> BallotBox -> Bool
$c> :: BallotBox -> BallotBox -> Bool
<= :: BallotBox -> BallotBox -> Bool
$c<= :: BallotBox -> BallotBox -> Bool
< :: BallotBox -> BallotBox -> Bool
$c< :: BallotBox -> BallotBox -> Bool
compare :: BallotBox -> BallotBox -> Ordering
$ccompare :: BallotBox -> BallotBox -> Ordering
$cp1Ord :: Eq BallotBox
Ord, ReadPrec [BallotBox]
ReadPrec BallotBox
Int -> ReadS BallotBox
ReadS [BallotBox]
(Int -> ReadS BallotBox)
-> ReadS [BallotBox]
-> ReadPrec BallotBox
-> ReadPrec [BallotBox]
-> Read BallotBox
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BallotBox]
$creadListPrec :: ReadPrec [BallotBox]
readPrec :: ReadPrec BallotBox
$creadPrec :: ReadPrec BallotBox
readList :: ReadS [BallotBox]
$creadList :: ReadS [BallotBox]
readsPrec :: Int -> ReadS BallotBox
$creadsPrec :: Int -> ReadS BallotBox
Read, Int -> BallotBox -> ShowS
[BallotBox] -> ShowS
BallotBox -> String
(Int -> BallotBox -> ShowS)
-> (BallotBox -> String)
-> ([BallotBox] -> ShowS)
-> Show BallotBox
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BallotBox] -> ShowS
$cshowList :: [BallotBox] -> ShowS
show :: BallotBox -> String
$cshow :: BallotBox -> String
showsPrec :: Int -> BallotBox -> ShowS
$cshowsPrec :: Int -> BallotBox -> ShowS
Show)

-- | Convert the given 'Bool'ean to a 'BallotBox' that is 'Empty', or contains a 'Check'.
toCheckBox
  :: Bool  -- ^ The given 'Bool' that determines if the box contains a 'Check'.
  -> BallotBox  -- ^ The corresponding 'BallotBox'.
toCheckBox :: Bool -> BallotBox
toCheckBox = Int -> BallotBox
forall a. Enum a => Int -> a
toEnum (Int -> BallotBox) -> (Bool -> Int) -> Bool -> BallotBox
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Int
forall a. Enum a => a -> Int
fromEnum

-- | Convert the given 'Bool'ean to a 'BallotBox' that is 'Empty', or contains a 'Cross'.
toCrossBox
  :: Bool  -- ^ The given 'Bool' that determines if the box contains a 'Cross'.
  -> BallotBox  -- ^ The corresponding 'BallotBox'.
toCrossBox :: Bool -> BallotBox
toCrossBox = Int -> BallotBox
forall a. Enum a => Int -> a
toEnum (Int -> BallotBox) -> (Bool -> Int) -> Bool -> BallotBox
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
*) (Int -> Int) -> (Bool -> Int) -> Bool -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Int
forall a. Enum a => a -> Int
fromEnum

instance Arbitrary BallotBox where
    arbitrary :: Gen BallotBox
arbitrary = Gen BallotBox
forall a. (Bounded a, Enum a) => Gen a
arbitraryBoundedEnum

instance UnicodeCharacter BallotBox where
    toUnicodeChar :: BallotBox -> Char
toUnicodeChar = Int -> BallotBox -> Char
forall a. Enum a => Int -> a -> Char
mapFromEnum Int
_ballotOffset
    fromUnicodeChar :: Char -> Maybe BallotBox
fromUnicodeChar = Int -> Char -> Maybe BallotBox
forall a. (Bounded a, Enum a) => Int -> Char -> Maybe a
mapToEnumSafe Int
_ballotOffset
    fromUnicodeChar' :: Char -> BallotBox
fromUnicodeChar' = Int -> Char -> BallotBox
forall a. Enum a => Int -> Char -> a
mapToEnum Int
_ballotOffset

instance UnicodeText BallotBox