std: add try_parse_addr and change an alt w/ ip_addr::ipv6 to avoid warning
This commit is contained in:
parent
bb88f772a4
commit
7de1a68217
1 changed files with 30 additions and 6 deletions
|
@ -5,10 +5,9 @@ Types/fns concerning Internet Protocol (IP), versions 4 & 6
|
||||||
import vec;
|
import vec;
|
||||||
import uint;
|
import uint;
|
||||||
|
|
||||||
export ip_addr;
|
export ip_addr, parse_addr_err;
|
||||||
export format_addr;
|
export format_addr;
|
||||||
export v4;
|
export v4;
|
||||||
//format_addr, parse_addr;
|
|
||||||
|
|
||||||
#[doc = "An IP address"]
|
#[doc = "An IP address"]
|
||||||
enum ip_addr {
|
enum ip_addr {
|
||||||
|
@ -17,6 +16,13 @@ enum ip_addr {
|
||||||
ipv6(u16,u16,u16,u16,u16,u16,u16,u16)
|
ipv6(u16,u16,u16,u16,u16,u16,u16,u16)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc="
|
||||||
|
Human-friendly feedback on why a parse_addr attempt failed
|
||||||
|
"]
|
||||||
|
type parse_addr_err = {
|
||||||
|
err_msg: str
|
||||||
|
};
|
||||||
|
|
||||||
#[doc="
|
#[doc="
|
||||||
Convert a `ip_addr` to a str
|
Convert a `ip_addr` to a str
|
||||||
|
|
||||||
|
@ -29,7 +35,7 @@ fn format_addr(ip: ip_addr) -> str {
|
||||||
ipv4(a, b, c, d) {
|
ipv4(a, b, c, d) {
|
||||||
#fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint]
|
#fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint]
|
||||||
}
|
}
|
||||||
ipv6(a,b,c,d,e,f,g,h) {
|
ipv6(_, _, _, _, _, _, _, _) {
|
||||||
fail "FIXME impl parsing of ipv6 addr";
|
fail "FIXME impl parsing of ipv6 addr";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,14 +58,32 @@ j Fails if the string is not a valid IPv4 address
|
||||||
* an `ip_addr` of the `ipv4` variant
|
* an `ip_addr` of the `ipv4` variant
|
||||||
"]
|
"]
|
||||||
fn parse_addr(ip: str) -> ip_addr {
|
fn parse_addr(ip: str) -> ip_addr {
|
||||||
|
alt try_parse_addr(ip) {
|
||||||
|
result::ok(addr) { addr }
|
||||||
|
result::err(err_data) {
|
||||||
|
fail err_data.err_msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn try_parse_addr(ip: str) -> result::result<ip_addr,parse_addr_err> {
|
||||||
let parts = vec::map(str::split_char(ip, '.'), {|s|
|
let parts = vec::map(str::split_char(ip, '.'), {|s|
|
||||||
alt uint::from_str(s) {
|
alt uint::from_str(s) {
|
||||||
some(n) if n <= 255u { n }
|
some(n) if n <= 255u { n }
|
||||||
_ { fail "Invalid IP Address part." }
|
_ { 256u }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if vec::len(parts) != 4u { fail "Too many dots in IP address"; }
|
if vec::len(parts) != 4u {
|
||||||
ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8)
|
result::err({err_msg: #fmt("'%s' doesn't have 4 parts",
|
||||||
|
ip)})
|
||||||
|
}
|
||||||
|
else if vec::contains(parts, 256u) {
|
||||||
|
result::err({err_msg: #fmt("invalid octal in provided addr '%s'",
|
||||||
|
ip)})
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result::ok(ipv4(parts[0] as u8, parts[1] as u8,
|
||||||
|
parts[2] as u8, parts[3] as u8))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue