mirror of
https://github.com/azalea-rs/simdnbt.git
synced 2025-08-02 23:44:40 +00:00
fix failing compilation when deny_unknown_fields is set
This commit is contained in:
parent
0a8149e459
commit
54710304ad
3 changed files with 38 additions and 3 deletions
|
@ -55,7 +55,7 @@ pub fn deserialize_derive(input: proc_macro::TokenStream) -> proc_macro::TokenSt
|
|||
let extra_checks = if struct_attrs.deny_unknown_fields {
|
||||
quote! {
|
||||
if !nbt.is_empty() {
|
||||
return Err(simdnbt::DeserializeError::UnknownField(nbt.keys().next().unwrap().clone()));
|
||||
return Err(simdnbt::DeserializeError::UnknownField(nbt.keys().next().unwrap().to_string()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
35
simdnbt/examples/registry.rs
Normal file
35
simdnbt/examples/registry.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use std::{collections::HashMap, io::Cursor};
|
||||
|
||||
use simdnbt::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[simdnbt(deny_unknown_fields)]
|
||||
pub struct TrimMaterialElement {
|
||||
pub asset_name: String,
|
||||
pub item_model_index: f32,
|
||||
pub override_armor_materials: HashMap<String, String>,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let original = TrimMaterialElement {
|
||||
asset_name: "asset name".to_string(),
|
||||
item_model_index: 0.0,
|
||||
override_armor_materials: HashMap::from_iter(vec![
|
||||
("asdf".into(), "fdsa".into()),
|
||||
("dsfgdgh".into(), "fgjrtiu".into()),
|
||||
]),
|
||||
description: Some("description".to_string()),
|
||||
};
|
||||
|
||||
let nbt = original.clone().to_nbt();
|
||||
let mut buf = Vec::new();
|
||||
nbt.write(&mut buf);
|
||||
|
||||
let nbt = simdnbt::borrow::read(&mut Cursor::new(&buf[..]))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let rewritten = TrimMaterialElement::from_nbt(&nbt).unwrap();
|
||||
|
||||
assert_eq!(original, rewritten);
|
||||
}
|
|
@ -20,6 +20,6 @@ pub enum DeserializeError {
|
|||
MissingField,
|
||||
#[error("Mismatched type for {0}")]
|
||||
MismatchedFieldType(String),
|
||||
#[error("Unknown fields {0:?}")]
|
||||
UnknownField(Vec<String>),
|
||||
#[error("Unknown field {0:?}")]
|
||||
UnknownField(String),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue