nil_zonefile/
resource.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
4pub enum ResourceValue {
5    Text(String),
6    Binary(Vec<u8>),
7}
8
9#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
10pub struct Resource {
11    pub val: Option<ResourceValue>,
12    pub mime: Option<String>,
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18    use ciborium;
19
20    #[test]
21    fn test_resource_text() {
22        let resource = Resource {
23            val: Some(ResourceValue::Text("Hello, world!".to_string())),
24            mime: Some("text/plain".to_string()),
25        };
26
27        // Test CBOR roundtrip
28        let mut bytes = Vec::new();
29        ciborium::ser::into_writer(&resource, &mut bytes).unwrap();
30        let decoded: Resource = ciborium::de::from_reader(&bytes[..]).unwrap();
31
32        assert_eq!(resource, decoded);
33        match decoded.val {
34            Some(ResourceValue::Text(text)) => assert_eq!(text, "Hello, world!"),
35            _ => panic!("Expected text resource"),
36        }
37        assert_eq!(decoded.mime, Some("text/plain".to_string()));
38    }
39
40    #[test]
41    fn test_resource_binary() {
42        let resource = Resource {
43            val: Some(ResourceValue::Binary(vec![0x01, 0x02, 0x03])),
44            mime: Some("application/octet-stream".to_string()),
45        };
46
47        // Test CBOR roundtrip
48        let mut bytes = Vec::new();
49        ciborium::ser::into_writer(&resource, &mut bytes).unwrap();
50        let decoded: Resource = ciborium::de::from_reader(&bytes[..]).unwrap();
51
52        assert_eq!(resource, decoded);
53        match decoded.val {
54            Some(ResourceValue::Binary(data)) => assert_eq!(data, vec![0x01, 0x02, 0x03]),
55            _ => panic!("Expected binary resource"),
56        }
57    }
58
59    #[test]
60    fn test_resource_empty() {
61        let resource = Resource {
62            val: None,
63            mime: None,
64        };
65
66        // Test CBOR roundtrip
67        let mut bytes = Vec::new();
68        ciborium::ser::into_writer(&resource, &mut bytes).unwrap();
69        let decoded: Resource = ciborium::de::from_reader(&bytes[..]).unwrap();
70
71        assert_eq!(resource, decoded);
72        assert!(decoded.val.is_none());
73        assert!(decoded.mime.is_none());
74    }
75
76    #[test]
77    fn test_resource_large_binary() {
78        let large_data: Vec<u8> = (0..1000).map(|i| (i % 256) as u8).collect();
79        let resource = Resource {
80            val: Some(ResourceValue::Binary(large_data.clone())),
81            mime: Some("application/octet-stream".to_string()),
82        };
83
84        // Test CBOR roundtrip
85        let mut bytes = Vec::new();
86        ciborium::ser::into_writer(&resource, &mut bytes).unwrap();
87        let decoded: Resource = ciborium::de::from_reader(&bytes[..]).unwrap();
88
89        assert_eq!(resource, decoded);
90        match decoded.val {
91            Some(ResourceValue::Binary(data)) => assert_eq!(data, large_data),
92            _ => panic!("Expected binary resource"),
93        }
94    }
95}