mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
Reject colliding project extension fields
This commit is contained in:
@@ -36,7 +36,7 @@ pub enum Error {
|
||||
|
||||
/// Neutral top-level project schema. Nested app-domain values and unknown fields remain JSON
|
||||
/// so they round-trip without requiring TextureCraft's in-memory model.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
||||
pub struct ProjectDocument {
|
||||
pub format_version: u32,
|
||||
pub name: String,
|
||||
@@ -52,6 +52,30 @@ pub struct ProjectDocument {
|
||||
pub extra: BTreeMap<String, Value>,
|
||||
}
|
||||
|
||||
impl Serialize for ProjectDocument {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
use serde::ser::{Error as _, SerializeMap as _};
|
||||
const RESERVED: &[&str] = &["format_version", "name", "mesh", "normal_format", "texture_sets", "layers", "next_id", "display", "cameras"];
|
||||
if self.extra.keys().any(|key| RESERVED.contains(&key.as_str())) {
|
||||
return Err(S::Error::custom("an extra project field duplicates a known field"));
|
||||
}
|
||||
let mut map = serializer.serialize_map(Some(9 + self.extra.len()))?;
|
||||
map.serialize_entry("format_version", &self.format_version)?;
|
||||
map.serialize_entry("name", &self.name)?;
|
||||
map.serialize_entry("mesh", &self.mesh)?;
|
||||
map.serialize_entry("normal_format", &self.normal_format)?;
|
||||
map.serialize_entry("texture_sets", &self.texture_sets)?;
|
||||
map.serialize_entry("layers", &self.layers)?;
|
||||
map.serialize_entry("next_id", &self.next_id)?;
|
||||
map.serialize_entry("display", &self.display)?;
|
||||
map.serialize_entry("cameras", &self.cameras)?;
|
||||
for (key, value) in &self.extra {
|
||||
map.serialize_entry(key, value)?;
|
||||
}
|
||||
map.end()
|
||||
}
|
||||
}
|
||||
|
||||
/// Read only the version before an application applies its own recovery policy. Serde's
|
||||
/// structured parse rejects a duplicate or invalid version field, like the project reader.
|
||||
pub fn project_version(bytes: &[u8]) -> Result<u32, serde_json::Error> {
|
||||
@@ -286,7 +310,10 @@ mod tests {
|
||||
let document: ProjectDocument = serde_json::from_slice(&bytes).unwrap();
|
||||
let mut expected = json;
|
||||
expected["cameras"] = serde_json::json!([]);
|
||||
assert_eq!(serde_json::to_value(document).unwrap(), expected);
|
||||
assert_eq!(serde_json::to_value(&document).unwrap(), expected);
|
||||
let mut duplicate = document;
|
||||
duplicate.extra.insert("mesh".into(), serde_json::json!("other.obj"));
|
||||
assert!(serde_json::to_vec(&duplicate).is_err());
|
||||
assert!(project_version(br#"{"format_version":1,"format_version":2}"#).is_err());
|
||||
assert!(project_version(br#"{"format_version":-1}"#).is_err());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user