Skip to content

Commit ba149d8

Browse files
committed
refactor & centralize rustdoc parameter handling & url generation
1 parent b113a25 commit ba149d8

40 files changed

+3134
-1234
lines changed

.sqlx/query-0011936b31678ee644dff3f5f8d8e1276f7f814f22b830c46d6b77edb209a033.json

Lines changed: 0 additions & 28 deletions
This file was deleted.

.sqlx/query-118390f408685404fa25f1de88df56c6f943b5530760163ff8a667fac627626f.json renamed to .sqlx/query-7072759134a3abceaa6d3105b1f7986fd2ec3e76ff4df7c8204f2d4eaebba8f1.json

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bzip2 = "0.6.0"
5959
getrandom = "0.3.1"
6060
itertools = { version = "0.14.0" }
6161
hex = "0.4.3"
62-
derive_more = { version = "2.0.0", features = ["display"] }
62+
derive_more = { version = "2.0.0", features = ["display", "deref"] }
6363
sysinfo = { version = "0.37.2", default-features = false, features = ["system"] }
6464
derive_builder = "0.20.2"
6565

src/db/add_package.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::{
44
error::Result,
55
registry_api::{CrateData, CrateOwner, ReleaseData},
66
storage::CompressionAlgorithm,
7-
utils::{MetadataPackage, rustc_version::parse_rustc_date},
7+
utils::{Dependency, MetadataPackage, rustc_version::parse_rustc_date},
88
web::crate_details::{latest_release, releases_for_crate},
99
};
1010
use anyhow::{Context, anyhow};
11-
use derive_more::Display;
11+
use derive_more::{Deref, Display};
1212
use futures_util::stream::TryStreamExt;
13-
use serde::Serialize;
13+
use serde::{Deserialize, Serialize};
1414
use serde_json::Value;
1515
use slug::slugify;
1616
use std::{
@@ -33,6 +33,44 @@ pub struct ReleaseId(pub i32);
3333
#[sqlx(transparent)]
3434
pub struct BuildId(pub i32);
3535

36+
type DepOut = (String, String, String, bool);
37+
type DepIn = (String, String, Option<String>, Option<bool>);
38+
39+
/// A crate dependency in our internal representation for releases.dependencies json.
40+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Deref)]
41+
#[serde(from = "DepIn", into = "DepOut")]
42+
pub(crate) struct ReleaseDependency(Dependency);
43+
44+
impl ReleaseDependency {
45+
pub(crate) fn into_inner(self) -> Dependency {
46+
self.0
47+
}
48+
}
49+
50+
impl From<DepIn> for ReleaseDependency {
51+
fn from((name, req, kind, optional): DepIn) -> Self {
52+
ReleaseDependency(Dependency {
53+
name,
54+
req,
55+
kind,
56+
optional: optional.unwrap_or(false),
57+
rename: None,
58+
})
59+
}
60+
}
61+
62+
impl From<ReleaseDependency> for DepOut {
63+
fn from(rd: ReleaseDependency) -> Self {
64+
let d = rd.0;
65+
(
66+
d.name,
67+
d.req.to_string(),
68+
d.kind.unwrap_or_else(|| "normal".into()),
69+
d.optional,
70+
)
71+
}
72+
}
73+
3674
/// Adds a package into database.
3775
///
3876
/// Package must be built first.
@@ -59,7 +97,7 @@ pub(crate) async fn finish_release(
5997
source_size: u64,
6098
) -> Result<()> {
6199
debug!("updating release data");
62-
let dependencies = convert_dependencies(metadata_pkg);
100+
let dependencies = convert_dependencies(metadata_pkg)?;
63101
let rustdoc = get_rustdoc(metadata_pkg, source_dir).unwrap_or(None);
64102
let readme = get_readme(metadata_pkg, source_dir).unwrap_or(None);
65103
let features = get_features(metadata_pkg);
@@ -94,7 +132,7 @@ pub(crate) async fn finish_release(
94132
WHERE id = $1"#,
95133
release_id.0,
96134
registry_data.release_time,
97-
serde_json::to_value(dependencies)?,
135+
dependencies,
98136
metadata_pkg.package_name(),
99137
registry_data.yanked,
100138
has_docs,
@@ -393,20 +431,14 @@ pub(crate) async fn initialize_build(
393431
Ok(build_id)
394432
}
395433

396-
/// Convert dependencies into Vec<(String, String, String, bool)>
397-
fn convert_dependencies(pkg: &MetadataPackage) -> Vec<(String, String, String, bool)> {
398-
pkg.dependencies
434+
/// Convert dependencies into our own internal JSON representation
435+
fn convert_dependencies(pkg: &MetadataPackage) -> Result<serde_json::Value> {
436+
let dependencies: Vec<_> = pkg
437+
.dependencies
399438
.iter()
400-
.map(|dependency| {
401-
let name = dependency.name.clone();
402-
let version = dependency.req.clone();
403-
let kind = dependency
404-
.kind
405-
.clone()
406-
.unwrap_or_else(|| "normal".to_string());
407-
(name, version, kind, dependency.optional)
408-
})
409-
.collect()
439+
.map(|dependency| ReleaseDependency(dependency.clone()))
440+
.collect::<Vec<_>>();
441+
Ok(serde_json::to_value(dependencies)?)
410442
}
411443

412444
/// Reads features and converts them to Vec<Feature> with default being first

src/db/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use sqlx::migrate::{Migrate, Migrator};
44

55
pub use self::add_package::update_latest_version_id;
66
pub(crate) use self::add_package::{
7-
add_doc_coverage, finish_build, finish_release, initialize_build, initialize_crate,
8-
initialize_release, update_build_with_error,
7+
ReleaseDependency, add_doc_coverage, finish_build, finish_release, initialize_build,
8+
initialize_crate, initialize_release, update_build_with_error,
99
};
1010
pub use self::{
1111
add_package::{

src/docbuilder/rustwide_builder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,11 +1450,10 @@ mod tests {
14501450
&format!("{default_target}/{crate_path}/index.html"),
14511451
)?);
14521452

1453-
let default_target_url =
1454-
format!("/{crate_}/{version}/{default_target}/{crate_path}/index.html");
1453+
let default_target_url = format!("/{crate_}/{version}/{default_target}/{crate_path}/");
14551454
runtime.block_on(web.assert_redirect(
14561455
&default_target_url,
1457-
&format!("/{crate_}/{version}/{crate_path}/index.html"),
1456+
&format!("/{crate_}/{version}/{crate_path}/"),
14581457
))?;
14591458

14601459
// Non-dist toolchains only have a single target, and of course
@@ -1498,7 +1497,6 @@ mod tests {
14981497
json_files.sort();
14991498
dbg!(&json_files);
15001499
assert!(json_files[0].starts_with(&format!("empty-library_1.0.0_{target}_")));
1501-
15021500
assert!(json_files[0].ends_with(&format!(".json.{ext}")));
15031501
assert_eq!(
15041502
json_files[1],

src/test/fakes.rs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::storage::{
1212
AsyncStorage, CompressionAlgorithm, RustdocJsonFormatVersion, compress, rustdoc_archive_path,
1313
rustdoc_json_path, source_archive_path,
1414
};
15-
use crate::utils::{Dependency, MetadataPackage, Target};
15+
use crate::utils::{Dependency, MetadataPackage, cargo_metadata::Target};
1616
use anyhow::{Context, bail};
1717
use base64::{Engine, engine::general_purpose::STANDARD as b64};
1818
use chrono::{DateTime, Utc};
@@ -330,7 +330,7 @@ impl<'a> FakeRelease<'a> {
330330
}
331331

332332
/// Returns the release_id
333-
pub(crate) async fn create(self) -> Result<ReleaseId> {
333+
pub(crate) async fn create(mut self) -> Result<ReleaseId> {
334334
use std::fs;
335335
use std::path::Path;
336336

@@ -514,37 +514,34 @@ impl<'a> FakeRelease<'a> {
514514
store_files_into(&self.source_files, crate_dir)?;
515515

516516
let default_target = self.default_target.unwrap_or("x86_64-unknown-linux-gnu");
517+
if !self.doc_targets.iter().any(|t| t == default_target) {
518+
self.doc_targets.insert(0, default_target.to_owned());
519+
}
517520

518-
{
519-
let mut targets = self.doc_targets.clone();
520-
if !targets.contains(&default_target.to_owned()) {
521-
targets.push(default_target.to_owned());
522-
}
523-
for target in &targets {
524-
let dummy_rustdoc_json_content = serde_json::to_vec(&serde_json::json!({
525-
"format_version": 42
526-
}))?;
527-
528-
for alg in RUSTDOC_JSON_COMPRESSION_ALGORITHMS {
529-
let compressed_json: Vec<u8> = compress(&*dummy_rustdoc_json_content, *alg)?;
530-
531-
for format_version in [
532-
RustdocJsonFormatVersion::Version(42),
533-
RustdocJsonFormatVersion::Latest,
534-
] {
535-
storage
536-
.store_one_uncompressed(
537-
&rustdoc_json_path(
538-
&package.name,
539-
&package.version,
540-
target,
541-
format_version,
542-
Some(*alg),
543-
),
544-
compressed_json.clone(),
545-
)
546-
.await?;
547-
}
521+
for target in &self.doc_targets {
522+
let dummy_rustdoc_json_content = serde_json::to_vec(&serde_json::json!({
523+
"format_version": 42
524+
}))?;
525+
526+
for alg in RUSTDOC_JSON_COMPRESSION_ALGORITHMS {
527+
let compressed_json: Vec<u8> = compress(&*dummy_rustdoc_json_content, *alg)?;
528+
529+
for format_version in [
530+
RustdocJsonFormatVersion::Version(42),
531+
RustdocJsonFormatVersion::Latest,
532+
] {
533+
storage
534+
.store_one_uncompressed(
535+
&rustdoc_json_path(
536+
&package.name,
537+
&package.version,
538+
target,
539+
format_version,
540+
Some(*alg),
541+
),
542+
compressed_json.clone(),
543+
)
544+
.await?;
548545
}
549546
}
550547
}
@@ -555,6 +552,7 @@ impl<'a> FakeRelease<'a> {
555552
let mut async_conn = db.async_conn().await;
556553
let crate_id = initialize_crate(&mut async_conn, &package.name).await?;
557554
let release_id = initialize_release(&mut async_conn, crate_id, &package.version).await?;
555+
558556
crate::db::finish_release(
559557
&mut async_conn,
560558
crate_id,

src/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl AxumResponseTestExt for axum::response::Response {
7272
expected_directives.to_str().unwrap(),
7373
);
7474
} else {
75-
assert!(cache_control.is_none());
75+
assert!(cache_control.is_none(), "{:?}", cache_control);
7676
}
7777
}
7878

src/utils/cargo_metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl Target {
125125
}
126126
}
127127

128-
#[derive(Debug, Deserialize, Serialize)]
128+
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
129129
pub(crate) struct Dependency {
130130
pub(crate) name: String,
131131
pub(crate) req: String,

src/utils/mod.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
//! Various utilities for docs.rs
22
3-
pub(crate) use self::cargo_metadata::{CargoMetadata, Package as MetadataPackage};
4-
pub(crate) use self::copy::copy_dir_all;
5-
pub use self::daemon::{start_daemon, watch_registry};
6-
pub(crate) use self::html::rewrite_rustdoc_html_stream;
7-
pub use self::queue::{
8-
get_crate_pattern_and_priority, get_crate_priority, list_crate_priorities,
9-
remove_crate_priority, set_crate_priority,
3+
pub(crate) use self::{
4+
cargo_metadata::{CargoMetadata, Dependency, Package as MetadataPackage},
5+
copy::copy_dir_all,
6+
html::rewrite_rustdoc_html_stream,
7+
rustc_version::{get_correct_docsrs_style_file, parse_rustc_version},
8+
};
9+
pub use self::{
10+
daemon::{start_daemon, watch_registry},
11+
queue::{
12+
get_crate_pattern_and_priority, get_crate_priority, list_crate_priorities,
13+
remove_crate_priority, set_crate_priority,
14+
},
15+
queue_builder::queue_builder,
1016
};
11-
pub use self::queue_builder::queue_builder;
12-
pub(crate) use self::rustc_version::{get_correct_docsrs_style_file, parse_rustc_version};
13-
14-
#[cfg(test)]
15-
pub(crate) use self::cargo_metadata::{Dependency, Target};
1617

17-
mod cargo_metadata;
18+
pub(crate) mod cargo_metadata;
1819
pub mod consistency;
1920
mod copy;
2021
pub mod daemon;
2122
mod html;
2223
mod queue;
2324
pub(crate) mod queue_builder;
2425
pub(crate) mod rustc_version;
25-
use anyhow::{Context as _, Result};
26-
use serde::Serialize;
27-
use serde::de::DeserializeOwned;
28-
use std::{fmt, panic};
29-
use tracing::{Span, error, warn};
3026
pub(crate) mod sized_buffer;
3127

32-
use std::{future::Future, thread, time::Duration};
28+
use anyhow::{Context as _, Result};
29+
use serde::{Serialize, de::DeserializeOwned};
30+
use std::{fmt, future::Future, panic, thread, time::Duration};
31+
use tracing::{Span, error, warn};
3332

3433
pub(crate) fn report_error(err: &anyhow::Error) {
3534
// Debug-format for anyhow errors includes context & backtrace

0 commit comments

Comments
 (0)