X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FMaskbits.hs;h=39a15b33908842a50aa85fe63e7fad0427175731;hb=5531fdace1587f4229a62c5284a4e76230f952ae;hp=cb4ab8986b2bcd343540baa56dbda4a11e7d9dc6;hpb=ac6042568d7c10b3d9d070fb3c4b1864256e51ac;p=hath.git diff --git a/src/Maskbits.hs b/src/Maskbits.hs index cb4ab89..39a15b3 100644 --- a/src/Maskbits.hs +++ b/src/Maskbits.hs @@ -1,22 +1,128 @@ -module Maskbits -( Maskbits(..), - maskbits_from_string -) where +module Maskbits( + Maskbits(..) ) +where --- A type representing the number of bits in a CIDR netmask. -data Maskbits = None | Maskbits Int - deriving (Eq, Show) +import Test.Tasty.QuickCheck ( Arbitrary(arbitrary), elements ) --- There are only 32 bits in an IPv4 address, so there --- can't be more bits than that in the mask. -maskbits_from_int :: Int -> Maskbits -maskbits_from_int x | (x < 0) || (x > 32) = None - | otherwise = Maskbits x +-- | A type representing the number of bits in a CIDR netmask. +data Maskbits = + Zero + | One + | Two + | Three + | Four + | Five + | Six + | Seven + | Eight + | Nine + | Ten + | Eleven + | Twelve + | Thirteen + | Fourteen + | Fifteen + | Sixteen + | Seventeen + | Eighteen + | Nineteen + | Twenty + | TwentyOne + | TwentyTwo + | TwentyThree + | TwentyFour + | TwentyFive + | TwentySix + | TwentySeven + | TwentyEight + | TwentyNine + | Thirty + | ThirtyOne + | ThirtyTwo + deriving (Enum, Eq, Ord) -maskbits_from_string :: String -> Maskbits -maskbits_from_string s = +instance Show Maskbits where + show Zero = "0" + show One = "1" + show Two = "2" + show Three = "3" + show Four = "4" + show Five = "5" + show Six = "6" + show Seven = "7" + show Eight = "8" + show Nine = "9" + show Ten = "10" + show Eleven = "11" + show Twelve = "12" + show Thirteen = "13" + show Fourteen = "14" + show Fifteen = "15" + show Sixteen = "16" + show Seventeen = "17" + show Eighteen = "18" + show Nineteen = "19" + show Twenty = "20" + show TwentyOne = "21" + show TwentyTwo = "22" + show TwentyThree = "23" + show TwentyFour = "24" + show TwentyFive = "25" + show TwentySix = "26" + show TwentySeven = "27" + show TwentyEight = "28" + show TwentyNine = "29" + show Thirty = "30" + show ThirtyOne = "31" + show ThirtyTwo = "32" + + +instance Arbitrary Maskbits where + arbitrary = + elements [ Zero, + One, + Two, + Three, + Four, + Five, + Six, + Seven, + Eight, + Nine, + Ten, + Eleven, + Twelve, + Thirteen, + Fourteen, + Fifteen, + Sixteen, + Seventeen, + Eighteen, + Nineteen, + Twenty, + TwentyOne, + TwentyTwo, + TwentyThree, + TwentyFour, + TwentyFive, + TwentySix, + TwentySeven, + TwentyEight, + TwentyNine, + Thirty, + ThirtyOne, + ThirtyTwo ] + + +instance Read Maskbits where + readsPrec _ s = case (reads s :: [(Int, String)]) of - [] -> None - x:_ -> maskbits_from_int (fst x) + [] -> [] + (x,leftover):_ -> go x leftover + where + go :: Int -> String -> [(Maskbits, String)] + go x' leftover' + | x' < minBound || x' > maxBound = [] + | otherwise = [(toEnum x', leftover')]