Skip to content

Commit 9845853

Browse files
Singletons should be Send, Sync and Clone (#7)
* Failing test: Singletons must be Clone. * Write data structure that allows to insert new elements without using a mutable reference. * Draft Sync check * Fix handling of hyphens and "wrong" versions in the rustdoc module. * Add `tracing`. * Extract into function. * Implement error + rendering diagnostic. * Fix trait resolution. * Fix package search * Fix error reporting when dealing with references. * Clippy fixes * Remove unused parameter. * Fix more clippy lints * Fix test expected error message. * Fix Clone checking. Send seems to be broken. * Green. * Sync :check: * Implement Clone * Remove stray dbg. * Be careful with namespaces. * Add nice help message explaining why we care about those traits. * Update example * Add rudimentary tracing instrumentation. * More instrumentation. * Remove wasteful clone.
1 parent d7035bc commit 9845853

File tree

29 files changed

+562
-204
lines changed

29 files changed

+562
-204
lines changed

examples/app_blueprint/blueprint.ron

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
),
2121
],
2222
component_lifecycles: {
23+
(
24+
registered_at: "app_blueprint",
25+
import_path: "crate :: http_client",
26+
): Singleton,
2327
(
2428
registered_at: "app_blueprint",
2529
import_path: "crate :: extract_path",
@@ -28,10 +32,6 @@
2832
registered_at: "app_blueprint",
2933
import_path: "crate :: logger",
3034
): Transient,
31-
(
32-
registered_at: "app_blueprint",
33-
import_path: "crate :: http_client",
34-
): Singleton,
3535
},
3636
router: {
3737
"/home": (
@@ -54,25 +54,25 @@
5454
constructor_locations: {
5555
(
5656
registered_at: "app_blueprint",
57-
import_path: "crate :: extract_path",
57+
import_path: "crate :: http_client",
5858
): (
59-
line: 39,
59+
line: 38,
6060
column: 10,
6161
file: "examples/app_blueprint/src/lib.rs",
6262
),
6363
(
6464
registered_at: "app_blueprint",
65-
import_path: "crate :: http_client",
65+
import_path: "crate :: logger",
6666
): (
67-
line: 38,
67+
line: 40,
6868
column: 10,
6969
file: "examples/app_blueprint/src/lib.rs",
7070
),
7171
(
7272
registered_at: "app_blueprint",
73-
import_path: "crate :: logger",
73+
import_path: "crate :: extract_path",
7474
): (
75-
line: 40,
75+
line: 39,
7676
column: 10,
7777
file: "examples/app_blueprint/src/lib.rs",
7878
),

libs/pavex/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ itertools = "0.10.3"
2424
cargo-manifest = "0.3"
2525
toml = "0.5"
2626
pathdiff = "0.2.1"
27+
elsa = "1.4.0"
28+
tracing = "0.1"

libs/pavex/src/rustdoc/compute.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::path::{Path, PathBuf};
22

33
use anyhow::Context;
4+
use guppy::Version;
45

56
use crate::rustdoc::package_id_spec::PackageIdSpecification;
7+
use crate::rustdoc::utils::normalize_crate_name;
68
use crate::rustdoc::TOOLCHAIN_CRATES;
79

810
#[derive(Debug, thiserror::Error)]
@@ -13,6 +15,17 @@ pub struct CannotGetCrateData {
1315
pub source: anyhow::Error,
1416
}
1517

18+
fn format_optional_version(v: &Option<Version>) -> Option<tracing::field::DisplayValue<String>> {
19+
v.as_ref().map(|v| {
20+
use std::fmt::Write;
21+
let mut s = format!("v{}.{}.{}", v.major, v.minor, v.patch);
22+
if !v.pre.is_empty() {
23+
write!(&mut s, "-{}", v.pre).unwrap();
24+
}
25+
tracing::field::display(s)
26+
})
27+
}
28+
1629
/// Return the JSON documentation for a crate.
1730
/// The crate is singled out, within the current workspace, using a [`PackageIdSpecification`].
1831
///
@@ -21,7 +34,15 @@ pub struct CannotGetCrateData {
2134
///
2235
/// `root_folder` is `cargo`'s target directory for the current workspace: that is where we are
2336
/// going to look for the JSON files generated by `rustdoc`.
24-
pub(super) fn get_crate_data(
37+
#[tracing::instrument(
38+
skip_all,
39+
fields(
40+
crate.name = package_id_spec.name,
41+
crate.version = format_optional_version(& package_id_spec.version),
42+
crate.source = package_id_spec.source
43+
)
44+
)]
45+
pub(super) fn compute_crate_docs(
2546
root_folder: &Path,
2647
package_id_spec: &PackageIdSpecification,
2748
) -> Result<rustdoc_types::Crate, CannotGetCrateData> {
@@ -34,17 +55,17 @@ pub(super) fn get_crate_data(
3455
// documentation on the fly. We assume that their JSON docs have been pre-computed and are
3556
// available for us to look at.
3657
if TOOLCHAIN_CRATES.contains(&package_id_spec.name.as_str()) {
37-
get_toolchain_crate_data(package_id_spec)
58+
get_toolchain_crate_docs(package_id_spec)
3859
} else {
39-
_get_crate_data(root_folder, package_id_spec)
60+
_compute_crate_docs(root_folder, package_id_spec)
4061
}
4162
.map_err(|e| CannotGetCrateData {
4263
package_spec: package_id_spec.to_string(),
4364
source: e,
4465
})
4566
}
4667

47-
fn get_toolchain_crate_data(
68+
fn get_toolchain_crate_docs(
4869
package_id_spec: &PackageIdSpecification,
4970
) -> Result<rustdoc_types::Crate, anyhow::Error> {
5071
let root_folder = get_json_docs_root_folder_via_rustup()?;
@@ -113,7 +134,7 @@ fn get_nightly_toolchain_root_folder_via_rustup() -> Result<PathBuf, anyhow::Err
113134
Ok(path.parent().unwrap().parent().unwrap().to_path_buf())
114135
}
115136

116-
fn _get_crate_data(
137+
fn _compute_crate_docs(
117138
target_directory: &Path,
118139
package_id_spec: &PackageIdSpecification,
119140
) -> Result<rustdoc_types::Crate, anyhow::Error> {
@@ -142,9 +163,10 @@ fn _get_crate_data(
142163
);
143164
}
144165

145-
let json_path = target_directory
146-
.join("doc")
147-
.join(format!("{}.json", &package_id_spec.name));
166+
let json_path = target_directory.join("doc").join(format!(
167+
"{}.json",
168+
normalize_crate_name(&package_id_spec.name)
169+
));
148170

149171
let json = fs_err::read_to_string(json_path).with_context(|| {
150172
format!(

libs/pavex/src/rustdoc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub use queries::{Crate, CrateCollection, GlobalTypeId, UnknownTypePath};
99
mod compute;
1010
mod package_id_spec;
1111
mod queries;
12+
mod utils;
1213

1314
pub const STD_PACKAGE_ID: &str = "std";
1415
pub const TOOLCHAIN_CRATES: [&str; 3] = ["std", "core", "alloc"];

0 commit comments

Comments
 (0)