Skip to content

use crates.io search to search crates #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/test/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ impl<'a> FakeRelease<'a> {
}
}

pub(crate) fn downloads(mut self, downloads: i32) -> Self {
self.registry_release_data.downloads = downloads;
self
}

pub(crate) fn description(mut self, new: impl Into<String>) -> Self {
self.package.description = Some(new.into());
self
Expand Down
6 changes: 6 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ mod queue_builder;
mod rustc_version;
pub(crate) mod sized_buffer;

pub(crate) const APP_USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
" ",
include_str!(concat!(env!("OUT_DIR"), "/git_version"))
);

pub(crate) fn report_error(err: &anyhow::Error) {
if std::env::var("SENTRY_DSN").is_ok() {
sentry_anyhow::capture_anyhow(err);
Expand Down
29 changes: 19 additions & 10 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ struct MatchVersion {
/// dashes (`-`) replaced with underscores (`_`) and vice versa.
pub corrected_name: Option<String>,
pub version: MatchSemver,
pub rustdoc_status: bool,
}

impl MatchVersion {
Expand Down Expand Up @@ -284,9 +285,12 @@ fn match_version(
.unwrap_or_else(|| "*".into());

let mut corrected_name = None;
let versions: Vec<(String, i32, bool)> = {
let query = "SELECT name, version, releases.id, releases.yanked
FROM releases INNER JOIN crates ON releases.crate_id = crates.id
let versions: Vec<(String, i32, bool, bool)> = {
let query = "
SELECT
name, version, releases.id, releases.yanked, releases.rustdoc_status
FROM releases
INNER JOIN crates ON releases.crate_id = crates.id
WHERE normalize_crate_name(name) = normalize_crate_name($1)";

let rows = conn.query(query, &[&name]).unwrap();
Expand All @@ -300,7 +304,7 @@ fn match_version(
}
};

rows.map(|row| (row.get(1), row.get(2), row.get(3)))
rows.map(|row| (row.get(1), row.get(2), row.get(3), row.get(4)))
.collect()
};

Expand All @@ -309,10 +313,13 @@ fn match_version(
}

// first check for exact match, we can't expect users to use semver in query
if let Some((version, id, _)) = versions.iter().find(|(vers, _, _)| vers == &req_version) {
if let Some((version, id, _yanked, rustdoc_status)) =
versions.iter().find(|(vers, _, _, _)| vers == &req_version)
{
return Ok(MatchVersion {
corrected_name,
version: MatchSemver::Exact((version.to_owned(), *id)),
rustdoc_status: *rustdoc_status,
});
}

Expand All @@ -321,9 +328,9 @@ fn match_version(

// we need to sort versions first
let versions_sem = {
let mut versions_sem: Vec<(Version, i32)> = Vec::with_capacity(versions.len());
let mut versions_sem: Vec<(Version, i32, bool)> = Vec::with_capacity(versions.len());

for version in versions.iter().filter(|(_, _, yanked)| !yanked) {
for version in versions.iter().filter(|(_, _, yanked, _)| !yanked) {
// in theory a crate must always have a semver compatible version,
// but check result just in case
let version_sem = Version::parse(&version.0)
Expand All @@ -337,21 +344,22 @@ fn match_version(
report_error(&err);
Nope::InternalServerError
})?;
versions_sem.push((version_sem, version.1));
versions_sem.push((version_sem, version.1, version.3));
}

versions_sem.sort();
versions_sem.reverse();
versions_sem
};

if let Some((version, id)) = versions_sem
if let Some((version, id, rustdoc_status)) = versions_sem
.iter()
.find(|(vers, _)| req_sem_ver.matches(vers))
.find(|(vers, _, _)| req_sem_ver.matches(vers))
{
return Ok(MatchVersion {
corrected_name,
version: MatchSemver::Semver((version.to_string(), *id)),
rustdoc_status: *rustdoc_status,
});
}

Expand All @@ -363,6 +371,7 @@ fn match_version(
.map(|v| MatchVersion {
corrected_name,
version: MatchSemver::Semver((v.0.to_string(), v.1)),
rustdoc_status: v.2,
})
.ok_or(Nope::VersionNotFound);
}
Expand Down
Loading