Skip to content

Commit b6bd191

Browse files
committed
per djkoloski's review
1 parent d3876e1 commit b6bd191

File tree

9 files changed

+214
-179
lines changed

9 files changed

+214
-179
lines changed

src/librustdoc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ minifier = "0.3.0"
1616
pulldown-cmark-old = { version = "0.9.6", package = "pulldown-cmark", default-features = false }
1717
regex = "1"
1818
rustdoc-json-types = { path = "../rustdoc-json-types" }
19-
serde_json = { version = "1.0", features = ["preserve_order"] }
19+
serde_json = "1.0"
2020
serde = { version = "1.0", features = ["derive"] }
2121
smallvec = "1.8.1"
2222
tempfile = "3"

src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ pub(crate) mod search_index;
2929
mod tests;
3030

3131
mod context;
32+
mod ordered_json;
3233
mod print_item;
3334
mod sidebar;
34-
mod sorted_json;
3535
mod sorted_template;
3636
mod span_map;
3737
mod type_layout;

src/librustdoc/html/render/sorted_json.rs renamed to src/librustdoc/html/render/ordered_json.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,54 @@ use std::fmt;
66

77
/// Prerenedered json.
88
///
9-
/// Arrays are sorted by their stringified entries, and objects are sorted by their stringified
10-
/// keys.
11-
///
12-
/// Must use serde_json with the preserve_order feature.
13-
///
149
/// Both the Display and serde_json::to_string implementations write the serialized json
1510
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
1611
#[serde(from = "Value")]
1712
#[serde(into = "Value")]
18-
pub(crate) struct SortedJson(String);
13+
pub(crate) struct OrderedJson(String);
1914

20-
impl SortedJson {
15+
impl OrderedJson {
2116
/// If you pass in an array, it will not be sorted.
22-
pub(crate) fn serialize<T: Serialize>(item: T) -> Self {
23-
SortedJson(serde_json::to_string(&item).unwrap())
17+
pub(crate) fn serialize<T: Serialize>(item: T) -> Result<Self, serde_json::Error> {
18+
Ok(OrderedJson(serde_json::to_string(&item)?))
2419
}
2520

2621
/// Serializes and sorts
27-
pub(crate) fn array<T: Borrow<SortedJson>, I: IntoIterator<Item = T>>(items: I) -> Self {
22+
pub(crate) fn array_sorted<T: Borrow<OrderedJson>, I: IntoIterator<Item = T>>(
23+
items: I,
24+
) -> Self {
2825
let items = items
2926
.into_iter()
3027
.sorted_unstable_by(|a, b| a.borrow().cmp(&b.borrow()))
3128
.format_with(",", |item, f| f(item.borrow()));
32-
SortedJson(format!("[{}]", items))
29+
OrderedJson(format!("[{}]", items))
3330
}
3431

35-
pub(crate) fn array_unsorted<T: Borrow<SortedJson>, I: IntoIterator<Item = T>>(
32+
pub(crate) fn array_unsorted<T: Borrow<OrderedJson>, I: IntoIterator<Item = T>>(
3633
items: I,
3734
) -> Self {
3835
let items = items.into_iter().format_with(",", |item, f| f(item.borrow()));
39-
SortedJson(format!("[{items}]"))
36+
OrderedJson(format!("[{items}]"))
4037
}
4138
}
4239

43-
impl fmt::Display for SortedJson {
40+
impl fmt::Display for OrderedJson {
4441
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45-
write!(f, "{}", self.0)
42+
self.0.fmt(f)
4643
}
4744
}
4845

49-
impl From<Value> for SortedJson {
46+
impl From<Value> for OrderedJson {
5047
fn from(value: Value) -> Self {
51-
SortedJson(serde_json::to_string(&value).unwrap())
48+
let serialized =
49+
serde_json::to_string(&value).expect("Serializing a Value to String should never fail");
50+
OrderedJson(serialized)
5251
}
5352
}
5453

55-
impl From<SortedJson> for Value {
56-
fn from(json: SortedJson) -> Self {
57-
serde_json::from_str(&json.0).unwrap()
54+
impl From<OrderedJson> for Value {
55+
fn from(json: OrderedJson) -> Self {
56+
serde_json::from_str(&json.0).expect("OrderedJson should always store valid JSON")
5857
}
5958
}
6059

@@ -65,10 +64,10 @@ impl From<SortedJson> for Value {
6564
/// JSON.parse loads faster than raw JS source,
6665
/// so this is used for large objects.
6766
#[derive(Debug, Clone, Serialize, Deserialize)]
68-
pub(crate) struct EscapedJson(SortedJson);
67+
pub(crate) struct EscapedJson(OrderedJson);
6968

70-
impl From<SortedJson> for EscapedJson {
71-
fn from(json: SortedJson) -> Self {
69+
impl From<OrderedJson> for EscapedJson {
70+
fn from(json: OrderedJson) -> Self {
7271
EscapedJson(json)
7372
}
7473
}
@@ -79,7 +78,7 @@ impl fmt::Display for EscapedJson {
7978
// for JSON content.
8079
// We need to escape double quotes for the JSON
8180
let json = self.0.0.replace('\\', r"\\").replace('\'', r"\'").replace("\\\"", "\\\\\"");
82-
write!(f, "{}", json)
81+
json.fmt(f)
8382
}
8483
}
8584

Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
use super::super::sorted_json::*;
1+
use super::super::ordered_json::*;
22

3-
fn check(json: SortedJson, serialized: &str) {
3+
fn check(json: OrderedJson, serialized: &str) {
44
assert_eq!(json.to_string(), serialized);
55
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
66

77
let json = json.to_string();
8-
let json: SortedJson = serde_json::from_str(&json).unwrap();
8+
let json: OrderedJson = serde_json::from_str(&json).unwrap();
99

1010
assert_eq!(json.to_string(), serialized);
1111
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
1212

1313
let json = serde_json::to_string(&json).unwrap();
14-
let json: SortedJson = serde_json::from_str(&json).unwrap();
14+
let json: OrderedJson = serde_json::from_str(&json).unwrap();
1515

1616
assert_eq!(json.to_string(), serialized);
1717
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
@@ -20,71 +20,71 @@ fn check(json: SortedJson, serialized: &str) {
2020
// Make sure there is no extra level of string, plus number of escapes.
2121
#[test]
2222
fn escape_json_number() {
23-
let json = SortedJson::serialize(3);
23+
let json = OrderedJson::serialize(3).unwrap();
2424
let json = EscapedJson::from(json);
2525
assert_eq!(format!("{json}"), "3");
2626
}
2727

2828
#[test]
2929
fn escape_json_single_quote() {
30-
let json = SortedJson::serialize("he's");
30+
let json = OrderedJson::serialize("he's").unwrap();
3131
let json = EscapedJson::from(json);
3232
assert_eq!(format!("{json}"), r#""he\'s""#);
3333
}
3434

3535
#[test]
3636
fn escape_json_array() {
37-
let json = SortedJson::serialize([1, 2, 3]);
37+
let json = OrderedJson::serialize([1, 2, 3]).unwrap();
3838
let json = EscapedJson::from(json);
3939
assert_eq!(format!("{json}"), r#"[1,2,3]"#);
4040
}
4141

4242
#[test]
4343
fn escape_json_string() {
44-
let json = SortedJson::serialize(r#"he"llo"#);
44+
let json = OrderedJson::serialize(r#"he"llo"#).unwrap();
4545
let json = EscapedJson::from(json);
4646
assert_eq!(format!("{json}"), r#""he\\\"llo""#);
4747
}
4848

4949
#[test]
5050
fn escape_json_string_escaped() {
51-
let json = SortedJson::serialize(r#"he\"llo"#);
51+
let json = OrderedJson::serialize(r#"he\"llo"#).unwrap();
5252
let json = EscapedJson::from(json);
5353
assert_eq!(format!("{json}"), r#""he\\\\\\\"llo""#);
5454
}
5555

5656
#[test]
5757
fn escape_json_string_escaped_escaped() {
58-
let json = SortedJson::serialize(r#"he\\"llo"#);
58+
let json = OrderedJson::serialize(r#"he\\"llo"#).unwrap();
5959
let json = EscapedJson::from(json);
6060
assert_eq!(format!("{json}"), r#""he\\\\\\\\\\\"llo""#);
6161
}
6262

6363
// Testing round trip + making sure there is no extra level of string
6464
#[test]
6565
fn number() {
66-
let json = SortedJson::serialize(3);
66+
let json = OrderedJson::serialize(3).unwrap();
6767
let serialized = "3";
6868
check(json, serialized);
6969
}
7070

7171
#[test]
7272
fn boolean() {
73-
let json = SortedJson::serialize(true);
73+
let json = OrderedJson::serialize(true).unwrap();
7474
let serialized = "true";
7575
check(json, serialized);
7676
}
7777

7878
#[test]
7979
fn string() {
80-
let json = SortedJson::serialize("he\"llo");
80+
let json = OrderedJson::serialize("he\"llo").unwrap();
8181
let serialized = r#""he\"llo""#;
8282
check(json, serialized);
8383
}
8484

8585
#[test]
8686
fn serialize_array() {
87-
let json = SortedJson::serialize([3, 1, 2]);
87+
let json = OrderedJson::serialize([3, 1, 2]).unwrap();
8888
let serialized = "[3,1,2]";
8989
check(json, serialized);
9090
}
@@ -93,18 +93,19 @@ fn serialize_array() {
9393
fn sorted_array() {
9494
let items = ["c", "a", "b"];
9595
let serialized = r#"["a","b","c"]"#;
96-
let items: Vec<SortedJson> = items.into_iter().map(SortedJson::serialize).collect();
97-
let json = SortedJson::array(items);
96+
let items: Vec<OrderedJson> =
97+
items.into_iter().map(OrderedJson::serialize).collect::<Result<Vec<_>, _>>().unwrap();
98+
let json = OrderedJson::array_sorted(items);
9899
check(json, serialized);
99100
}
100101

101102
#[test]
102103
fn nested_array() {
103-
let a = SortedJson::serialize(3);
104-
let b = SortedJson::serialize(2);
105-
let c = SortedJson::serialize(1);
106-
let d = SortedJson::serialize([1, 3, 2]);
107-
let json = SortedJson::array([a, b, c, d]);
104+
let a = OrderedJson::serialize(3).unwrap();
105+
let b = OrderedJson::serialize(2).unwrap();
106+
let c = OrderedJson::serialize(1).unwrap();
107+
let d = OrderedJson::serialize([1, 3, 2]).unwrap();
108+
let json = OrderedJson::array_sorted([a, b, c, d]);
108109
let serialized = r#"[1,2,3,[1,3,2]]"#;
109110
check(json, serialized);
110111
}
@@ -113,7 +114,8 @@ fn nested_array() {
113114
fn array_unsorted() {
114115
let items = ["c", "a", "b"];
115116
let serialized = r#"["c","a","b"]"#;
116-
let items: Vec<SortedJson> = items.into_iter().map(SortedJson::serialize).collect();
117-
let json = SortedJson::array_unsorted(items);
117+
let items: Vec<OrderedJson> =
118+
items.into_iter().map(OrderedJson::serialize).collect::<Result<Vec<_>, _>>().unwrap();
119+
let json = OrderedJson::array_unsorted(items);
118120
check(json, serialized);
119121
}

src/librustdoc/html/render/search_index.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ 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::sorted_json::SortedJson;
20+
use crate::html::render::ordered_json::OrderedJson;
2121
use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, RenderTypeId};
2222

2323
use encode::{bitmap_to_string, write_vlqhex_to_string};
@@ -48,7 +48,7 @@ use encode::{bitmap_to_string, write_vlqhex_to_string};
4848
/// [2]: https://en.wikipedia.org/wiki/Sliding_window_protocol#Basic_concept
4949
/// [3]: https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/description-tcp-features
5050
pub(crate) struct SerializedSearchIndex {
51-
pub(crate) index: SortedJson,
51+
pub(crate) index: OrderedJson,
5252
pub(crate) desc: Vec<(usize, String)>,
5353
}
5454

@@ -694,9 +694,9 @@ pub(crate) fn build_index<'tcx>(
694694
desc_index,
695695
empty_desc,
696696
};
697-
let index = SortedJson::array_unsorted([
698-
SortedJson::serialize(crate_name.as_str()),
699-
SortedJson::serialize(data),
697+
let index = OrderedJson::array_unsorted([
698+
OrderedJson::serialize(crate_name.as_str()).unwrap(),
699+
OrderedJson::serialize(data).unwrap(),
700700
]);
701701
SerializedSearchIndex { index, desc }
702702
}

0 commit comments

Comments
 (0)