Skip to content

Commit 31a7b6e

Browse files
committed
Refactor RenderedLink into its own type
1 parent d5495e2 commit 31a7b6e

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

src/librustdoc/clean/types.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Item {
118118
self.attrs.collapsed_doc_value()
119119
}
120120

121-
pub fn links(&self) -> Vec<(String, String)> {
121+
pub fn links(&self) -> Vec<RenderedLink> {
122122
self.attrs.links(&self.def_id.krate)
123123
}
124124

@@ -441,6 +441,13 @@ pub struct ItemLink {
441441
pub(crate) fragment: Option<String>,
442442
}
443443

444+
pub struct RenderedLink {
445+
/// The text the link was original written as
446+
pub(crate) original_text: String,
447+
/// The URL to put in the `href`
448+
pub(crate) href: String,
449+
}
450+
444451
impl Attributes {
445452
/// Extracts the content from an attribute `#[doc(cfg(content))]`.
446453
pub fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
@@ -617,7 +624,7 @@ impl Attributes {
617624
/// Gets links as a vector
618625
///
619626
/// Cache must be populated before call
620-
pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
627+
pub fn links(&self, krate: &CrateNum) -> Vec<RenderedLink> {
621628
use crate::html::format::href;
622629
use crate::html::render::CURRENT_DEPTH;
623630

@@ -631,7 +638,7 @@ impl Attributes {
631638
href.push_str("#");
632639
href.push_str(fragment);
633640
}
634-
Some((s.clone(), href))
641+
Some(RenderedLink { original_text: s.clone(), href })
635642
} else {
636643
None
637644
}
@@ -651,16 +658,16 @@ impl Attributes {
651658
};
652659
// This is a primitive so the url is done "by hand".
653660
let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
654-
Some((
655-
s.clone(),
656-
format!(
661+
Some(RenderedLink {
662+
original_text: s.clone(),
663+
href: format!(
657664
"{}{}std/primitive.{}.html{}",
658665
url,
659666
if !url.ends_with('/') { "/" } else { "" },
660667
&fragment[..tail],
661668
&fragment[tail..]
662669
),
663-
))
670+
})
664671
} else {
665672
panic!("This isn't a primitive?!");
666673
}

src/librustdoc/html/markdown.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use std::fmt::Write;
3434
use std::ops::Range;
3535
use std::str;
3636

37+
use crate::clean::RenderedLink;
3738
use crate::doctest;
3839
use crate::html::highlight;
3940
use crate::html::toc::TocBuilder;
@@ -52,7 +53,7 @@ fn opts() -> Options {
5253
pub struct Markdown<'a>(
5354
pub &'a str,
5455
/// A list of link replacements.
55-
pub &'a [(String, String)],
56+
pub &'a [RenderedLink],
5657
/// The current list of used header IDs.
5758
pub &'a mut IdMap,
5859
/// Whether to allow the use of explicit error codes in doctest lang strings.
@@ -78,7 +79,7 @@ pub struct MarkdownHtml<'a>(
7879
pub &'a Option<Playground>,
7980
);
8081
/// A tuple struct like `Markdown` that renders only the first paragraph.
81-
pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [(String, String)]);
82+
pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [RenderedLink]);
8283

8384
#[derive(Copy, Clone, PartialEq, Debug)]
8485
pub enum ErrorCodes {
@@ -339,11 +340,11 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
339340
/// Make headings links with anchor IDs and build up TOC.
340341
struct LinkReplacer<'a, 'b, I: Iterator<Item = Event<'a>>> {
341342
inner: I,
342-
links: &'b [(String, String)],
343+
links: &'b [RenderedLink],
343344
}
344345

345346
impl<'a, 'b, I: Iterator<Item = Event<'a>>> LinkReplacer<'a, 'b, I> {
346-
fn new(iter: I, links: &'b [(String, String)]) -> Self {
347+
fn new(iter: I, links: &'b [RenderedLink]) -> Self {
347348
LinkReplacer { inner: iter, links }
348349
}
349350
}
@@ -354,8 +355,8 @@ impl<'a, 'b, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, 'b, I>
354355
fn next(&mut self) -> Option<Self::Item> {
355356
let event = self.inner.next();
356357
if let Some(Event::Start(Tag::Link(kind, dest, text))) = event {
357-
if let Some(&(_, ref replace)) = self.links.iter().find(|link| link.0 == *dest) {
358-
Some(Event::Start(Tag::Link(kind, replace.to_owned().into(), text)))
358+
if let Some(link) = self.links.iter().find(|link| link.original_text == *dest) {
359+
Some(Event::Start(Tag::Link(kind, link.href.clone().into(), text)))
359360
} else {
360361
Some(Event::Start(Tag::Link(kind, dest, text)))
361362
}
@@ -855,8 +856,8 @@ impl Markdown<'_> {
855856
return String::new();
856857
}
857858
let replacer = |_: &str, s: &str| {
858-
if let Some(&(_, ref replace)) = links.iter().find(|link| &*link.0 == s) {
859-
Some((replace.clone(), s.to_owned()))
859+
if let Some(link) = links.iter().find(|link| &*link.original_text == s) {
860+
Some((link.original_text.clone(), link.href.clone()))
860861
} else {
861862
None
862863
}
@@ -933,8 +934,8 @@ impl MarkdownSummaryLine<'_> {
933934
}
934935

935936
let replacer = |_: &str, s: &str| {
936-
if let Some(&(_, ref replace)) = links.iter().find(|link| &*link.0 == s) {
937-
Some((replace.clone(), s.to_owned()))
937+
if let Some(rendered_link) = links.iter().find(|link| &*link.original_text == s) {
938+
Some((rendered_link.original_text.clone(), rendered_link.href.clone()))
938939
} else {
939940
None
940941
}

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use rustc_span::symbol::{sym, Symbol};
6363
use serde::ser::SerializeSeq;
6464
use serde::{Serialize, Serializer};
6565

66-
use crate::clean::{self, AttributesExt, Deprecation, GetDefId, SelfTy, TypeKind};
66+
use crate::clean::{self, AttributesExt, Deprecation, GetDefId, RenderedLink, SelfTy, TypeKind};
6767
use crate::config::RenderInfo;
6868
use crate::config::RenderOptions;
6969
use crate::docfs::{DocFS, PathError};
@@ -1780,7 +1780,7 @@ fn render_markdown(
17801780
w: &mut Buffer,
17811781
cx: &Context,
17821782
md_text: &str,
1783-
links: Vec<(String, String)>,
1783+
links: Vec<RenderedLink>,
17841784
prefix: &str,
17851785
is_hidden: bool,
17861786
) {

0 commit comments

Comments
 (0)