|
2 | 2 |
|
3 | 3 |
|
4 | 4 | use super::DocBuilder;
|
5 |
| -use rustc_serialize::json::{Json, Array}; |
6 |
| -use hyper; |
7 |
| -use hyper::header::{Accept, qitem}; |
8 | 5 | use db::connect_db;
|
9 | 6 | use errors::*;
|
| 7 | +use crates_index_diff::{ChangeKind, Index}; |
10 | 8 |
|
11 | 9 |
|
12 | 10 | impl DocBuilder {
|
13 | 11 | /// Updates crates.io-index repository and adds new crates into build queue
|
14 | 12 | pub fn get_new_crates(&mut self) -> Result<()> {
|
15 | 13 | try!(self.load_database_cache());
|
16 |
| - |
17 |
| - let body = { |
18 |
| - use std::io::Read; |
19 |
| - let client = hyper::Client::new(); |
20 |
| - let mut res = try!(client.get("https://crates.io/summary") |
21 |
| - .header(Accept(vec![qitem("application/json".parse().unwrap())])) |
22 |
| - .send()); |
23 |
| - let mut body = String::new(); |
24 |
| - try!(res.read_to_string(&mut body)); |
25 |
| - body |
26 |
| - }; |
27 |
| - |
28 |
| - let json = try!(Json::from_str(&body)); |
29 |
| - |
30 |
| - let crates = { |
31 |
| - let mut crates: Vec<(String, String)> = Vec::new(); |
32 |
| - for section in ["just_updated", "new_crates"].iter() { |
33 |
| - match json.as_object() |
34 |
| - .and_then(|o| o.get(§ion[..])) |
35 |
| - .and_then(|j| j.as_array()) |
36 |
| - .map(get_crates_from_array) { |
37 |
| - Some(mut c) => crates.append(c.as_mut()), |
38 |
| - None => continue, |
39 |
| - } |
40 |
| - } |
41 |
| - crates |
42 |
| - }; |
43 |
| - |
44 | 14 | let conn = try!(connect_db());
|
45 |
| - for (name, version) in crates { |
46 |
| - if self.db_cache.contains(&format!("{}-{}", name, version)[..]) { |
| 15 | + let index = try!(Index::from_path_or_cloned(&self.options.crates_io_index_path)); |
| 16 | + let changes = try!(index.fetch_changes()); |
| 17 | + for krate in changes.iter().filter(|k| k.kind != ChangeKind::Yanked) { |
| 18 | + if self.db_cache.contains(&format!("{}-{}", krate.name, krate.version)) { |
47 | 19 | continue;
|
48 | 20 | }
|
49 |
| - let _ = conn.execute("INSERT INTO queue (name, version) VALUES ($1, $2)", |
50 |
| - &[&name, &version]); |
| 21 | + conn.execute("INSERT INTO queue (name, version) VALUES ($1, $2)", |
| 22 | + &[&krate.name, &krate.version]).ok(); |
51 | 23 | }
|
52 | 24 |
|
53 | 25 | Ok(())
|
@@ -80,33 +52,6 @@ impl DocBuilder {
|
80 | 52 | }
|
81 | 53 | }
|
82 | 54 |
|
83 |
| - |
84 |
| -/// Returns Vec<CRATE_NAME, CRATE_VERSION> from a summary array |
85 |
| -fn get_crates_from_array<'a>(crates: &'a Array) -> Vec<(String, String)> { |
86 |
| - let mut crates_vec: Vec<(String, String)> = Vec::new(); |
87 |
| - for crte in crates { |
88 |
| - let name = match crte.as_object() |
89 |
| - .and_then(|o| o.get("id")) |
90 |
| - .and_then(|i| i.as_string()) |
91 |
| - .map(|s| s.to_owned()) { |
92 |
| - Some(s) => s, |
93 |
| - None => continue, |
94 |
| - }; |
95 |
| - let version = match crte.as_object() |
96 |
| - .and_then(|o| o.get("max_version")) |
97 |
| - .and_then(|v| v.as_string()) |
98 |
| - .map(|s| s.to_owned()) { |
99 |
| - Some(s) => s, |
100 |
| - None => continue, |
101 |
| - }; |
102 |
| - crates_vec.push((name, version)); |
103 |
| - } |
104 |
| - crates_vec |
105 |
| -} |
106 |
| - |
107 |
| - |
108 |
| - |
109 |
| - |
110 | 55 | #[cfg(test)]
|
111 | 56 | mod test {
|
112 | 57 | extern crate env_logger;
|
|
0 commit comments