Skip to content

Commit 4b418cd

Browse files
committed
rename sortedjson -> orderedjson
1 parent 67663fc commit 4b418cd

File tree

9 files changed

+228
-193
lines changed

9 files changed

+228
-193
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
pub(crate) 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

+29-27
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,74 @@
1+
use std::borrow::Borrow;
2+
use std::fmt;
3+
14
use itertools::Itertools as _;
25
use serde::{Deserialize, Serialize};
36
use serde_json::Value;
4-
use std::borrow::Borrow;
5-
use std::fmt;
67

78
/// Prerenedered json.
89
///
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-
///
1410
/// Both the Display and serde_json::to_string implementations write the serialized json
1511
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
1612
#[serde(from = "Value")]
1713
#[serde(into = "Value")]
18-
pub(crate) struct SortedJson(String);
14+
pub(crate) struct OrderedJson(String);
1915

20-
impl SortedJson {
16+
impl OrderedJson {
2117
/// 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())
18+
pub(crate) fn serialize<T: Serialize>(item: T) -> Result<Self, serde_json::Error> {
19+
Ok(OrderedJson(serde_json::to_string(&item)?))
2420
}
2521

2622
/// Serializes and sorts
27-
pub(crate) fn array<T: Borrow<SortedJson>, I: IntoIterator<Item = T>>(items: I) -> Self {
23+
pub(crate) fn array_sorted<T: Borrow<OrderedJson>, I: IntoIterator<Item = T>>(
24+
items: I,
25+
) -> Self {
2826
let items = items
2927
.into_iter()
3028
.sorted_unstable_by(|a, b| a.borrow().cmp(&b.borrow()))
3129
.format_with(",", |item, f| f(item.borrow()));
32-
SortedJson(format!("[{}]", items))
30+
OrderedJson(format!("[{}]", items))
3331
}
3432

35-
pub(crate) fn array_unsorted<T: Borrow<SortedJson>, I: IntoIterator<Item = T>>(
33+
pub(crate) fn array_unsorted<T: Borrow<OrderedJson>, I: IntoIterator<Item = T>>(
3634
items: I,
3735
) -> Self {
3836
let items = items.into_iter().format_with(",", |item, f| f(item.borrow()));
39-
SortedJson(format!("[{items}]"))
37+
OrderedJson(format!("[{items}]"))
4038
}
4139
}
4240

43-
impl fmt::Display for SortedJson {
41+
impl fmt::Display for OrderedJson {
4442
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45-
write!(f, "{}", self.0)
43+
self.0.fmt(f)
4644
}
4745
}
4846

49-
impl From<Value> for SortedJson {
47+
impl From<Value> for OrderedJson {
5048
fn from(value: Value) -> Self {
51-
SortedJson(serde_json::to_string(&value).unwrap())
49+
let serialized =
50+
serde_json::to_string(&value).expect("Serializing a Value to String should never fail");
51+
OrderedJson(serialized)
5252
}
5353
}
5454

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

6161
/// For use in JSON.parse('{...}').
6262
///
63-
/// JSON.parse supposedly loads faster than raw JS source,
63+
/// Assumes we are going to be wrapped in single quoted strings.
64+
///
65+
/// JSON.parse loads faster than raw JS source,
6466
/// so this is used for large objects.
6567
#[derive(Debug, Clone, Serialize, Deserialize)]
66-
pub(crate) struct EscapedJson(SortedJson);
68+
pub(crate) struct EscapedJson(OrderedJson);
6769

68-
impl From<SortedJson> for EscapedJson {
69-
fn from(json: SortedJson) -> Self {
70+
impl From<OrderedJson> for EscapedJson {
71+
fn from(json: OrderedJson) -> Self {
7072
EscapedJson(json)
7173
}
7274
}
@@ -77,7 +79,7 @@ impl fmt::Display for EscapedJson {
7779
// for JSON content.
7880
// We need to escape double quotes for the JSON
7981
let json = self.0.0.replace('\\', r"\\").replace('\'', r"\'").replace("\\\"", "\\\\\"");
80-
write!(f, "{}", json)
82+
json.fmt(f)
8183
}
8284
}
8385

Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
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);
1818
}
1919

20-
// Test this basic are needed because we are testing that our Display impl + serialize impl don't
21-
// nest everything in extra level of string. We also are testing round trip.
20+
// Make sure there is no extra level of string, plus number of escapes.
2221
#[test]
2322
fn escape_json_number() {
24-
let json = SortedJson::serialize(3);
23+
let json = OrderedJson::serialize(3).unwrap();
2524
let json = EscapedJson::from(json);
2625
assert_eq!(format!("{json}"), "3");
2726
}
2827

2928
#[test]
3029
fn escape_json_single_quote() {
31-
let json = SortedJson::serialize("he's");
30+
let json = OrderedJson::serialize("he's").unwrap();
3231
let json = EscapedJson::from(json);
3332
assert_eq!(format!("{json}"), r#""he\'s""#);
3433
}
3534

3635
#[test]
3736
fn escape_json_array() {
38-
let json = SortedJson::serialize([1, 2, 3]);
37+
let json = OrderedJson::serialize([1, 2, 3]).unwrap();
3938
let json = EscapedJson::from(json);
4039
assert_eq!(format!("{json}"), r#"[1,2,3]"#);
4140
}
4241

4342
#[test]
4443
fn escape_json_string() {
45-
let json = SortedJson::serialize(r#"he"llo"#);
44+
let json = OrderedJson::serialize(r#"he"llo"#).unwrap();
4645
let json = EscapedJson::from(json);
4746
assert_eq!(format!("{json}"), r#""he\\\"llo""#);
4847
}
4948

5049
#[test]
5150
fn escape_json_string_escaped() {
52-
let json = SortedJson::serialize(r#"he\"llo"#);
51+
let json = OrderedJson::serialize(r#"he\"llo"#).unwrap();
5352
let json = EscapedJson::from(json);
5453
assert_eq!(format!("{json}"), r#""he\\\\\\\"llo""#);
5554
}
5655

5756
#[test]
5857
fn escape_json_string_escaped_escaped() {
59-
let json = SortedJson::serialize(r#"he\\"llo"#);
58+
let json = OrderedJson::serialize(r#"he\\"llo"#).unwrap();
6059
let json = EscapedJson::from(json);
6160
assert_eq!(format!("{json}"), r#""he\\\\\\\\\\\"llo""#);
6261
}
6362

63+
// 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
@@ -18,7 +18,7 @@ use crate::formats::cache::{Cache, OrphanImplItem};
1818
use crate::formats::item_type::ItemType;
1919
use crate::html::format::join_with_double_colon;
2020
use crate::html::markdown::short_markdown_summary;
21-
use crate::html::render::sorted_json::SortedJson;
21+
use crate::html::render::ordered_json::OrderedJson;
2222
use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, RenderTypeId};
2323

2424
/// The serialized search description sharded version
@@ -47,7 +47,7 @@ use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, Re
4747
/// [2]: https://en.wikipedia.org/wiki/Sliding_window_protocol#Basic_concept
4848
/// [3]: https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/description-tcp-features
4949
pub(crate) struct SerializedSearchIndex {
50-
pub(crate) index: SortedJson,
50+
pub(crate) index: OrderedJson,
5151
pub(crate) desc: Vec<(usize, String)>,
5252
}
5353

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

0 commit comments

Comments
 (0)