Skip to content

Commit

Permalink
Rename URL to Url and IPv6 to Ipv6, per Rust naming convention.
Browse files Browse the repository at this point in the history
I find it ugly, but meh.
  • Loading branch information
SimonSapin committed Feb 4, 2014
1 parent affce41 commit 7592e78
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
44 changes: 22 additions & 22 deletions parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use encoding::EncodingRef;
use encoding::all::UTF_8;

use super::{
ParseResult, URL, RelativeSchemeData, OtherSchemeData,
SchemeRelativeURL, UserInfo, Host, Domain,
ParseResult, Url, RelativeSchemeData, OtherSchemeData,
SchemeRelativeUrl, UserInfo, Host, Domain,
utf8_percent_encode, percent_encode_byte,
SimpleEncodeSet, DefaultEncodeSet, UserInfoEncodeSet};

Expand All @@ -40,7 +40,7 @@ fn parse_error(_message: &str) {
}


pub fn parse_url(input: &str, base_url: Option<&URL>) -> ParseResult<URL> {
pub fn parse_url(input: &str, base_url: Option<&Url>) -> ParseResult<Url> {
let input = input.trim_chars(& &[' ', '\t', '\n', '\r', '\x0C']);
let (scheme_result, remaining) = parse_scheme(input);
match scheme_result {
Expand All @@ -52,9 +52,9 @@ pub fn parse_url(input: &str, base_url: Option<&URL>) -> ParseResult<URL> {
parse_error("Relative URL with a scheme");
parse_relative_url(scheme, remaining, base)
},
_ => parse_relative_url(scheme, remaining, &URL {
_ => parse_relative_url(scheme, remaining, &Url {
scheme: ~"", query: None, fragment: None,
scheme_data: RelativeSchemeData(SchemeRelativeURL {
scheme_data: RelativeSchemeData(SchemeRelativeUrl {
userinfo: None, host: Domain(~[]), port: ~"", path: ~[]
})
}),
Expand All @@ -76,7 +76,7 @@ pub fn parse_url(input: &str, base_url: Option<&URL>) -> ParseResult<URL> {
// Scheme data state
let (scheme_data, remaining) = parse_scheme_data(remaining);
let (query, fragment) = parse_query_and_fragment(remaining);
Ok(URL { scheme: scheme, scheme_data: OtherSchemeData(scheme_data),
Ok(Url { scheme: scheme, scheme_data: OtherSchemeData(scheme_data),
query: query, fragment: fragment })
}
},
Expand Down Expand Up @@ -109,7 +109,7 @@ fn parse_scheme<'a>(input: &'a str) -> (Option<~str>, &'a str) {
}


fn parse_absolute_url<'a>(scheme: ~str, input: &'a str) -> ParseResult<URL> {
fn parse_absolute_url<'a>(scheme: ~str, input: &'a str) -> ParseResult<Url> {
// Authority first slash state
let remaining = skip_slashes(input);
// Authority state
Expand All @@ -123,17 +123,17 @@ fn parse_absolute_url<'a>(scheme: ~str, input: &'a str) -> ParseResult<URL> {
remaining,
/* full_url= */ true,
/* in_file_scheme= */ false);
let scheme_data = RelativeSchemeData(SchemeRelativeURL { userinfo: userinfo, host: host, port: port, path: path });
let scheme_data = RelativeSchemeData(SchemeRelativeUrl { userinfo: userinfo, host: host, port: port, path: path });
let (query, fragment) = parse_query_and_fragment(remaining);
Ok(URL { scheme: scheme, scheme_data: scheme_data, query: query, fragment: fragment })
Ok(Url { scheme: scheme, scheme_data: scheme_data, query: query, fragment: fragment })
}


fn parse_relative_url<'a>(scheme: ~str, input: &'a str, base: &URL) -> ParseResult<URL> {
fn parse_relative_url<'a>(scheme: ~str, input: &'a str, base: &Url) -> ParseResult<Url> {
match base.scheme_data {
OtherSchemeData(_) => Err("Relative URL with a non-relative-scheme base"),
RelativeSchemeData(ref base_scheme_data) => if input.is_empty() {
Ok(URL { scheme: scheme, scheme_data: base.scheme_data.clone(),
Ok(Url { scheme: scheme, scheme_data: base.scheme_data.clone(),
query: base.query.clone(), fragment: None })
} else {
let in_file_scheme = scheme.as_slice() == "file";
Expand All @@ -160,10 +160,10 @@ fn parse_relative_url<'a>(scheme: ~str, input: &'a str, base: &URL) -> ParseResu
};
let (path, remaining) = parse_path_start(
remaining, /* full_url= */ true, in_file_scheme);
let scheme_data = RelativeSchemeData(SchemeRelativeURL {
let scheme_data = RelativeSchemeData(SchemeRelativeUrl {
userinfo: None, host: host, port: ~"", path: path });
let (query, fragment) = parse_query_and_fragment(remaining);
Ok(URL { scheme: scheme, scheme_data: scheme_data,
Ok(Url { scheme: scheme, scheme_data: scheme_data,
query: query, fragment: fragment })
} else {
parse_absolute_url(scheme, input)
Expand All @@ -173,29 +173,29 @@ fn parse_relative_url<'a>(scheme: ~str, input: &'a str, base: &URL) -> ParseResu
let (path, remaining) = parse_path(
~[], input.slice_from(1), /* full_url= */ true, in_file_scheme);
let scheme_data = RelativeSchemeData(if in_file_scheme {
SchemeRelativeURL {
SchemeRelativeUrl {
userinfo: None, host: Domain(~[]), port: ~"", path: path
}
} else {
SchemeRelativeURL {
SchemeRelativeUrl {
userinfo: base_scheme_data.userinfo.clone(),
host: base_scheme_data.host.clone(),
port: base_scheme_data.port.clone(),
path: path
}
});
let (query, fragment) = parse_query_and_fragment(remaining);
Ok(URL { scheme: scheme, scheme_data: scheme_data,
Ok(Url { scheme: scheme, scheme_data: scheme_data,
query: query, fragment: fragment })
}
},
'?' => {
let (query, fragment) = parse_query_and_fragment(input);
Ok(URL { scheme: scheme, scheme_data: base.scheme_data.clone(),
Ok(Url { scheme: scheme, scheme_data: base.scheme_data.clone(),
query: query, fragment: fragment })
},
'#' => {
Ok(URL { scheme: scheme, scheme_data: base.scheme_data.clone(),
Ok(Url { scheme: scheme, scheme_data: base.scheme_data.clone(),
query: base.query.clone(),
fragment: Some(parse_fragment(input.slice_from(1))) })
}
Expand All @@ -210,7 +210,7 @@ fn parse_relative_url<'a>(scheme: ~str, input: &'a str, base: &URL) -> ParseResu
// Windows drive letter quirk
let (path, remaining) = parse_path(
~[], input, /* full_url= */ true, in_file_scheme);
(RelativeSchemeData(SchemeRelativeURL {
(RelativeSchemeData(SchemeRelativeUrl {
userinfo: None,
host: Domain(~[]),
port: ~"",
Expand All @@ -222,15 +222,15 @@ fn parse_relative_url<'a>(scheme: ~str, input: &'a str, base: &URL) -> ParseResu
// Relative path state
let (path, remaining) = parse_path(
initial_path, input, /* full_url= */ true, in_file_scheme);
(RelativeSchemeData(SchemeRelativeURL {
(RelativeSchemeData(SchemeRelativeUrl {
userinfo: base_scheme_data.userinfo.clone(),
host: base_scheme_data.host.clone(),
port: base_scheme_data.port.clone(),
path: path
}), remaining)
};
let (query, fragment) = parse_query_and_fragment(remaining);
Ok(URL { scheme: scheme, scheme_data: scheme_data,
Ok(Url { scheme: scheme, scheme_data: scheme_data,
query: query, fragment: fragment })
}
}
Expand Down Expand Up @@ -557,7 +557,7 @@ fn parse_query_and_fragment(input: &str) -> (Option<~str>, Option<~str>) {
'?' => {
let (query, remaining) = parse_query(
input.slice_from(1),
UTF_8 as EncodingRef,
UTF_8 as EncodingRef, // TODO
/* full_url = */ true);
(Some(query), remaining.map(parse_fragment))
},
Expand Down
10 changes: 5 additions & 5 deletions tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use std::char;
use std::u32;
use super::{URL, RelativeSchemeData, SchemeRelativeURL, UserInfo, OtherSchemeData};
use super::{Url, RelativeSchemeData, SchemeRelativeUrl, UserInfo, OtherSchemeData};


#[test]
Expand All @@ -27,23 +27,23 @@ fn test_url_parsing() {
query: expected_query,
fragment: expected_fragment
} = test;
let base = match URL::parse(base, None) {
let base = match Url::parse(base, None) {
Ok(base) => base,
Err(message) => fail!("Error parsing base {:?}: {}", base, message)
};
let url = URL::parse(input, Some(&base));
let url = Url::parse(input, Some(&base));
if expected_scheme.is_none() {
assert!(url.is_err(), "Expected a parse error for URL {:?}", input);
continue
}
let URL { scheme, scheme_data, query, fragment } = match url {
let Url { scheme, scheme_data, query, fragment } = match url {
Ok(url) => url,
Err(message) => fail!("Error parsing URL {:?}: {}", input, message)
};

assert_eq!(Some(scheme), expected_scheme);
match scheme_data {
RelativeSchemeData(SchemeRelativeURL { userinfo, host, port, path }) => {
RelativeSchemeData(SchemeRelativeUrl { userinfo, host, port, path }) => {
let (username, password) = match userinfo {
None => (~"", None),
Some(UserInfo { username, password }) => (username, password),
Expand Down
34 changes: 17 additions & 17 deletions url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod tests;


#[deriving(Clone)]
pub struct URL {
pub struct Url {
scheme: ~str,
scheme_data: SchemeData,
query: Option<~str>, // See form_urlencoded::parse_str() to get name/value pairs.
Expand All @@ -41,12 +41,12 @@ pub struct URL {

#[deriving(Clone)]
pub enum SchemeData {
RelativeSchemeData(SchemeRelativeURL),
RelativeSchemeData(SchemeRelativeUrl),
OtherSchemeData(~str), // data: URLs, mailto: URLs, etc.
}

#[deriving(Clone)]
pub struct SchemeRelativeURL {
pub struct SchemeRelativeUrl {
userinfo: Option<UserInfo>,
host: Host,
port: ~str,
Expand All @@ -62,16 +62,16 @@ pub struct UserInfo {
#[deriving(Clone)]
pub enum Host {
Domain(~[~str]), // Can only be empty in the file scheme
IPv6(IPv6Address)
Ipv6(Ipv6Address)
}

pub struct IPv6Address {
pub struct Ipv6Address {
pieces: [u16, ..8]
}

impl Clone for IPv6Address {
fn clone(&self) -> IPv6Address {
IPv6Address { pieces: self.pieces }
impl Clone for Ipv6Address {
fn clone(&self) -> Ipv6Address {
Ipv6Address { pieces: self.pieces }
}
}

Expand All @@ -86,8 +86,8 @@ macro_rules! is_match(
pub type ParseResult<T> = Result<T, &'static str>;


impl URL {
pub fn parse(input: &str, base_url: Option<&URL>) -> ParseResult<URL> {
impl Url {
pub fn parse(input: &str, base_url: Option<&Url>) -> ParseResult<Url> {
parser::parse_url(input, base_url)
}

Expand All @@ -107,7 +107,7 @@ impl URL {
let mut result = self.scheme.to_owned();
result.push_str(":");
match self.scheme_data {
RelativeSchemeData(SchemeRelativeURL {
RelativeSchemeData(SchemeRelativeUrl {
ref userinfo, ref host, ref port, ref path
}) => {
result.push_str("//");
Expand Down Expand Up @@ -160,9 +160,9 @@ impl Host {
Err("Empty host")
} else if input[0] == '[' as u8 {
if input[input.len() - 1] == ']' as u8 {
IPv6Address::parse(input.slice(1, input.len() - 1)).map(IPv6)
Ipv6Address::parse(input.slice(1, input.len() - 1)).map(Ipv6)
} else {
Err("Invalid IPv6 address")
Err("Invalid Ipv6 address")
}
} else {
let mut percent_encoded = ~"";
Expand All @@ -186,7 +186,7 @@ impl Host {
pub fn serialize(&self) -> ~str {
match *self {
Domain(ref labels) => labels.connect("."),
IPv6(ref address) => {
Ipv6(ref address) => {
let mut result = ~"[";
result.push_str(address.serialize());
result.push_str("]");
Expand All @@ -197,8 +197,8 @@ impl Host {
}


impl IPv6Address {
pub fn parse(input: &str) -> ParseResult<IPv6Address> {
impl Ipv6Address {
pub fn parse(input: &str) -> ParseResult<Ipv6Address> {
let len = input.len();
let mut is_ip_v4 = false;
let mut pieces = [0, 0, 0, 0, 0, 0, 0, 0];
Expand Down Expand Up @@ -311,7 +311,7 @@ impl IPv6Address {
return Err("Invalid IPv6 address")
}
}
Ok(IPv6Address { pieces: pieces })
Ok(Ipv6Address { pieces: pieces })
}

pub fn serialize(&self) -> ~str {
Expand Down

0 comments on commit 7592e78

Please sign in to comment.