Skip to content

Commit 5c12868

Browse files
committed
Migrate from liburl to rust-url
The standard url library is soon-to-be deprecated, and now that we're bootstrapping it's trivial to move over to rust-url. Closes #204
1 parent 4a69ffa commit 5c12868

File tree

9 files changed

+57
-36
lines changed

9 files changed

+57
-36
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ rev = "a3c7f2c3"
2727
git = "https://github.com/carllerche/hamcrest-rust.git"
2828
rev = "05acf768"
2929

30+
[dependencies.url]
31+
git = "https://github.com/servo/rust-url"
32+
rev = "98a28e85"
33+
3034
[[bin]]
3135
name = "cargo"
3236
test = false

src/bin/cargo-git-checkout.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use cargo::{execute_main_without_stdin};
1212
use cargo::core::MultiShell;
1313
use cargo::core::source::{Source, SourceId};
1414
use cargo::sources::git::{GitSource};
15-
use cargo::util::{Config, CliResult, CliError, Require, human};
15+
use cargo::util::{Config, CliResult, CliError, human};
1616
use url::Url;
1717

1818
docopt!(Options, "
@@ -31,9 +31,10 @@ fn main() {
3131
fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
3232
let Options { flag_url: url, flag_reference: reference, .. } = options;
3333

34-
let url: Url = try!(from_str(url.as_slice())
35-
.require(|| human(format!("The URL `{}` you passed was \
36-
not a valid URL", url)))
34+
let url: Url = try!(Url::parse(url.as_slice()).map_err(|e| {
35+
human(format!("The URL `{}` you passed was \
36+
not a valid URL: {}", url, e))
37+
})
3738
.map_err(|e| CliError::from_boxed(e, 1)));
3839

3940
let source_id = SourceId::for_git(&url, reference.as_slice());

src/cargo/core/package_id.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use semver;
2-
use url::Url;
32
use std::hash::Hash;
43
use std::fmt;
54
use std::fmt::{Show,Formatter};
@@ -33,28 +32,6 @@ impl<'a> ToVersion for &'a str {
3332
}
3433
}
3534

36-
trait ToUrl {
37-
fn to_url(self) -> Result<Url, String>;
38-
}
39-
40-
impl<'a> ToUrl for &'a str {
41-
fn to_url(self) -> Result<Url, String> {
42-
Url::parse(self)
43-
}
44-
}
45-
46-
impl ToUrl for Url {
47-
fn to_url(self) -> Result<Url, String> {
48-
Ok(self)
49-
}
50-
}
51-
52-
impl<'a> ToUrl for &'a Url {
53-
fn to_url(self) -> Result<Url, String> {
54-
Ok(self.clone())
55-
}
56-
}
57-
5835
#[deriving(Clone, PartialEq)]
5936
pub struct PackageId {
6037
name: String,

src/cargo/core/resolver.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ mod test {
135135

136136
use core::source::{SourceId, RegistryKind, GitKind, Location, Remote};
137137
use core::{Dependency, PackageId, Summary, Registry};
138-
use util::CargoResult;
138+
use util::{CargoResult, ToUrl};
139139

140140
fn resolve<R: Registry>(pkg: &PackageId, deps: &[Dependency], registry: &mut R)
141141
-> CargoResult<Vec<PackageId>> {
@@ -148,7 +148,7 @@ mod test {
148148

149149
impl ToDep for &'static str {
150150
fn to_dep(self) -> Dependency {
151-
let url = from_str("http://example.com").unwrap();
151+
let url = "http://example.com".to_url().unwrap();
152152
let source_id = SourceId::new(RegistryKind, Remote(url));
153153
Dependency::parse(self, Some("1.0.0"), &source_id).unwrap()
154154
}
@@ -200,13 +200,13 @@ mod test {
200200
}
201201

202202
fn dep(name: &str) -> Dependency {
203-
let url = from_str("http://example.com").unwrap();
203+
let url = "http://example.com".to_url().unwrap();
204204
let source_id = SourceId::new(RegistryKind, Remote(url));
205205
Dependency::parse(name, Some("1.0.0"), &source_id).unwrap()
206206
}
207207

208208
fn dep_loc(name: &str, location: &str) -> Dependency {
209-
let url = from_str(location).unwrap();
209+
let url = location.to_url().unwrap();
210210
let source_id = SourceId::new(GitKind("master".to_string()), Remote(url));
211211
Dependency::parse(name, Some("1.0.0"), &source_id).unwrap()
212212
}

src/cargo/core/source.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,14 @@ impl Source for SourceSet {
330330
#[cfg(test)]
331331
mod tests {
332332
use super::{SourceId, Remote, GitKind};
333+
use util::ToUrl;
333334

334335
#[test]
335336
fn github_sources_equal() {
336-
let loc = Remote(from_str("https://github.com/foo/bar").unwrap());
337+
let loc = Remote("https://github.com/foo/bar".to_url().unwrap());
337338
let s1 = SourceId::new(GitKind("master".to_string()), loc);
338339

339-
let loc = Remote(from_str("git://github.com/foo/bar").unwrap());
340+
let loc = Remote("git://github.com/foo/bar".to_url().unwrap());
340341
let mut s2 = SourceId::new(GitKind("master".to_string()), loc);
341342

342343
assert_eq!(s1, s2);

src/cargo/sources/git/source.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ fn ident(location: &Location) -> String {
6868
str::from_utf8(last).unwrap().to_string()
6969
}
7070
Remote(ref url) => {
71-
let path = canonicalize_url(url.path.path.as_slice());
71+
let path = url.path().unwrap().connect("/");
72+
let path = canonicalize_url(path.as_slice());
7273
path.as_slice().split('/').last().unwrap().to_string()
7374
}
7475
};
@@ -184,6 +185,7 @@ mod test {
184185
use url::Url;
185186
use core::source::Remote;
186187
use super::ident;
188+
use util::ToUrl;
187189

188190
#[test]
189191
pub fn test_url_to_path_ident_with_path() {
@@ -226,6 +228,6 @@ mod test {
226228
}
227229

228230
fn url(s: &str) -> Url {
229-
from_str(s).unwrap()
231+
s.to_url().unwrap()
230232
}
231233
}

src/cargo/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub use self::hex::{to_hex, short_hash};
99
pub use self::pool::TaskPool;
1010
pub use self::dependency_queue::{DependencyQueue, Fresh, Dirty, Freshness};
1111
pub use self::graph::Graph;
12+
pub use self::to_url::ToUrl;
1213

1314
pub mod graph;
1415
pub mod process_builder;
@@ -21,3 +22,4 @@ pub mod errors;
2122
pub mod hex;
2223
mod pool;
2324
mod dependency_queue;
25+
mod to_url;

src/cargo/util/to_url.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use url;
2+
use url::{Url, UrlParser};
3+
4+
pub trait ToUrl {
5+
fn to_url(self) -> Result<Url, String>;
6+
}
7+
8+
impl ToUrl for Url {
9+
fn to_url(self) -> Result<Url, String> {
10+
Ok(self)
11+
}
12+
}
13+
14+
impl<'a> ToUrl for &'a Url {
15+
fn to_url(self) -> Result<Url, String> {
16+
Ok(self.clone())
17+
}
18+
}
19+
20+
impl<'a> ToUrl for &'a str {
21+
fn to_url(self) -> Result<Url, String> {
22+
UrlParser::new().scheme_type_mapper(mapper).parse(self).map_err(|s| {
23+
s.to_string()
24+
})
25+
}
26+
}
27+
28+
fn mapper(s: &str) -> url::SchemeType {
29+
match s {
30+
"git" => url::RelativeScheme("9418"),
31+
"ssh" => url::RelativeScheme("22"),
32+
s => url::whatwg_scheme_type_mapper(s),
33+
}
34+
}

tests/test_cargo_compile_git_deps.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ test!(cargo_compile_with_short_ssh_git {
456456
execs()
457457
.with_stdout("")
458458
.with_stderr(format!("Cargo.toml is not a valid manifest\n\n\
459-
invalid url `{}`: `url: Invalid character in scheme.\n", url)));
459+
invalid url `{}`: `Relative URL without a base\n", url)));
460460
})
461461

462462
test!(two_revs_same_deps {

0 commit comments

Comments
 (0)