aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / serde_json / tests / regression / issue795.rs
| 1 | #![allow(clippy::assertions_on_result_states)] |
| 2 | |
| 3 | use serde::de::{ |
| 4 | Deserialize, Deserializer, EnumAccess, IgnoredAny, MapAccess, VariantAccess, Visitor, |
| 5 | }; |
| 6 | use serde_json::json; |
| 7 | use std::fmt; |
| 8 | |
| 9 | #[derive(Debug)] |
| 10 | pub enum Enum { |
| 11 | Variant { |
| 12 | #[allow(dead_code)] |
| 13 | x: u8, |
| 14 | }, |
| 15 | } |
| 16 | |
| 17 | impl<'de> Deserialize<'de> for Enum { |
| 18 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 19 | where |
| 20 | D: Deserializer<'de>, |
| 21 | { |
| 22 | struct EnumVisitor; |
| 23 | |
| 24 | impl<'de> Visitor<'de> for EnumVisitor { |
| 25 | type Value = Enum; |
| 26 | |
| 27 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 28 | formatter.write_str("enum Enum") |
| 29 | } |
| 30 | |
| 31 | fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> |
| 32 | where |
| 33 | A: EnumAccess<'de>, |
| 34 | { |
| 35 | let (IgnoredAny, variant) = data.variant()?; |
| 36 | variant.struct_variant(&["x"], self) |
| 37 | } |
| 38 | |
| 39 | fn visit_map<A>(self, mut data: A) -> Result<Self::Value, A::Error> |
| 40 | where |
| 41 | A: MapAccess<'de>, |
| 42 | { |
| 43 | let mut x = 0; |
| 44 | if let Some((IgnoredAny, value)) = data.next_entry()? { |
| 45 | x = value; |
| 46 | } |
| 47 | Ok(Enum::Variant { x }) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | deserializer.deserialize_enum("Enum", &["Variant"], EnumVisitor) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn test() { |
| 57 | let s = r#" {"Variant":{"x":0,"y":0}} "#; |
| 58 | assert!(serde_json::from_str::<Enum>(s).is_err()); |
| 59 | |
| 60 | let j = json!({"Variant":{"x":0,"y":0}}); |
| 61 | assert!(serde_json::from_value::<Enum>(j).is_err()); |
| 62 | } |