Skip to content

Commit 65ed9fc

Browse files
committed
version number, copy_dir_all
1 parent d2598b4 commit 65ed9fc

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

src/librustdoc/html/render/copy_dir.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ use std::fs;
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`
7+
///
8+
/// Creates all directories needed to perform the copy.
79
pub(crate) fn copy_dir_all<S: Into<PathBuf>, D: Into<PathBuf>>(src: S, dst: D) -> Result<()> {
8-
copy_dir_mono(src.into(), dst.into())
10+
copy_dir_all_mono(src.into(), dst.into())
911
}
1012

1113
/// Helper for `copy_dir`
12-
fn copy_dir_mono(src: PathBuf, dst: PathBuf) -> Result<()> {
14+
fn copy_dir_all_mono(src: PathBuf, dst: PathBuf) -> Result<()> {
1315
let mut dirs: Vec<(PathBuf, PathBuf)> = Vec::default();
1416

1517
if !src.is_dir() {

src/librustdoc/html/render/search_index.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ pub(crate) fn build_index<'tcx>(
429429
})
430430
.collect::<Vec<_>>();
431431

432+
// XXX: update CrateInfoVersion in write_shared upon any changes to this format.
432433
struct CrateData<'a> {
433434
items: Vec<&'a IndexItem>,
434435
paths: Vec<(ItemType, Vec<Symbol>, Option<Vec<Symbol>>)>,
@@ -682,6 +683,8 @@ pub(crate) fn build_index<'tcx>(
682683
desc.iter().map(|(len, _)| *len).sum::<usize>() + empty_desc.len()
683684
);
684685

686+
// XXX: update CrateInfoVersion in write_shared upon any changes to this format.
687+
685688
// The index, which is actually used to search, is JSON
686689
// It uses `JSON.parse(..)` to actually load, since JSON
687690
// parses faster than the full JavaScript syntax.

src/librustdoc/html/render/sorted_json.rs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,26 @@ use itertools::Itertools as _;
1515
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
1616
#[serde(from = "Value")]
1717
#[serde(into = "Value")]
18-
pub struct SortedJson(String);
18+
pub(crate) struct SortedJson(String);
1919

2020
impl SortedJson {
2121
/// If you pass in an array, it will not be sorted.
22-
pub fn serialize<T: Serialize>(item: T) -> Self {
22+
pub(crate) fn serialize<T: Serialize>(item: T) -> Self {
2323
SortedJson(serde_json::to_string(&item).unwrap())
2424
}
2525

26-
/// Assumes that `item` is already JSON encoded
27-
///
28-
/// TODO: remove this, and use SortedJson everywhere JSON is rendered
29-
pub fn preserialized(item: String) -> Self {
30-
SortedJson(item)
31-
}
32-
3326
/// Serializes and sorts
34-
pub fn array<T: Borrow<SortedJson>, I: IntoIterator<Item=T>>(items: I) -> Self {
27+
pub(crate) fn array<T: Borrow<SortedJson>, I: IntoIterator<Item=T>>(items: I) -> Self {
3528
let items = items.into_iter()
3629
.sorted_unstable_by(|a, b| a.borrow().cmp(&b.borrow()))
3730
.format_with(",", |item, f| f(item.borrow()));
3831
SortedJson(format!("[{}]", items))
3932
}
4033

41-
pub fn array_unsorted<T: Borrow<SortedJson>, I: IntoIterator<Item=T>>(items: I) -> Self {
34+
pub(crate) fn array_unsorted<T: Borrow<SortedJson>, I: IntoIterator<Item=T>>(items: I) -> Self {
4235
let items = items.into_iter().format_with(",", |item, f| f(item.borrow()));
4336
SortedJson(format!("[{items}]"))
4437
}
45-
46-
pub fn object<K, V, I>(items: I) -> Self
47-
where K: Borrow<SortedJson>,
48-
V: Borrow<SortedJson>,
49-
I: IntoIterator<Item=(K, V)>,
50-
{
51-
let items = items.into_iter()
52-
.sorted_unstable_by(|a, b| a.0.borrow().cmp(&b.0.borrow()))
53-
.format_with(",", |(k, v), f| f(&format_args!("{}:{}", k.borrow(), v.borrow())));
54-
SortedJson(format!("{{{}}}", items))
55-
}
5638
}
5739

5840
impl fmt::Display for SortedJson {
@@ -80,8 +62,16 @@ mod tests {
8062
fn check(json: SortedJson, serialized: &str) {
8163
assert_eq!(json.to_string(), serialized);
8264
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
65+
8366
let json = json.to_string();
8467
let json: SortedJson = serde_json::from_str(&json).unwrap();
68+
69+
assert_eq!(json.to_string(), serialized);
70+
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
71+
72+
let json = serde_json::to_string(&json).unwrap();
73+
let json: SortedJson = serde_json::from_str(&json).unwrap();
74+
8575
assert_eq!(json.to_string(), serialized);
8676
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
8777
}
@@ -100,6 +90,13 @@ mod tests {
10090
check(json, serialized);
10191
}
10292

93+
#[test]
94+
fn string() {
95+
let json = SortedJson::serialize("he\"llo");
96+
let serialized = r#""he\"llo""#;
97+
check(json, serialized);
98+
}
99+
103100
#[test]
104101
fn serialize_array() {
105102
let json = SortedJson::serialize([3, 1, 2]);
@@ -116,6 +113,17 @@ mod tests {
116113
check(json, serialized);
117114
}
118115

116+
#[test]
117+
fn nested_array() {
118+
let a = SortedJson::serialize(3);
119+
let b = SortedJson::serialize(2);
120+
let c = SortedJson::serialize(1);
121+
let d = SortedJson::serialize([1, 3, 2]);
122+
let json = SortedJson::array([a, b, c, d]);
123+
let serialized = r#"[1,2,3,[1,3,2]]"#;
124+
check(json, serialized);
125+
}
126+
119127
#[test]
120128
fn array_unsorted() {
121129
let items = ["c", "a", "b"];
@@ -124,15 +132,4 @@ mod tests {
124132
let json = SortedJson::array_unsorted(items);
125133
check(json, serialized);
126134
}
127-
128-
#[test]
129-
fn object() {
130-
let items = [("c", 1), ("a", 10), ("b", 3)];
131-
let serialized = r#"{"a":10,"b":3,"c":1}"#;
132-
let items: Vec<(SortedJson, SortedJson)> = items.into_iter()
133-
.map(|(k, v)| (SortedJson::serialize(k), SortedJson::serialize(v)))
134-
.collect();
135-
let json = SortedJson::object(items);
136-
check(json, serialized);
137-
}
138135
}

src/librustdoc/html/render/write_shared.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub(crate) fn write_shared(
8181
let crate_name_json = SortedJson::serialize(crate_name); // "rand"
8282
let external_crates = hack_get_external_crate_names(cx)?;
8383
let info = CrateInfo {
84+
version: CrateInfoVersion::V1,
8485
src_files_js: PartsAndLocations::<SourcesPart>::get(cx, &crate_name_json)?,
8586
search_index_js: PartsAndLocations::<SearchIndexPart>::get(cx, index)?,
8687
all_crates: PartsAndLocations::<AllCratesPart>::get(crate_name_json.clone())?,
@@ -193,9 +194,11 @@ fn write_search_desc(cx: &mut Context<'_>, krate: &Crate, search_desc: &[(usize,
193194
Ok(())
194195
}
195196

197+
196198
/// Written to `crate-info.json`. Contains pre-rendered contents to insert into the CCI template
197199
#[derive(Serialize, Deserialize, Clone, Debug)]
198200
struct CrateInfo {
201+
version: CrateInfoVersion,
199202
src_files_js: PartsAndLocations<SourcesPart>,
200203
search_index_js: PartsAndLocations<SearchIndexPart>,
201204
all_crates: PartsAndLocations<AllCratesPart>,
@@ -215,7 +218,7 @@ impl CrateInfo {
215218
.or_else(|| (&self.type_impl as &dyn Any).downcast_ref())
216219
}
217220

218-
/// read all of the crate info from its location on the filesystem
221+
/// Read all of the crate info from its location on the filesystem
219222
fn read(parts_paths: &[PathToParts]) -> Result<Vec<Self>, Error> {
220223
parts_paths.iter()
221224
.map(|parts_path| {
@@ -228,6 +231,15 @@ impl CrateInfo {
228231
}
229232
}
230233

234+
/// Version for the format of the crate-info.json file.
235+
///
236+
/// This enum should only ever have one variant, representing the current version.
237+
/// Gives pretty good error message about expecting the current version on deserialize.
238+
///
239+
/// Must be incremented (V2, V3, etc.) upon any changes to the search index or CrateInfo.
240+
#[derive(Serialize, Deserialize, Clone, Debug)]
241+
enum CrateInfoVersion { V1 }
242+
231243
/// Paths (relative to the doc root) and their pre-merge contents
232244
#[derive(Serialize, Deserialize, Debug, Clone)]
233245
#[serde(transparent)]
@@ -324,7 +336,7 @@ fn hack_get_external_crate_names(cx: &Context<'_>) -> Result<Vec<String>, Error>
324336
return Ok(Vec::default());
325337
};
326338
// this is only run once so it's fine not to cache it
327-
// dot_matches_new_line false: all crates on same line. greedy: match last bracket
339+
// !dot_matches_new_line: all crates on same line. greedy: match last bracket
328340
let regex = Regex::new(r"\[.*\]").unwrap();
329341
let Some(content) = regex.find(&content) else {
330342
return Err(Error::new("could not find crates list in crates.js", path));

0 commit comments

Comments
 (0)