From: Michael Orlitzky Date: Sun, 2 May 2010 19:22:28 +0000 (-0400) Subject: Add a None constructor for IPv4Address. X-Git-Tag: 0.0.1~80 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=0913645a2ad512b0f32338a48c2bc71793be23a8;p=hath.git Add a None constructor for IPv4Address. Hide the constructor for IPv4Address. Created an ipv4address_from_octets function which mimics the constructor but will return IPv4Address.None in response to being passed Octet.Nones. --- diff --git a/src/IPv4Address.hs b/src/IPv4Address.hs index 3d6b1bb..7c5d80d 100644 --- a/src/IPv4Address.hs +++ b/src/IPv4Address.hs @@ -1,17 +1,43 @@ -module IPv4Address where +module IPv4Address +( ipv4address_from_octets, + IPv4Address(None), + Maskbits, + max_octet1, + max_octet2, + max_octet3, + max_octet4, + min_octet1, + min_octet2, + min_octet3, + min_octet4 +) + where import Bit import Octet type Maskbits = Int -data IPv4Address = IPv4Address { octet1 :: Octet, - octet2 :: Octet, - octet3 :: Octet, - octet4 :: Octet } +data IPv4Address = None | IPv4Address { octet1 :: Octet, + octet2 :: Octet, + octet3 :: Octet, + octet4 :: Octet } deriving (Eq, Show) +-- We don't export our constructor so this function is the only +-- way to construct an address from octets. As a result, we can +-- return IPv4Address.None in response to being passed one of more +-- Octet.None octets. +ipv4address_from_octets :: Octet -> Octet -> Octet -> Octet -> IPv4Address +ipv4address_from_octets oct1 oct2 oct3 oct4 + | or [oct1 == Octet.None, + oct2 == Octet.None, + oct3 == Octet.None, + oct4 == Octet.None] = IPv4Address.None + | otherwise = IPv4Address oct1 oct2 oct3 oct4 + + min_address :: IPv4Address -> Maskbits -> IPv4Address min_address addr mask | mask == 32 = IPv4Address oct1 oct2 oct3 oct4