From: Michael Orlitzky Date: Sun, 2 May 2010 21:04:37 +0000 (-0400) Subject: Added the Maskbits module and type. X-Git-Tag: 0.0.1~78 X-Git-Url: http://gitweb.michael.orlitzky.com/?p=hath.git;a=commitdiff_plain;h=ac6042568d7c10b3d9d070fb3c4b1864256e51ac Added the Maskbits module and type. --- diff --git a/src/Maskbits.hs b/src/Maskbits.hs new file mode 100644 index 0000000..cb4ab89 --- /dev/null +++ b/src/Maskbits.hs @@ -0,0 +1,22 @@ +module Maskbits +( Maskbits(..), + maskbits_from_string +) where + +-- A type representing the number of bits in a CIDR netmask. +data Maskbits = None | Maskbits Int + deriving (Eq, Show) + + +-- 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 + + +maskbits_from_string :: String -> Maskbits +maskbits_from_string s = + case (reads s :: [(Int, String)]) of + [] -> None + x:_ -> maskbits_from_int (fst x)