Skip to content

Commit

Permalink
w
Browse files Browse the repository at this point in the history
  • Loading branch information
orium committed Jun 21, 2024
1 parent e3132e9 commit b91bcb0
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 38 deletions.
32 changes: 11 additions & 21 deletions src/transform/intralinks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,19 @@ where

// WIP! have a thing that checks if the nightly toolchain is installed

// We only load intralink resolution information when we need it.
let intralink_resolver: IntralinkResolver<'_> = match strip_links {
true => {
// WIP! ideally we wouldn't create an intralink resolver if we dont need it
// see other WIP to split the strip links into another place.
// Create an empty resolver, since we are going to strip all intralinks.
IntralinkResolver::new(self.package_name.as_str(), &self.config.docs_rs)
}
// We only load symbols type information when we need them.
false => {
// WIP! ideally we would want to return
create_intralink_resolver(
self.package_name.as_str(),
&self.package_target,
self.workspace_package.as_deref(),
&self.manifest_path,
&self.config,
)?
}
false => create_intralink_resolver(
self.package_name.as_str(),
&self.package_target,
self.workspace_package.as_deref(),
&self.manifest_path,
&self.config,
)?,
};

let doc = rewrite_links(doc, &intralink_resolver, &self.emit_warning, &self.config);
Expand All @@ -181,7 +177,6 @@ where
}
}

// WIP! separate the strip links logic from rewrite_reference_links_definitions and rewrite_markdown_links?
fn rewrite_links(
doc: &Doc,
intralink_resolver: &IntralinkResolver,
Expand All @@ -191,18 +186,13 @@ fn rewrite_links(
let RewriteReferenceLinksResult { doc, reference_links_to_remove } =
rewrite_reference_links_definitions(doc, intralink_resolver, emit_warning, config);

let doc = rewrite_markdown_links(
rewrite_markdown_links(
&doc,
intralink_resolver,
emit_warning,
config,
&reference_links_to_remove,
);

// TODO Refactor link removal code so that it all happens in a new phase and not inside the
// functions above.

doc
)
}

enum MarkdownLinkAction {
Expand Down
32 changes: 15 additions & 17 deletions src/transform/intralinks/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ fn get_item_info<'a>(
}
}

// WIP! are we using the right abstraction here?
fn transitive_items<'a>(
item_id: &'a ItemId,
item_info: &ItemInfo<'a>,
Expand Down Expand Up @@ -482,7 +481,7 @@ fn run_rustdoc(
.manifest_path(manifest_path)
.document_private_items(true)
// WIP!simple We need to parameterize the features based on the configuration
// also "no default features"
// also "no default features"
.all_features(true)
.quiet(true)
.color(rustdoc_json::Color::Never)
Expand All @@ -507,9 +506,6 @@ fn run_rustdoc(
})?
};

// WIP! temp debug
eprintln!("json file: {}", rustdoc_json_path.display());

let rustdoc_crate = crate_from_file(&rustdoc_json_path)?;

match rustdoc_crate.format_version {
Expand All @@ -521,6 +517,19 @@ fn run_rustdoc(
}
}

fn items_info(rustdoc_crate: &Crate) -> HashMap<&ItemId, ItemInfo<'_>> {
let mut items_info: HashMap<&ItemId, ItemInfo<'_>> =
HashMap::with_capacity(rustdoc_crate.index.len());

for (item_id, item_summary) in &rustdoc_crate.paths {
let item_info = ItemInfo::from(item_summary, None, ItemContext::Normal);

transitive_items(item_id, &item_info, ItemContext::Normal, rustdoc_crate, &mut items_info);
}

items_info
}

pub fn create_intralink_resolver<'a>(
package_name: &'a str,
package_target: &PackageTarget,
Expand All @@ -530,16 +539,7 @@ pub fn create_intralink_resolver<'a>(
) -> Result<IntralinkResolver<'a>, IntralinkError> {
let rustdoc_crate = run_rustdoc(package_target, workspace_package, manifest_path)?;

let mut items_info: HashMap<&ItemId, ItemInfo<'_>> =
HashMap::with_capacity(rustdoc_crate.index.len());

// WIP!simple this should be in a function of it's own.
for (item_id, item_summary) in &rustdoc_crate.paths {
let item_info = ItemInfo::from(item_summary, None, ItemContext::Normal);

transitive_items(item_id, &item_info, ItemContext::Normal, &rustdoc_crate, &mut items_info);
}

let items_info: HashMap<&ItemId, ItemInfo<'_>> = items_info(&rustdoc_crate);
let links_items_id = crate_rustdoc_intralinks(&rustdoc_crate);
let mut intralink_resolver = IntralinkResolver::new(package_name, &config.docs_rs);

Expand All @@ -557,5 +557,3 @@ pub fn create_intralink_resolver<'a>(
}

// WIP! move the tests that belong here

// WIP! support links like [`crate`]
23 changes: 23 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,29 @@ fn integration_test_transform_intralinks_skip_rustdoc_when_no_intralinks() {

let options =
TestOptions { args: &["--entrypoint", "bin:real-test"], ..TestOptions::default() };

run_test_with_options(test_name, &options);
}

#[test]
fn integration_test_transform_intralinks_skip_rustdoc_when_strip_intralinks() {
let test_name = "transform_intralinks_skip_rustdoc_when_strip_intralinks";

// This checks that if rustdoc runs we actually fail, so that the real test before is
// actually asserting that rustdoc is not running because otherwise it would have failed.
let options = TestOptions {
args: &["--entrypoint", "bin:test-precondition"],
expected_exit_code: 1,
check_readme_expected: false,
..TestOptions::default()
};
run_test_with_options(test_name, &options);

let options = TestOptions {
args: &["--entrypoint", "bin:real-test", "--intralinks-strip-links"],
..TestOptions::default()
};

run_test_with_options(test_name, &options);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "integration_test"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "test-precondition"
path = "src/test-precondition-main.rs"

[[bin]]
name = "real-test"
path = "src/main.rs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- cargo-rdme start -->

This crate is not very interesting.

<!-- cargo-rdme end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- cargo-rdme start -->
<!-- cargo-rdme end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! This [crate](crate) is not very interesting.
mod no_module_here;

fn main() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Crate with an [`main`](main) to prove that rustdoc fails with this. Otherwise the test is not
//! testing that rustdoc is not running.
#![deny(rustdoc::broken_intra_doc_links)]

mod no_module_here;

fn main() {

}

0 comments on commit b91bcb0

Please sign in to comment.