]> gitweb.michael.orlitzky.com - hath.git/blob - src/Maskbits.hs
Added the Maskbits module and type.
[hath.git] / src / Maskbits.hs
1 module Maskbits
2 ( Maskbits(..),
3 maskbits_from_string
4 ) where
5
6 -- A type representing the number of bits in a CIDR netmask.
7 data Maskbits = None | Maskbits Int
8 deriving (Eq, Show)
9
10
11 -- There are only 32 bits in an IPv4 address, so there
12 -- can't be more bits than that in the mask.
13 maskbits_from_int :: Int -> Maskbits
14 maskbits_from_int x | (x < 0) || (x > 32) = None
15 | otherwise = Maskbits x
16
17
18 maskbits_from_string :: String -> Maskbits
19 maskbits_from_string s =
20 case (reads s :: [(Int, String)]) of
21 [] -> None
22 x:_ -> maskbits_from_int (fst x)