Skip to content

Commit 32fbcd8

Browse files
authored
Use BTreeMap instead of HashMap for deterministic ordering (#12)
* Use BTreeMap instead of HashMap for deterministic ordering * Release 0.2.0
1 parent 030d4b6 commit 32fbcd8

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "jtd"
3-
version = "0.1.4"
3+
version = "0.2.0"
44
description = "A Rust implementation of JSON Type Definition"
55
authors = ["JSON Type Definition Contributors"]
66
edition = "2018"

src/form.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::schema::Schema;
2-
use std::collections::HashMap;
3-
use std::collections::HashSet;
2+
use std::collections::BTreeMap;
3+
use std::collections::BTreeSet;
44
use std::str::FromStr;
55

66
#[derive(Clone, Debug, PartialEq)]
@@ -77,7 +77,7 @@ impl FromStr for TypeValue {
7777
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
7878
pub struct Enum {
7979
pub nullable: bool,
80-
pub values: HashSet<String>,
80+
pub values: BTreeSet<String>,
8181
}
8282

8383
#[derive(Clone, Debug, PartialEq)]
@@ -91,8 +91,8 @@ pub struct Elements {
9191
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
9292
pub struct Properties {
9393
pub nullable: bool,
94-
pub required: HashMap<String, Schema>,
95-
pub optional: HashMap<String, Schema>,
94+
pub required: BTreeMap<String, Schema>,
95+
pub optional: BTreeMap<String, Schema>,
9696
pub additional: bool,
9797
pub has_required: bool,
9898
}
@@ -109,7 +109,7 @@ pub struct Values {
109109
pub struct Discriminator {
110110
pub nullable: bool,
111111
pub discriminator: String,
112-
pub mapping: HashMap<String, Schema>,
112+
pub mapping: BTreeMap<String, Schema>,
113113
}
114114

115115
#[cfg(test)]

src/schema.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::form;
22
use crate::serde;
33
use serde_json::Value;
4-
use std::collections::{HashMap, HashSet};
4+
use std::collections::{BTreeMap, BTreeSet};
55
use std::convert::{TryFrom, TryInto};
66

77
#[derive(Clone, Debug, Default, PartialEq)]
88
pub struct Schema {
9-
pub definitions: HashMap<String, Schema>,
9+
pub definitions: BTreeMap<String, Schema>,
1010
pub form: form::Form,
11-
pub metadata: HashMap<String, Value>,
11+
pub metadata: BTreeMap<String, Value>,
1212
}
1313

1414
#[cfg(feature = "fuzz")]
@@ -23,7 +23,7 @@ impl arbitrary::Arbitrary for Schema {
2323
// manipulating arbitrary JSON values.
2424
//
2525
// So we'll always have metadata be None.
26-
metadata: HashMap::new(),
26+
metadata: BTreeMap::new(),
2727
})
2828
}
2929
}
@@ -205,7 +205,7 @@ impl TryFrom<serde::Schema> for Schema {
205205
return Err(SerdeConvertError::InvalidForm);
206206
}
207207

208-
let mut definitions = HashMap::new();
208+
let mut definitions = BTreeMap::new();
209209
for (name, sub_schema) in schema.definitions.unwrap_or_default() {
210210
definitions.insert(name, sub_schema.try_into()?);
211211
}
@@ -235,7 +235,7 @@ impl TryFrom<serde::Schema> for Schema {
235235
}
236236

237237
if let Some(enum_) = schema.enum_ {
238-
let mut values = HashSet::new();
238+
let mut values = BTreeSet::new();
239239
for val in enum_ {
240240
if values.contains(&val) {
241241
return Err(SerdeConvertError::DuplicatedEnumValue(val));
@@ -268,12 +268,12 @@ impl TryFrom<serde::Schema> for Schema {
268268
if schema.properties.is_some() || schema.optional_properties.is_some() {
269269
let has_required = schema.properties.is_some();
270270

271-
let mut required = HashMap::new();
271+
let mut required = BTreeMap::new();
272272
for (name, sub_schema) in schema.properties.unwrap_or_default() {
273273
required.insert(name, sub_schema.try_into()?);
274274
}
275275

276-
let mut optional = HashMap::new();
276+
let mut optional = BTreeMap::new();
277277
for (name, sub_schema) in schema.optional_properties.unwrap_or_default() {
278278
optional.insert(name, sub_schema.try_into()?);
279279
}
@@ -303,7 +303,7 @@ impl TryFrom<serde::Schema> for Schema {
303303
}
304304

305305
if let Some(discriminator) = schema.discriminator {
306-
let mut mapping = HashMap::new();
306+
let mut mapping = BTreeMap::new();
307307
for (name, sub_schema) in schema.mapping.unwrap() {
308308
mapping.insert(name, sub_schema.try_into()?);
309309
}
@@ -605,7 +605,7 @@ mod tests {
605605
required: vec![("foo".to_owned(), Default::default())]
606606
.into_iter()
607607
.collect(),
608-
optional: HashMap::new(),
608+
optional: BTreeMap::new(),
609609
additional: false,
610610
has_required: true,
611611
}),
@@ -627,7 +627,7 @@ mod tests {
627627
Ok(Schema {
628628
form: form::Form::Properties(form::Properties {
629629
nullable: false,
630-
required: HashMap::new(),
630+
required: BTreeMap::new(),
631631
optional: vec![("foo".to_owned(), Default::default())]
632632
.into_iter()
633633
.collect(),
@@ -909,7 +909,7 @@ mod tests {
909909

910910
#[test]
911911
fn spec_invalid_schemas_suite() {
912-
let test_cases: HashMap<String, Value> = serde_json::from_str(include_str!(
912+
let test_cases: BTreeMap<String, Value> = serde_json::from_str(include_str!(
913913
"../json-typedef-spec/tests/invalid_schemas.json"
914914
))
915915
.unwrap();

src/serde.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::schema;
22
use serde::{Deserialize, Serialize};
33
use serde_json::Value;
4-
use std::collections::HashMap;
4+
use std::collections::BTreeMap;
55

66
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
77
#[serde(rename_all = "camelCase")]
88
#[serde(deny_unknown_fields)]
99
pub struct Schema {
1010
#[serde(skip_serializing_if = "Option::is_none")]
11-
pub definitions: Option<HashMap<String, Schema>>,
11+
pub definitions: Option<BTreeMap<String, Schema>>,
1212

1313
#[serde(skip_serializing_if = "Option::is_none")]
1414
pub nullable: Option<bool>,
@@ -26,10 +26,10 @@ pub struct Schema {
2626
pub elements: Option<Box<Schema>>,
2727

2828
#[serde(skip_serializing_if = "Option::is_none")]
29-
pub properties: Option<HashMap<String, Schema>>,
29+
pub properties: Option<BTreeMap<String, Schema>>,
3030

3131
#[serde(skip_serializing_if = "Option::is_none")]
32-
pub optional_properties: Option<HashMap<String, Schema>>,
32+
pub optional_properties: Option<BTreeMap<String, Schema>>,
3333

3434
#[serde(skip_serializing_if = "Option::is_none")]
3535
pub additional_properties: Option<bool>,
@@ -41,10 +41,10 @@ pub struct Schema {
4141
pub discriminator: Option<String>,
4242

4343
#[serde(skip_serializing_if = "Option::is_none")]
44-
pub mapping: Option<HashMap<String, Schema>>,
44+
pub mapping: Option<BTreeMap<String, Schema>>,
4545

4646
#[serde(skip_serializing_if = "Option::is_none")]
47-
pub metadata: Option<HashMap<String, Value>>,
47+
pub metadata: Option<BTreeMap<String, Value>>,
4848
}
4949

5050
#[cfg(feature = "fuzz")]

src/validator.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ mod tests {
333333
use crate::SerdeSchema;
334334
use serde::{Deserialize, Serialize};
335335
use serde_json::json;
336-
use std::collections::{HashMap, HashSet};
336+
use std::collections::{BTreeMap, BTreeSet};
337337
use std::convert::TryInto;
338338

339339
#[test]
@@ -391,14 +391,14 @@ mod tests {
391391
errors: Vec<TestCaseError>,
392392
}
393393

394-
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
394+
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug)]
395395
#[serde(rename_all = "camelCase")]
396396
struct TestCaseError {
397397
instance_path: Vec<String>,
398398
schema_path: Vec<String>,
399399
}
400400

401-
let test_cases: HashMap<String, TestCase> =
401+
let test_cases: BTreeMap<String, TestCase> =
402402
serde_json::from_str(include_str!("../json-typedef-spec/tests/validation.json"))
403403
.unwrap();
404404

@@ -413,7 +413,7 @@ mod tests {
413413
max_errors: None,
414414
};
415415

416-
let errors: HashSet<_> = validator
416+
let errors: BTreeSet<_> = validator
417417
.validate(&schema, &test_case.instance)
418418
.expect(&format!("validating: {}", name))
419419
.into_iter()
@@ -424,7 +424,7 @@ mod tests {
424424
.collect();
425425

426426
assert_eq!(
427-
test_case.errors.into_iter().collect::<HashSet<_>>(),
427+
test_case.errors.into_iter().collect::<BTreeSet<_>>(),
428428
errors,
429429
"wrong set of errors returned for test case: {}",
430430
name

0 commit comments

Comments
 (0)