1 use serde::{Deserialize, Deserializer}; 2 3 /// Possible filesystem types for rootfs 4 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 5 pub enum FsType { 6 Fat32, 7 } 8 9 impl<'de> Deserialize<'de> for FsType { 10 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> 11 where 12 D: Deserializer<'de>, 13 { 14 let mut s = String::deserialize(deserializer)?; 15 s.make_ascii_lowercase(); 16 match s.as_str() { 17 "fat32" => Ok(FsType::Fat32), 18 _ => Err(serde::de::Error::custom("invalid fs type")), 19 } 20 } 21 } 22 23 #[cfg(test)] 24 mod tests { 25 use super::*; 26 use serde_json::{self, Value}; 27 28 fn deserialize_fs_type(input: &str) -> Result<FsType, serde_json::Error> { 29 let json = Value::String(input.to_string()); 30 serde_json::from_value(json) 31 } 32 33 #[test] 34 fn test_deserialize_fat32_lowercase() { 35 let r = deserialize_fs_type("fat32"); 36 assert_eq!(r.is_ok(), true); 37 let fs_type = r.unwrap(); 38 assert_eq!(fs_type, FsType::Fat32); 39 } 40 41 #[test] 42 fn test_deserialize_fat32_mixed_case() { 43 let r = deserialize_fs_type("FAT32"); 44 assert_eq!(r.is_ok(), true); 45 let fs_type = r.unwrap(); 46 assert_eq!(fs_type, FsType::Fat32); 47 } 48 49 #[test] 50 fn testdeserialize_random_string() { 51 assert!(deserialize_fs_type("abc123").is_err()); 52 } 53 } 54