Make the various from_str functions return options

So that they can be used with user input without causing task
failures.

Closes #1335
This commit is contained in:
Marijn Haverbeke
2012-02-22 13:18:15 +01:00
parent 72373438d2
commit ffd50b9cdf
26 changed files with 196 additions and 308 deletions

View File

@@ -49,10 +49,13 @@ Failure:
String must be a valid IPv4 address
*/
fn parse_addr(ip: str) -> ip_addr {
let parts = vec::map(str::split_byte(ip, "."[0]),
{|s| uint::from_str(s) });
let parts = vec::map(str::split_byte(ip, "."[0]), {|s|
alt uint::from_str(s) {
some(n) if n <= 255u { n }
_ { fail "Invalid IP Address part." }
}
});
if vec::len(parts) != 4u { fail "Too many dots in IP address"; }
for i in parts { if i > 255u { fail "Invalid IP Address part."; } }
ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8)
}