Skip to content

Commit 5746982

Browse files
committed
work from today
1 parent d69743b commit 5746982

File tree

13 files changed

+397
-225
lines changed

13 files changed

+397
-225
lines changed

src/librustdoc/config.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub(crate) struct RenderOptions {
297297
/// If `true` (default) write the shared cross-crate information to the doc root
298298
pub(crate) write_rendered_cci: bool,
299299
/// Copies these externally documented crates into our doc root.
300-
pub(crate) include_rendered_docs: Vec<PathToDocSubdirectory>,
300+
pub(crate) include_rendered_docs: Vec<PathToDocRoot>,
301301
/// Path to crate-info.json for external crates.
302302
pub(crate) parts_paths: Vec<PathToParts>,
303303
/// Where to write crate-info.json.
@@ -506,10 +506,14 @@ impl Options {
506506
Err(err) => dcx.fatal(err),
507507
};
508508

509-
let include_rendered_docs = matches.opt_strs("include-rendered-docs").into_iter()
510-
.map(|p| PathToDocSubdirectory(PathBuf::from(p)))
509+
let include_rendered_docs = matches
510+
.opt_strs("include-rendered-docs")
511+
.into_iter()
512+
.map(|p| PathToDocRoot(PathBuf::from(p)))
511513
.collect();
512-
let parts_paths = matches.opt_strs("include-info-json").into_iter()
514+
let parts_paths = matches
515+
.opt_strs("include-info-json")
516+
.into_iter()
513517
.map(|path| PathToParts(PathBuf::from(path)))
514518
.collect();
515519

@@ -757,7 +761,8 @@ impl Options {
757761
Ok(result) => result,
758762
Err(e) => dcx.fatal(format!("could not parse --merge: {e}")),
759763
};
760-
let parts_out_dir = matches.opt_str("write-info-json").map(|p| PathToParts(PathBuf::from(p)));
764+
let parts_out_dir =
765+
matches.opt_str("write-info-json").map(|p| PathToParts(PathBuf::from(p)));
761766

762767
if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) {
763768
dcx.fatal(
@@ -926,9 +931,9 @@ fn parse_extern_html_roots(
926931
Ok(externs)
927932
}
928933

929-
/// Path to the doc subdirectory for a specific crate, for example `target/doc/serde_json`.
934+
/// Path to the doc root for a specific crate, for example `target/doc/serde_json`.
930935
#[derive(Clone, Debug)]
931-
pub(crate) struct PathToDocSubdirectory(pub PathBuf);
936+
pub(crate) struct PathToDocRoot(pub PathBuf);
932937

933938
/// Path to crate-info.json
934939
///
@@ -952,7 +957,9 @@ fn parse_merge(matches: &getopts::Matches) -> Result<ShouldMerge, &'static str>
952957
None => Ok(ShouldMerge { read_rendered_cci: true, write_rendered_cci: true }),
953958
Some("read-write") => Ok(ShouldMerge { read_rendered_cci: true, write_rendered_cci: true }),
954959
Some("none") => Ok(ShouldMerge { read_rendered_cci: false, write_rendered_cci: false }),
955-
Some("write-only") => Ok(ShouldMerge { read_rendered_cci: false, write_rendered_cci: true }),
960+
Some("write-only") => {
961+
Ok(ShouldMerge { read_rendered_cci: false, write_rendered_cci: true })
962+
}
956963
Some(_) => Err("argument to --merge must be `read-write`, `none`, or `write-only`"),
957964
}
958965
}

src/librustdoc/html/render/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use rustc_session::Session;
1212
use rustc_span::edition::Edition;
1313
use rustc_span::{sym, FileName, Symbol};
1414

15+
use super::copy_dir::copy_dir_all;
1516
use super::print_item::{full_path, item_path, print_item};
16-
use super::write_shared::{write_shared};
17-
use super::copy_dir::{copy_dir_all};
17+
use super::write_shared::write_shared;
1818
use super::{
1919
collect_spans_and_sources, scrape_examples_help,
2020
sidebar::print_sidebar,

src/librustdoc/html/render/copy_dir.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use std::io::{Error, Result, ErrorKind};
2-
use std::path::PathBuf;
31
use std::fs;
2+
use std::io::{Error, ErrorKind, Result};
3+
use std::path::PathBuf;
44

55
/// Recursively copies the contents of the directory `src` to the directory `dst`.
66
/// Analogous to `cp -rf src/* dst` and Python's `shutil.copytree`
77
///
88
/// Creates all directories needed to perform the copy.
9+
///
10+
/// Will overwrite files in the output directory.
911
pub(crate) fn copy_dir_all<S: Into<PathBuf>, D: Into<PathBuf>>(src: S, dst: D) -> Result<()> {
1012
copy_dir_all_mono(src.into(), dst.into())
1113
}
@@ -15,18 +17,24 @@ fn copy_dir_all_mono(src: PathBuf, dst: PathBuf) -> Result<()> {
1517
let mut dirs: Vec<(PathBuf, PathBuf)> = Vec::default();
1618

1719
if !src.is_dir() {
18-
return Err(Error::new(ErrorKind::Other, format!("src path `{src:?}` should be a directory")));
20+
return Err(Error::new(
21+
ErrorKind::Other,
22+
format!("src path `{src:?}` should be a directory"),
23+
));
1924
}
2025
if !dst.is_dir() {
21-
return Err(Error::new(ErrorKind::Other, format!("dst path `{dst:?}` should be a directory")));
26+
return Err(Error::new(
27+
ErrorKind::Other,
28+
format!("dst path `{dst:?}` should be a directory"),
29+
));
2230
}
2331

2432
dirs.push((src, dst));
2533

2634
while let Some((src, dst)) = dirs.pop() {
2735
match fs::create_dir(&dst) {
28-
Ok(()) => {},
29-
Err(e) if e.kind() == ErrorKind::AlreadyExists => {},
36+
Ok(()) => {}
37+
Err(e) if e.kind() == ErrorKind::AlreadyExists => {}
3038
Err(e) => return Err(e),
3139
}
3240

@@ -50,10 +58,10 @@ fn copy_dir_all_mono(src: PathBuf, dst: PathBuf) -> Result<()> {
5058
#[cfg(test)]
5159
mod tests {
5260
use super::copy_dir_all;
53-
use tempfile::TempDir;
61+
use std::fs;
5462
use std::io::Result;
5563
use std::path::Path;
56-
use std::fs;
64+
use tempfile::TempDir;
5765

5866
fn create_paths(root: &Path, paths: &[&str]) -> Result<()> {
5967
for path in paths {

src/librustdoc/html/render/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ pub(crate) mod search_index;
2828
#[cfg(test)]
2929
mod tests;
3030

31-
mod copy_dir;
3231
mod context;
32+
mod copy_dir;
3333
mod print_item;
3434
mod sidebar;
35+
mod sorted_json;
36+
mod sorted_template;
3537
mod span_map;
3638
mod type_layout;
3739
mod write_shared;
38-
mod sorted_json;
39-
mod sorted_template;
4040

4141
pub(crate) use self::context::*;
4242
pub(crate) use self::span_map::{collect_spans_and_sources, LinkFromSrc};

src/librustdoc/html/render/search_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::formats::cache::{Cache, OrphanImplItem};
1717
use crate::formats::item_type::ItemType;
1818
use crate::html::format::join_with_double_colon;
1919
use crate::html::markdown::short_markdown_summary;
20-
use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, RenderTypeId};
2120
use crate::html::render::sorted_json::SortedJson;
21+
use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, RenderTypeId};
2222

2323
use encode::{bitmap_to_string, write_vlqhex_to_string};
2424

src/librustdoc/html/render/sorted_json.rs

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use itertools::Itertools as _;
2+
use serde::{Deserialize, Serialize};
13
use serde_json::Value;
2-
use serde::{Serialize, Deserialize};
3-
use std::fmt;
44
use std::borrow::Borrow;
5-
use itertools::Itertools as _;
5+
use std::fmt;
66

77
/// Prerenedered json.
88
///
@@ -24,14 +24,17 @@ impl SortedJson {
2424
}
2525

2626
/// Serializes and sorts
27-
pub(crate) fn array<T: Borrow<SortedJson>, I: IntoIterator<Item=T>>(items: I) -> Self {
28-
let items = items.into_iter()
27+
pub(crate) fn array<T: Borrow<SortedJson>, I: IntoIterator<Item = T>>(items: I) -> Self {
28+
let items = items
29+
.into_iter()
2930
.sorted_unstable_by(|a, b| a.borrow().cmp(&b.borrow()))
3031
.format_with(",", |item, f| f(item.borrow()));
3132
SortedJson(format!("[{}]", items))
3233
}
3334

34-
pub(crate) fn array_unsorted<T: Borrow<SortedJson>, I: IntoIterator<Item=T>>(items: I) -> Self {
35+
pub(crate) fn array_unsorted<T: Borrow<SortedJson>, I: IntoIterator<Item = T>>(
36+
items: I,
37+
) -> Self {
3538
let items = items.into_iter().format_with(",", |item, f| f(item.borrow()));
3639
SortedJson(format!("[{items}]"))
3740
}
@@ -55,6 +58,29 @@ impl From<SortedJson> for Value {
5558
}
5659
}
5760

61+
/// For use in JSON.parse('{...}').
62+
///
63+
/// JSON.parse supposedly loads faster than raw JS source,
64+
/// so this is used for large objects.
65+
#[derive(Debug, Clone, Serialize, Deserialize)]
66+
pub(crate) struct EscapedJson(SortedJson);
67+
68+
impl From<SortedJson> for EscapedJson {
69+
fn from(json: SortedJson) -> Self {
70+
EscapedJson(json)
71+
}
72+
}
73+
74+
impl fmt::Display for EscapedJson {
75+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76+
// All these `replace` calls are because we have to go through JS string
77+
// for JSON content.
78+
// We need to escape double quotes for the JSON
79+
let json = self.0.0.replace('\\', r"\\").replace('\'', r"\'").replace("\\\"", "\\\\\"");
80+
write!(f, "{}", json)
81+
}
82+
}
83+
5884
#[cfg(test)]
5985
mod tests {
6086
use super::*;
@@ -76,6 +102,48 @@ mod tests {
76102
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
77103
}
78104

105+
#[test]
106+
fn escape_json_number() {
107+
let json = SortedJson::serialize(3);
108+
let json = EscapedJson::from(json);
109+
assert_eq!(format!("{json}"), "3");
110+
}
111+
112+
#[test]
113+
fn escape_json_single_quote() {
114+
let json = SortedJson::serialize("he's");
115+
let json = EscapedJson::from(json);
116+
assert_eq!(dbg!(format!("{json}")), r#""he\'s""#);
117+
}
118+
119+
#[test]
120+
fn escape_json_array() {
121+
let json = SortedJson::serialize([1,2,3]);
122+
let json = EscapedJson::from(json);
123+
assert_eq!(dbg!(format!("{json}")), r#"[1,2,3]"#);
124+
}
125+
126+
#[test]
127+
fn escape_json_string() {
128+
let json = SortedJson::serialize(r#"he"llo"#);
129+
let json = EscapedJson::from(json);
130+
assert_eq!(dbg!(format!("{json}")), r#""he\\\"llo""#);
131+
}
132+
133+
#[test]
134+
fn escape_json_string_escaped() {
135+
let json = SortedJson::serialize(r#"he\"llo"#);
136+
let json = EscapedJson::from(json);
137+
assert_eq!(format!("{json}"), r#""he\\\\\\\"llo""#);
138+
}
139+
140+
#[test]
141+
fn escape_json_string_escaped_escaped() {
142+
let json = SortedJson::serialize(r#"he\\"llo"#);
143+
let json = EscapedJson::from(json);
144+
assert_eq!(format!("{json}"), r#""he\\\\\\\\\\\"llo""#);
145+
}
146+
79147
#[test]
80148
fn number() {
81149
let json = SortedJson::serialize(3);
@@ -100,7 +168,7 @@ mod tests {
100168
#[test]
101169
fn serialize_array() {
102170
let json = SortedJson::serialize([3, 1, 2]);
103-
let serialized = "[3,1,2]";
171+
let serialized = "[3,1,2]";
104172
check(json, serialized);
105173
}
106174

src/librustdoc/html/render/sorted_template.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use std::collections::BTreeSet;
12
use std::fmt;
23
use std::marker::PhantomData;
3-
use std::collections::BTreeSet;
44
use std::str::FromStr;
55

6-
use serde::{Serialize, Deserialize};
6+
use serde::{Deserialize, Serialize};
77

88
/// Append-only templates for sorted, deduplicated lists of items.
99
///

0 commit comments

Comments
 (0)