Skip to content

Commit 856a820

Browse files
authored
Merge pull request redis-rs#91 from spk/update-rust-url
Update rust-url to 1.1 and add test for parse_redis_url
2 parents 5f80034 + 9be88aa commit 856a820

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ readme = "README.md"
1717

1818
[dependencies]
1919
sha1 = "0.1.1"
20-
url = "0.5.4"
20+
url = "1.1"
2121
rustc-serialize = "0.3.16"
2222
unix_socket = { version ="0.5.0", optional = true }

src/connection.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,19 @@ use unix_socket::UnixStream;
2020

2121
static DEFAULT_PORT: u16 = 6379;
2222

23-
fn redis_scheme_type_mapper(scheme: &str) -> url::SchemeType {
24-
match scheme {
25-
"redis" => url::SchemeType::Relative(DEFAULT_PORT),
26-
"unix" => url::SchemeType::FileLike,
27-
_ => url::SchemeType::NonRelative,
28-
}
29-
}
30-
3123
/// This function takes a redis URL string and parses it into a URL
3224
/// as used by rust-url. This is necessary as the default parser does
3325
/// not understand how redis URLs function.
34-
pub fn parse_redis_url(input: &str) -> url::ParseResult<url::Url> {
35-
let mut parser = url::UrlParser::new();
36-
parser.scheme_type_mapper(redis_scheme_type_mapper);
37-
match parser.parse(input) {
26+
pub fn parse_redis_url(input: &str) -> Result<url::Url, ()> {
27+
match url::Url::parse(input) {
3828
Ok(result) => {
39-
if result.scheme == "redis" || result.scheme == "unix" {
29+
if result.scheme() == "redis" || result.scheme() == "unix" {
4030
Ok(result)
4131
} else {
42-
Err(url::ParseError::InvalidScheme)
32+
Err(())
4333
}
4434
},
45-
Err(err) => Err(err),
35+
Err(_) => Err(()),
4636
}
4737
}
4838

@@ -97,12 +87,13 @@ impl<'a> IntoConnectionInfo for &'a str {
9787
fn url_to_tcp_connection_info(url: url::Url) -> RedisResult<ConnectionInfo> {
9888
Ok(ConnectionInfo {
9989
addr: Box::new(ConnectionAddr::Tcp(
100-
unwrap_or!(url.serialize_host(),
101-
fail!((ErrorKind::InvalidClientConfig, "Missing hostname"))),
90+
match url.host() {
91+
Some(host) => host.to_string(),
92+
None => fail!((ErrorKind::InvalidClientConfig, "Missing hostname")),
93+
},
10294
url.port().unwrap_or(DEFAULT_PORT)
10395
)),
104-
db: match url.serialize_path().unwrap_or("".to_string())
105-
.trim_matches('/') {
96+
db: match url.path().trim_matches('/') {
10697
"" => 0,
10798
path => unwrap_or!(path.parse::<i64>().ok(),
10899
fail!((ErrorKind::InvalidClientConfig, "Invalid database number"))),
@@ -118,8 +109,7 @@ fn url_to_unix_connection_info(url: url::Url) -> RedisResult<ConnectionInfo> {
118109
unwrap_or!(url.to_file_path().ok(),
119110
fail!((ErrorKind::InvalidClientConfig, "Missing path"))),
120111
)),
121-
db: match url.query_pairs().unwrap_or(vec![])
122-
.into_iter().filter(|&(ref key, _)| key == "db").next() {
112+
db: match url.query_pairs().into_iter().filter(|&(ref key, _)| key == "db").next() {
123113
Some((_, db)) => unwrap_or!(db.parse::<i64>().ok(),
124114
fail!((ErrorKind::InvalidClientConfig, "Invalid database number"))),
125115
None => 0,
@@ -136,9 +126,9 @@ fn url_to_unix_connection_info(_: url::Url) -> RedisResult<ConnectionInfo> {
136126

137127
impl IntoConnectionInfo for url::Url {
138128
fn into_connection_info(self) -> RedisResult<ConnectionInfo> {
139-
if self.scheme == "redis" {
129+
if self.scheme() == "redis" {
140130
url_to_tcp_connection_info(self)
141-
} else if self.scheme == "unix" {
131+
} else if self.scheme() == "unix" {
142132
url_to_unix_connection_info(self)
143133
} else {
144134
fail!((ErrorKind::InvalidClientConfig, "URL provided is not a redis URL"));

tests/test_basic.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ impl TestContext {
110110
}
111111

112112

113+
#[test]
114+
fn test_parse_redis_url() {
115+
let redis_url = format!("redis://127.0.0.1:{}/0", SERVER_PORT);
116+
match redis::parse_redis_url(&redis_url) {
117+
Ok(_) => assert!(true),
118+
Err(_) => assert!(false),
119+
}
120+
match redis::parse_redis_url("unix:/var/run/redis/redis.sock") {
121+
Ok(_) => assert!(true),
122+
Err(_) => assert!(false),
123+
}
124+
match redis::parse_redis_url("127.0.0.1") {
125+
Ok(_) => assert!(false),
126+
Err(_) => assert!(true),
127+
}
128+
}
129+
113130
#[test]
114131
fn test_args() {
115132
let ctx = TestContext::new();

0 commit comments

Comments
 (0)