IP addresses: slight change in errors naming.

master
Philippe Pittoli 2024-02-10 15:36:30 +01:00
parent d7f713839c
commit 51bdcc3ba4
1 changed files with 13 additions and 14 deletions

View File

@ -19,19 +19,18 @@ import GenericParser.SomeParsers (nat)
import GenericParser.RFC5234 (hexdig)
data IPv6Error
= InvalidCharacter
| TooManyHexaDecimalCharacters
| NotEnoughChunks
| TooManyChunks
| IPv6UnrelevantShortRepresentation
= IP6TooManyHexaDecimalCharacters
| IP6NotEnoughChunks
| IP6TooManyChunks
| IP6UnrelevantShortRepresentation
-- | `ipv6_chunk` parses just a group of hexadecimal characters.
-- | Return an error (TooManyHexaDecimalCharacters) in case the group has more than 4 characters.
-- | Return an error (IP6TooManyHexaDecimalCharacters) in case the group has more than 4 characters.
ipv6_chunk :: Parser IPv6Error String
ipv6_chunk = do pos <- current_position
hexachars <- many1 hexdig
if A.length hexachars > 4
then Parser \_ -> failureError pos (Just TooManyHexaDecimalCharacters)
then Parser \_ -> failureError pos (Just IP6TooManyHexaDecimalCharacters)
else pure $ CU.fromCharArray hexachars
-- | `ipv6_chunk'` is `ipv6_chunk` with a following ':' character.
@ -52,9 +51,9 @@ ipv6_full = do chunks <- many1 ipv6_chunk'
pos <- current_position
lastchunk <- ipv6_chunk
case compare (A.length chunks) 7 of
LT -> Parser \_ -> failureError pos (Just NotEnoughChunks)
LT -> Parser \_ -> failureError pos (Just IP6NotEnoughChunks)
EQ -> pure $ A.fold (A.intersperse ":" (chunks <> [lastchunk]))
GT -> Parser \_ -> failureError pos (Just TooManyChunks)
GT -> Parser \_ -> failureError pos (Just IP6TooManyChunks)
-- | `ipv6_shortened` parses a shortened representation of an IPv6 address.
ipv6_shortened :: Parser IPv6Error String
@ -73,7 +72,7 @@ ipv6_shortened =
let nb_zero_filling = 8 - (A.length chunks_part1 + A.length chunks_part2)
filling = repeat nb_zero_filling "0000"
if nb_zero_filling < 1
then Parser \_ -> failureError pos (Just IPv6UnrelevantShortRepresentation)
then Parser \_ -> failureError pos (Just IP6UnrelevantShortRepresentation)
else pure $ A.fold (A.intersperse ":" $ A.concat [chunks_part1, filling, chunks_part2])
-- | TODO: accept IPv6 addresses between brackets ([ipv6]).
@ -81,15 +80,15 @@ ipv6 :: Parser IPv6Error String
ipv6 = ipv6_shortened <|> ipv6_full
data IPv4Error
= NumberTooBig Int
| IPv4UnrelevantShortRepresentation
= IP4NumberTooBig Int
| IP4UnrelevantShortRepresentation
-- | `ipv4_byte` a parser for 0 to 255 natural integers, which is part of the representation of an IPv4 address.
ipv4_byte :: Parser IPv4Error Int
ipv4_byte = do pos <- current_position
number <- nat
if number > 255
then Parser \_ -> failureError pos ((Just <<< NumberTooBig) number)
then Parser \_ -> failureError pos ((Just <<< IP4NumberTooBig) number)
else pure number
-- | `ipv4_byte'` is `ipv4_byte` with a leading '.'.
@ -123,7 +122,7 @@ ipv4_shortened =
nb_zero_filling = 4 - (A.length chunks_part1 + A.length chunks_part2)
filling = A.fold (A.intersperse "." $ repeat nb_zero_filling "0")
if nb_zero_filling < 1
then Parser \_ -> failureError pos (Just IPv4UnrelevantShortRepresentation)
then Parser \_ -> failureError pos (Just IP4UnrelevantShortRepresentation)
else pure $ A.fold (A.intersperse "." [part1, filling, part2])
ipv4 :: Parser IPv4Error String