Skip to content

Commit

Permalink
Singletons should be Send, Sync and Clone (#7)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
LukeMathWalker authored Nov 20, 2022
1 parent d7035bc commit 9845853
Show file tree
Hide file tree
Showing 29 changed files with 562 additions and 204 deletions.
20 changes: 10 additions & 10 deletions examples/app_blueprint/blueprint.ron
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
),
],
component_lifecycles: {
(
registered_at: "app_blueprint",
import_path: "crate :: http_client",
): Singleton,
(
registered_at: "app_blueprint",
import_path: "crate :: extract_path",
Expand All @@ -28,10 +32,6 @@
registered_at: "app_blueprint",
import_path: "crate :: logger",
): Transient,
(
registered_at: "app_blueprint",
import_path: "crate :: http_client",
): Singleton,
},
router: {
"/home": (
Expand All @@ -54,25 +54,25 @@
constructor_locations: {
(
registered_at: "app_blueprint",
import_path: "crate :: extract_path",
import_path: "crate :: http_client",
): (
line: 39,
line: 38,
column: 10,
file: "examples/app_blueprint/src/lib.rs",
),
(
registered_at: "app_blueprint",
import_path: "crate :: http_client",
import_path: "crate :: logger",
): (
line: 38,
line: 40,
column: 10,
file: "examples/app_blueprint/src/lib.rs",
),
(
registered_at: "app_blueprint",
import_path: "crate :: logger",
import_path: "crate :: extract_path",
): (
line: 40,
line: 39,
column: 10,
file: "examples/app_blueprint/src/lib.rs",
),
Expand Down
2 changes: 2 additions & 0 deletions libs/pavex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ itertools = "0.10.3"
cargo-manifest = "0.3"
toml = "0.5"
pathdiff = "0.2.1"
elsa = "1.4.0"
tracing = "0.1"
38 changes: 30 additions & 8 deletions libs/pavex/src/rustdoc/compute.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::path::{Path, PathBuf};

use anyhow::Context;
use guppy::Version;

use crate::rustdoc::package_id_spec::PackageIdSpecification;
use crate::rustdoc::utils::normalize_crate_name;
use crate::rustdoc::TOOLCHAIN_CRATES;

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

fn format_optional_version(v: &Option<Version>) -> Option<tracing::field::DisplayValue<String>> {
v.as_ref().map(|v| {
use std::fmt::Write;
let mut s = format!("v{}.{}.{}", v.major, v.minor, v.patch);
if !v.pre.is_empty() {
write!(&mut s, "-{}", v.pre).unwrap();
}
tracing::field::display(s)
})
}

/// Return the JSON documentation for a crate.
/// The crate is singled out, within the current workspace, using a [`PackageIdSpecification`].
///
Expand All @@ -21,7 +34,15 @@ pub struct CannotGetCrateData {
///
/// `root_folder` is `cargo`'s target directory for the current workspace: that is where we are
/// going to look for the JSON files generated by `rustdoc`.
pub(super) fn get_crate_data(
#[tracing::instrument(
skip_all,
fields(
crate.name = package_id_spec.name,
crate.version = format_optional_version(& package_id_spec.version),
crate.source = package_id_spec.source
)
)]
pub(super) fn compute_crate_docs(
root_folder: &Path,
package_id_spec: &PackageIdSpecification,
) -> Result<rustdoc_types::Crate, CannotGetCrateData> {
Expand All @@ -34,17 +55,17 @@ pub(super) fn get_crate_data(
// documentation on the fly. We assume that their JSON docs have been pre-computed and are
// available for us to look at.
if TOOLCHAIN_CRATES.contains(&package_id_spec.name.as_str()) {
get_toolchain_crate_data(package_id_spec)
get_toolchain_crate_docs(package_id_spec)
} else {
_get_crate_data(root_folder, package_id_spec)
_compute_crate_docs(root_folder, package_id_spec)
}
.map_err(|e| CannotGetCrateData {
package_spec: package_id_spec.to_string(),
source: e,
})
}

fn get_toolchain_crate_data(
fn get_toolchain_crate_docs(
package_id_spec: &PackageIdSpecification,
) -> Result<rustdoc_types::Crate, anyhow::Error> {
let root_folder = get_json_docs_root_folder_via_rustup()?;
Expand Down Expand Up @@ -113,7 +134,7 @@ fn get_nightly_toolchain_root_folder_via_rustup() -> Result<PathBuf, anyhow::Err
Ok(path.parent().unwrap().parent().unwrap().to_path_buf())
}

fn _get_crate_data(
fn _compute_crate_docs(
target_directory: &Path,
package_id_spec: &PackageIdSpecification,
) -> Result<rustdoc_types::Crate, anyhow::Error> {
Expand Down Expand Up @@ -142,9 +163,10 @@ fn _get_crate_data(
);
}

let json_path = target_directory
.join("doc")
.join(format!("{}.json", &package_id_spec.name));
let json_path = target_directory.join("doc").join(format!(
"{}.json",
normalize_crate_name(&package_id_spec.name)
));

let json = fs_err::read_to_string(json_path).with_context(|| {
format!(
Expand Down
1 change: 1 addition & 0 deletions libs/pavex/src/rustdoc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use queries::{Crate, CrateCollection, GlobalTypeId, UnknownTypePath};
mod compute;
mod package_id_spec;
mod queries;
mod utils;

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

0 comments on commit 9845853

Please sign in to comment.