sort table serialize

This commit is contained in:
Robin Appelman 2023-12-15 16:12:18 +01:00
commit 8e3f20a298
2 changed files with 18 additions and 6 deletions

View file

@ -1,13 +1,25 @@
use super::{Array, Entry, Statement, Value};
use crate::error::StatementInTableError;
use crate::{Event, Item, Reader, Result};
use serde::Serialize;
use serde::{Serialize, Serializer};
use std::collections::HashMap;
use std::ops::Deref;
/// A table of entries.
#[derive(Clone, PartialEq, Eq, Debug, Serialize)]
pub struct Table(HashMap<String, Entry>);
pub struct Table(#[serde(serialize_with = "ordered_map")] HashMap<String, Entry>);
fn ordered_map<S, K: Ord + Serialize, V: Serialize>(
value: &HashMap<K, V>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use std::collections::BTreeMap;
let ordered: BTreeMap<_, _> = value.iter().collect();
ordered.serialize(serializer)
}
fn insert(map: &mut HashMap<String, Entry>, key: String, value: Entry) {
if !map.contains_key(&key) {

View file

@ -4,13 +4,13 @@ expression: parsed
---
Table({
"LightmappedGeneric": Table(Table({
"$detailblendmode": Value(Value("0")),
"$detailscale": Value(Value("1.9")),
"%keywords": Value(Value("tf")),
"$baseTexture": Value(Value("cp_mountainlab/concrete/concretefloor003")),
"$bumpmap": Value(Value("concrete/concretefloor007b_height-ssbump")),
"$detail": Value(Value("overlays/detail001")),
"$baseTexture": Value(Value("cp_mountainlab/concrete/concretefloor003")),
"$detailblendfactor": Value(Value("1")),
"$detailblendmode": Value(Value("0")),
"$detailscale": Value(Value("1.9")),
"$ssbump": Value(Value("1")),
"%keywords": Value(Value("tf")),
})),
})