Skip to content

Commit 0e2e340

Browse files
committed
w
1 parent b246297 commit 0e2e340

File tree

6 files changed

+45
-41
lines changed

6 files changed

+45
-41
lines changed

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn find_first_file_in_ancestors(dir_path: impl AsRef<Path>, filename: &str)
4848

4949
#[derive(Debug)]
5050
pub enum PackageTarget {
51-
Bin { name: String },
51+
Bin { crate_name: String },
5252
Lib,
5353
}
5454

@@ -139,9 +139,9 @@ impl Project {
139139
}
140140

141141
#[must_use]
142-
pub fn get_bin_default_name(&self) -> Option<&str> {
142+
pub fn get_bin_default_crate_name(&self) -> Option<&str> {
143143
match self.bin_path.len() {
144-
1 => self.bin_path.keys().next().map(|name| name.as_str()),
144+
1 => self.bin_path.keys().next().map(String::as_str),
145145
_ => None,
146146
}
147147
}

src/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,18 +370,19 @@ fn entrypoint<'a>(project: &'a Project, entrypoint_opt: &EntrypointOpt) -> Optio
370370
}
371371

372372
fn package_target(project: &Project, entrypoint_opt: &EntrypointOpt) -> Option<PackageTarget> {
373-
// WIP! it should probably be the package name, not the crate name in bin?
374-
375-
let bin_default =
376-
|| project.get_bin_default_name().map(|name| PackageTarget::Bin { name: name.to_owned() });
373+
let bin_default = || {
374+
project
375+
.get_bin_default_crate_name()
376+
.map(|name| PackageTarget::Bin { crate_name: name.to_owned() })
377+
};
377378

378379
match entrypoint_opt {
379380
EntrypointOpt::Auto => {
380381
project.get_lib_entryfile_path().map(|_| PackageTarget::Lib).or_else(bin_default)
381382
}
382383
EntrypointOpt::Lib => Some(PackageTarget::Lib),
383384
EntrypointOpt::BinDefault => bin_default(),
384-
EntrypointOpt::BinName(name) => Some(PackageTarget::Bin { name: name.clone() }),
385+
EntrypointOpt::BinName(name) => Some(PackageTarget::Bin { crate_name: name.clone() }),
385386
}
386387
}
387388

src/transform/intralinks/links.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ pub fn markdown_link_iterator(markdown: &Markdown) -> MarkdownItemIterator<Markd
240240

241241
Some((range.into(), link))
242242
}
243-
Some(_) => None,
244-
None => None,
243+
Some(_) | None => None,
245244
},
246245
_ => {
247246
if in_link.is_some() {
@@ -266,9 +265,7 @@ fn parse_raw_reference_link_definition(
266265
// We need to parse things manually here, because the pulldown-cmark parser escapes the title
267266
// and the link. We need the raw version to emit them later.
268267

269-
let Some(link_and_title) = raw_ref_def.get(label.len() + 3..).map(str::trim) else {
270-
return None;
271-
};
268+
let link_and_title = raw_ref_def.get(label.len() + 3..).map(str::trim)?;
272269

273270
assert_eq!(
274271
raw_ref_def.get(label.len() + 1..label.len() + 3),

src/transform/intralinks/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct IntralinksConfig {
5353
}
5454

5555
pub struct DocTransformIntralinks<F> {
56-
crate_name: String,
56+
package_name: String,
5757
package_target: PackageTarget,
5858
workspace_package: Option<String>,
5959
manifest_path: PathBuf,
@@ -66,15 +66,15 @@ where
6666
F: Fn(&str),
6767
{
6868
pub fn new(
69-
crate_name: impl Into<String>,
69+
package_name: impl Into<String>,
7070
package_target: PackageTarget,
7171
workspace_package: Option<String>,
7272
manifest_path: PathBuf,
7373
emit_warning: F,
7474
config: Option<IntralinksConfig>,
7575
) -> DocTransformIntralinks<F> {
7676
DocTransformIntralinks {
77-
crate_name: crate_name.into(),
77+
package_name: package_name.into(),
7878
package_target,
7979
workspace_package,
8080
manifest_path,
@@ -91,7 +91,7 @@ struct ItemPath<'a> {
9191

9292
impl<'a> ItemPath<'a> {
9393
fn new(segments: &'a [String]) -> ItemPath<'a> {
94-
assert!(segments.len() > 0, "path item must not be empty");
94+
assert!(!segments.is_empty(), "path item must not be empty");
9595

9696
ItemPath { segments: Cow::Borrowed(segments) }
9797
}
@@ -116,7 +116,7 @@ impl<'a> ItemPath<'a> {
116116
impl Display for ItemPath<'_> {
117117
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118118
// TODO Use standard library intersperse() one it stabilizes (https://github.com/rust-lang/rust/issues/79524).
119-
let iter = Itertools::intersperse(self.segments.iter().map(|s| s.as_str()), "::");
119+
let iter = Itertools::intersperse(self.segments.iter().map(String::as_str), "::");
120120

121121
for s in iter {
122122
f.write_str(s)?;
@@ -160,13 +160,13 @@ where
160160
true => {
161161
// WIP! ideally we wouldn't create an intralink resolver if we dont need it
162162
// see other WIP to split the strip links into another place.
163-
IntralinkResolver::new(self.crate_name.as_str(), &self.config.docs_rs)
163+
IntralinkResolver::new(self.package_name.as_str(), &self.config.docs_rs)
164164
}
165165
// We only load symbols type information when we need them.
166166
false => {
167167
// WIP! ideally we would want to return
168168
create_intralink_resolver(
169-
self.crate_name.as_str(),
169+
self.package_name.as_str(),
170170
&self.package_target,
171171
self.workspace_package.as_deref(),
172172
&self.manifest_path,
@@ -216,9 +216,9 @@ fn markdown_link(
216216
intralink_resolver: &IntralinkResolver,
217217
emit_warning: &impl Fn(&str),
218218
) -> MarkdownLinkAction {
219-
assert!(IntralinkResolver::is_intralink(&link));
219+
assert!(IntralinkResolver::is_intralink(link));
220220

221-
match intralink_resolver.resolve_link(&link) {
221+
match intralink_resolver.resolve_link(link) {
222222
None => {
223223
emit_warning(&format!("Could not resolve definition of `{}`.", link.symbol()));
224224

src/transform/intralinks/rustdoc.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a> ItemInfo<'a> {
7272
return None;
7373
}
7474

75-
if self.parent_kind.zip(other.parent_kind).map(|(s, o)| s != o).unwrap_or(false) {
75+
if self.parent_kind.zip(other.parent_kind).is_some_and(|(s, o)| s != o) {
7676
return None;
7777
}
7878

@@ -251,7 +251,7 @@ fn get_item_info<'a>(
251251
rustdoc_crate: &'a Crate,
252252
) -> Option<ItemInfo<'a>> {
253253
match rustdoc_crate.paths.get(item_id) {
254-
Some(item_summary) => Some(ItemInfo::from(&item_summary, parent_kind, item_context)),
254+
Some(item_summary) => Some(ItemInfo::from(item_summary, parent_kind, item_context)),
255255
None => rustdoc_crate.index.get(item_id).map(|item| {
256256
let path = match item.name.as_ref() {
257257
None => parent_path.clone(),
@@ -324,12 +324,12 @@ fn transitive_items<'a>(
324324
pub struct IntralinkResolver<'a> {
325325
link_url: HashMap<Link, String>,
326326
config: &'a IntralinksDocsRsConfig,
327-
crate_name: &'a str,
327+
package_name: &'a str,
328328
}
329329

330330
impl<'a> IntralinkResolver<'a> {
331-
pub fn new(crate_name: &'a str, config: &'a IntralinksDocsRsConfig) -> IntralinkResolver<'a> {
332-
IntralinkResolver { link_url: HashMap::new(), crate_name, config }
331+
pub fn new(package_name: &'a str, config: &'a IntralinksDocsRsConfig) -> IntralinkResolver<'a> {
332+
IntralinkResolver { link_url: HashMap::new(), package_name, config }
333333
}
334334

335335
fn url_segment(kind: ItemKind, name: &str) -> String {
@@ -390,16 +390,15 @@ impl<'a> IntralinkResolver<'a> {
390390
// WIP! improve this mess!
391391
// 0 means local crate.
392392
if item_info.crate_id == 0 {
393-
let base_url =
394-
self.config.docs_rs_base_url.as_ref().map_or("https://docs.rs", String::as_str);
395-
let version = self.config.docs_rs_version.as_ref().map_or("latest", String::as_str);
396-
// WIP!simple do we need this? can we extract it from somewhere?
397-
let crate_name = &self.crate_name;
393+
let base_url = self.config.docs_rs_base_url.as_deref().unwrap_or("https://docs.rs");
394+
let version = self.config.docs_rs_version.as_deref().unwrap_or("latest");
395+
let package_name = &self.package_name;
398396

399-
let url = format!("{base_url}/{crate_name}/{version}/{url_path}");
397+
let url = format!("{base_url}/{package_name}/{version}/{url_path}");
400398

401399
self.link_url.insert(link, url);
402400
} else {
401+
// WIP!simple how about links to crates that are not the standard library?
403402
if let Some(rustdoc_types::ExternalCrate {
404403
// WIP!simple how about package name vs crate name
405404
html_root_url: Some(ref base_url),
@@ -409,9 +408,9 @@ impl<'a> IntralinkResolver<'a> {
409408
// TODO Once we are able to use the stable version we can remove this
410409
// (https://github.com/rust-lang/rust/issues/76578).
411410
let base_url = base_url
411+
// WIP!simple we should probably only do this if we are actually talking about a standard lib item.
412412
.strip_suffix("/nightly/")
413-
.map(|p| format!("{}/stable/", p))
414-
.unwrap_or_else(|| base_url.to_owned());
413+
.map_or_else(|| base_url.to_owned(), |p| format!("{p}/stable/"));
415414

416415
let url = format!("{base_url}{url_path}");
417416

@@ -424,7 +423,7 @@ impl<'a> IntralinkResolver<'a> {
424423
self.link_url.get(link).map(String::as_str)
425424
}
426425

427-
// WIP! does this belong here?
426+
// WIP!simple does this belong here?
428427
pub fn is_intralink(link: &Link) -> bool {
429428
let has_lone_colon = || link.raw_link.replace("::", "").contains(':');
430429

@@ -441,7 +440,9 @@ fn run_rustdoc(
441440

442441
let rustdoc_json_path: PathBuf = {
443442
let target: rustdoc_json::PackageTarget = match package_target {
444-
PackageTarget::Bin { name } => rustdoc_json::PackageTarget::Bin(name.clone()),
443+
PackageTarget::Bin { crate_name } => {
444+
rustdoc_json::PackageTarget::Bin(crate_name.clone())
445+
}
445446
PackageTarget::Lib => rustdoc_json::PackageTarget::Lib,
446447
};
447448
let mut stderr = Vec::new();
@@ -451,7 +452,7 @@ fn run_rustdoc(
451452
.toolchain("nightly")
452453
.manifest_path(manifest_path)
453454
.document_private_items(true)
454-
// WIP! We need to parameterize the features based on the configuration
455+
// WIP!simple We need to parameterize the features based on the configuration
455456
// also "no default features"
456457
.all_features(true)
457458
.quiet(true)
@@ -489,7 +490,7 @@ fn run_rustdoc(
489490
}
490491

491492
pub fn create_intralink_resolver<'a>(
492-
crate_name: &'a str,
493+
package_name: &'a str,
493494
package_target: &PackageTarget,
494495
workspace_package: Option<&str>,
495496
manifest_path: &PathBuf,
@@ -501,14 +502,14 @@ pub fn create_intralink_resolver<'a>(
501502
HashMap::with_capacity(rustdoc_crate.index.len());
502503

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

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

510511
let links_items_id = crate_rustdoc_intralinks(&rustdoc_crate);
511-
let mut intralink_resolver = IntralinkResolver::new(crate_name, &config.docs_rs);
512+
let mut intralink_resolver = IntralinkResolver::new(package_name, &config.docs_rs);
512513

513514
for (link, item_id) in links_items_id {
514515
let link = Link::new(link.clone());

tests/tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,8 @@ fn integration_test_transform_intralinks_skip_rustdoc_when_no_intralinks() {
458458
fn integration_test_transform_intralinks_item_path_collision() {
459459
run_test("transform_intralinks_item_path_collision");
460460
}
461+
462+
#[test]
463+
fn integration_test_transform_intralinks_package_and_crate_names() {
464+
run_test("transform_intralinks_package_and_crate_names");
465+
}

0 commit comments

Comments
 (0)