Skip to content

Commit

Permalink
Improve fetch algorithm. Ignore yanked when no version is specified. …
Browse files Browse the repository at this point in the history
…Request only versions from server to save bandwith.
  • Loading branch information
sebasgarcep committed Jan 2, 2017
1 parent fa6b748 commit 43fb6ff
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/bin/add/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ pub fn get_latest_version(crate_name: &str) -> Result<String, FetchVersionError>
return Ok("CURRENT_VERSION_TEST".into());
}

let crate_data = try!(fetch_cratesio(&format!("/crates/{}", crate_name)));
let crate_data = try!(fetch_cratesio(&format!("/crates/{}/versions", crate_name)));
let crate_json = try!(Json::from_str(&crate_data));

crate_json.as_object()
.and_then(|c| c.get("crate"))
.and_then(|c| c.as_object())
.and_then(|c| c.get("max_version"))
.and_then(|c| c.get("versions"))
.and_then(|c| c.as_array())
.and_then(|c| {
for version in c {
let version_object = version.as_object();
let yanked = version_object
.and_then(|v| v.get("yanked"))
.and_then(|v| v.as_boolean());
if let Some(false) = yanked {
return version_object;
}
}
return None;
})
.and_then(|v| v.get("num"))
.and_then(|v| v.as_string())
.map(|v| v.to_owned())
.ok_or(FetchVersionError::GetVersion)
Expand Down

0 comments on commit 43fb6ff

Please sign in to comment.