Add support for optional enum field
Some checks failed
Build / build (push) Has been cancelled

This commit is contained in:
2025-10-30 16:03:58 +01:00
parent 477254f27c
commit ce1fb4a11e
2 changed files with 17 additions and 3 deletions

View File

@@ -164,7 +164,18 @@ pub fn deserialize_internal(
})*
_ => (),
}
self.__map.take().ok_or(miniserde::Error)?.finish()?;
let mut map = match self.__map.take() {
Some(map) => map,
None => match tag.as_str() {
#(
#struct_variant_names => <#struct_names as miniserde::Deserialize>::begin(
unsafe {&mut *(&mut self.#struct_names as *mut #ex::Option<#struct_names>)}
).map()?,
)*
_ => return #ex::Err(miniserde::Error),
},
};
map.finish()?;
match tag.as_str() {
#(#struct_variant_names => {
self.__out.replace(self.#struct_names.take().ok_or(miniserde::Error)?.as_enum());

View File

@@ -51,10 +51,13 @@ fn test_internal() {
x: i32,
},
D,
E {
value: Option<i32>,
},
}
use Internal::*;
let example = r#"[{"type":"renamedB"},{"type":"C","x":2},{"type":"D"}]"#;
let example = r#"[{"type":"renamedB"},{"type":"C","x":2},{"type":"D"},{"type":"E"},{"type":"E","value":5}]"#;
let actual: Vec<Internal> = json::from_str(example).unwrap();
let expected = [B, C { x: 2 }, D];
let expected = [B, C { x: 2 }, D, E { value: None }, E { value: Some(5) }];
assert_eq!(actual, expected);
}