Skip to content

Commit

Permalink
Move code to get latest unyanked version into fn
Browse files Browse the repository at this point in the history
cf. #100
  • Loading branch information
killercup committed Jan 2, 2017
1 parent b41dd60 commit 991892f
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/bin/add/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ pub fn get_latest_version(crate_name: &str) -> Result<String, FetchVersionError>
let crate_data = try!(fetch_cratesio(&format!("/crates/{}/versions", crate_name)));
let crate_json = try!(Json::from_str(&crate_data));

read_latest_version(crate_json)
}

/// Read latest version from JSON structure
///
/// Assumes the version are sorted so that the first non-yanked version is the
/// latest, and thus the one we want.
fn read_latest_version(crate_json: Json) -> Result<String, FetchVersionError> {
crate_json.as_object()
.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(Json::as_array)
.and_then(|vs| vs.iter()
.filter_map(Json::as_object)
.find(|&v| !v.get("yanked").and_then(Json::as_boolean).unwrap_or(true))
)
.and_then(|v| v.get("num"))
.and_then(|v| v.as_string())
.map(|v| v.to_owned())
.and_then(Json::as_string)
.map(str::to_owned)
.ok_or(FetchVersionError::GetVersion)
}

Expand Down

0 comments on commit 991892f

Please sign in to comment.