|
2 | 2 |
|
3 | 3 | use crate::{ |
4 | 4 | db::BuildId, |
| 5 | + storage::CompressionAlgorithm, |
5 | 6 | web::{ |
6 | 7 | MatchedRelease, MetaData, ReqVersion, error::AxumNope, escaped_uri::EscapedURI, |
7 | 8 | extractors::Path, url_decode, |
@@ -573,6 +574,30 @@ impl RustdocParams { |
573 | 574 | EscapedURI::from_path(path) |
574 | 575 | } |
575 | 576 |
|
| 577 | + pub(crate) fn json_download_url( |
| 578 | + &self, |
| 579 | + wanted_compression: Option<CompressionAlgorithm>, |
| 580 | + format_version: Option<&str>, |
| 581 | + ) -> EscapedURI { |
| 582 | + let mut path = format!("/crate/{}/{}", self.name, self.req_version); |
| 583 | + |
| 584 | + if let Some(doc_target) = self.doc_target() { |
| 585 | + path.push_str(&format!("/{doc_target}")); |
| 586 | + } |
| 587 | + |
| 588 | + if let Some(format_version) = format_version { |
| 589 | + path.push_str(&format!("/json/{format_version}")); |
| 590 | + } else { |
| 591 | + path.push_str("/json"); |
| 592 | + } |
| 593 | + |
| 594 | + if let Some(wanted_compression) = wanted_compression { |
| 595 | + path.push_str(&format!(".{}", wanted_compression.file_extension())); |
| 596 | + } |
| 597 | + |
| 598 | + EscapedURI::from_path(path) |
| 599 | + } |
| 600 | + |
576 | 601 | pub(crate) fn features_url(&self) -> EscapedURI { |
577 | 602 | EscapedURI::from_path(format!( |
578 | 603 | "/crate/{}/{}/features", |
@@ -863,7 +888,7 @@ mod tests { |
863 | 888 | use super::*; |
864 | 889 | use crate::{ |
865 | 890 | db::types::version::Version, |
866 | | - test::{AxumResponseTestExt, AxumRouterTestExt}, |
| 891 | + test::{AxumResponseTestExt, AxumRouterTestExt, V1}, |
867 | 892 | }; |
868 | 893 | use axum::{Router, routing::get}; |
869 | 894 | use test_case::test_case; |
@@ -1719,4 +1744,66 @@ mod tests { |
1719 | 1744 | format!("/{KRATE}/0.14.0/{KRATE}/trait.Itertools.html") |
1720 | 1745 | ) |
1721 | 1746 | } |
| 1747 | + |
| 1748 | + #[test_case(None)] |
| 1749 | + #[test_case(Some(CompressionAlgorithm::Gzip))] |
| 1750 | + #[test_case(Some(CompressionAlgorithm::Zstd))] |
| 1751 | + fn test_plain_json_url(wanted_compression: Option<CompressionAlgorithm>) { |
| 1752 | + let mut params = RustdocParams::new(KRATE) |
| 1753 | + .with_page_kind(PageKind::Rustdoc) |
| 1754 | + .with_req_version(ReqVersion::Exact(V1)); |
| 1755 | + |
| 1756 | + assert_eq!( |
| 1757 | + params.json_download_url(wanted_compression, None), |
| 1758 | + format!( |
| 1759 | + "/crate/{KRATE}/{V1}/json{}", |
| 1760 | + wanted_compression |
| 1761 | + .map(|c| format!(".{}", c.file_extension())) |
| 1762 | + .unwrap_or_default() |
| 1763 | + ) |
| 1764 | + ); |
| 1765 | + |
| 1766 | + params = params.with_doc_target("some-target"); |
| 1767 | + |
| 1768 | + assert_eq!( |
| 1769 | + params.json_download_url(wanted_compression, None), |
| 1770 | + format!( |
| 1771 | + "/crate/{KRATE}/{V1}/some-target/json{}", |
| 1772 | + wanted_compression |
| 1773 | + .map(|c| format!(".{}", c.file_extension())) |
| 1774 | + .unwrap_or_default() |
| 1775 | + ) |
| 1776 | + ); |
| 1777 | + } |
| 1778 | + |
| 1779 | + #[test_case(None)] |
| 1780 | + #[test_case(Some(CompressionAlgorithm::Gzip))] |
| 1781 | + #[test_case(Some(CompressionAlgorithm::Zstd))] |
| 1782 | + fn test_plain_json_url_with_format(wanted_compression: Option<CompressionAlgorithm>) { |
| 1783 | + let mut params = RustdocParams::new(KRATE) |
| 1784 | + .with_page_kind(PageKind::Rustdoc) |
| 1785 | + .with_req_version(ReqVersion::Exact(V1)); |
| 1786 | + |
| 1787 | + assert_eq!( |
| 1788 | + params.json_download_url(wanted_compression, Some("42")), |
| 1789 | + format!( |
| 1790 | + "/crate/{KRATE}/{V1}/json/42{}", |
| 1791 | + wanted_compression |
| 1792 | + .map(|c| format!(".{}", c.file_extension())) |
| 1793 | + .unwrap_or_default() |
| 1794 | + ) |
| 1795 | + ); |
| 1796 | + |
| 1797 | + params = params.with_doc_target("some-target"); |
| 1798 | + |
| 1799 | + assert_eq!( |
| 1800 | + params.json_download_url(wanted_compression, Some("42")), |
| 1801 | + format!( |
| 1802 | + "/crate/{KRATE}/{V1}/some-target/json/42{}", |
| 1803 | + wanted_compression |
| 1804 | + .map(|c| format!(".{}", c.file_extension())) |
| 1805 | + .unwrap_or_default() |
| 1806 | + ) |
| 1807 | + ); |
| 1808 | + } |
1722 | 1809 | } |
0 commit comments