Skip to content

Commit 05b70cf

Browse files
zahidkizmazsyphar
authored andcommitted
feat: set documentation_url to None when url starts with https://docs.rs
Documentation url is mainly used for the external documentation websites. When the url starts with https://docs.rs we don't need to include it on the page.
1 parent 8a866f3 commit 05b70cf

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/test/fakes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ impl<'a> FakeRelease<'a> {
321321
self
322322
}
323323

324+
pub(crate) fn documentation_url(mut self, documentation_url: Option<String>) -> Self {
325+
self.package.documentation = documentation_url;
326+
self
327+
}
328+
324329
pub(crate) fn create(self) -> Result<i32> {
325330
let runtime = self.runtime.clone();
326331
runtime.block_on(self.create_async())

src/web/crate_details.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ impl CrateDetails {
219219
.transpose()?,
220220
};
221221

222+
// When documentation_url points to docs.rs itself, then we don't need to
223+
// show it on the page because user is already on docs.rs website
224+
let documentation_url = match krate.documentation_url {
225+
Some(url) if url.starts_with("https://docs.rs/") => None,
226+
Some(url) => Some(url),
227+
None => None,
228+
};
229+
222230
let mut crate_details = CrateDetails {
223231
name: krate.name,
224232
version: version.clone(),
@@ -241,9 +249,9 @@ impl CrateDetails {
241249
releases: prefetched_releases,
242250
repository_metadata,
243251
metadata,
252+
documentation_url,
244253
is_library: krate.is_library,
245254
license: krate.license,
246-
documentation_url: krate.documentation_url,
247255
documented_items: krate.documented_items,
248256
total_items: krate.total_items,
249257
total_items_needing_examples: krate.total_items_needing_examples,
@@ -892,6 +900,49 @@ mod tests {
892900
Ok(())
893901
}
894902

903+
#[test]
904+
fn test_crate_details_documentation_url_is_none_when_url_is_docs_rs() {
905+
async_wrapper(|env| async move {
906+
let db = env.async_db().await;
907+
let mut conn = db.async_conn().await;
908+
909+
env.async_fake_release()
910+
.await
911+
.name("foo")
912+
.version("0.1.0")
913+
.documentation_url(Some("https://foo.com".into()))
914+
.create_async()
915+
.await?;
916+
env.async_fake_release()
917+
.await
918+
.name("foo")
919+
.version("0.2.0")
920+
.documentation_url(Some("https://docs.rs/foo/".into()))
921+
.create_async()
922+
.await?;
923+
env.async_fake_release()
924+
.await
925+
.name("foo")
926+
.version("0.3.0")
927+
.documentation_url(None)
928+
.create_async()
929+
.await?;
930+
931+
let details_0_1 = crate_details(&mut conn, "foo", "0.1.0", None).await;
932+
let details_0_2 = crate_details(&mut conn, "foo", "0.2.0", None).await;
933+
let details_0_3 = crate_details(&mut conn, "foo", "0.3.0", None).await;
934+
935+
assert_eq!(
936+
details_0_1.documentation_url,
937+
Some("https://foo.com".into())
938+
);
939+
assert_eq!(details_0_2.documentation_url, None);
940+
assert_eq!(details_0_3.documentation_url, None);
941+
942+
Ok(())
943+
});
944+
}
945+
895946
#[test]
896947
fn test_last_successful_build_when_last_releases_failed_or_yanked() {
897948
async_wrapper(|env| async move {

0 commit comments

Comments
 (0)