1 #![allow(clippy::wildcard_imports)] 2 3 use serde_repr::{Deserialize_repr, Serialize_repr}; 4 5 mod small_prime { 6 use super::*; 7 8 #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] 9 #[repr(u8)] 10 enum SmallPrime { 11 Two = 2, 12 Three = 3, 13 Five = 5, 14 Seven = 7, 15 } 16 17 #[test] test_serialize()18 fn test_serialize() { 19 let j = serde_json::to_string(&SmallPrime::Seven).unwrap(); 20 assert_eq!(j, "7"); 21 } 22 23 #[test] test_deserialize()24 fn test_deserialize() { 25 let p: SmallPrime = serde_json::from_str("2").unwrap(); 26 assert_eq!(p, SmallPrime::Two); 27 } 28 } 29 30 mod other { 31 use super::*; 32 33 #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] 34 #[repr(u8)] 35 enum TestOther { 36 A, 37 B, 38 #[serde(other, rename = "useless")] 39 Other, 40 } 41 42 #[test] test_deserialize()43 fn test_deserialize() { 44 let p: TestOther = serde_json::from_str("0").unwrap(); 45 assert_eq!(p, TestOther::A); 46 let p: TestOther = serde_json::from_str("1").unwrap(); 47 assert_eq!(p, TestOther::B); 48 let p: TestOther = serde_json::from_str("5").unwrap(); 49 assert_eq!(p, TestOther::Other); 50 } 51 } 52 53 mod implicit_discriminant { 54 use super::*; 55 56 #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] 57 #[repr(u8)] 58 enum ImplicitDiscriminant { 59 Zero, 60 One, 61 Two, 62 Three, 63 } 64 65 #[test] test_serialize()66 fn test_serialize() { 67 let j = serde_json::to_string(&ImplicitDiscriminant::Three).unwrap(); 68 assert_eq!(j, "3"); 69 } 70 71 #[test] test_deserialize()72 fn test_deserialize() { 73 let p: ImplicitDiscriminant = serde_json::from_str("2").unwrap(); 74 assert_eq!(p, ImplicitDiscriminant::Two); 75 } 76 } 77