nil-zonefile 0.3.0

A library for parsing and creating zonefiles on the new internet.
Documentation
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum ResourceValue {
    Text(String),
    Binary(Vec<u8>),
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Resource {
    pub val: Option<ResourceValue>,
    pub mime: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use ciborium;

    #[test]
    fn test_resource_text() {
        let resource = Resource {
            val: Some(ResourceValue::Text("Hello, world!".to_string())),
            mime: Some("text/plain".to_string()),
        };

        // Test CBOR roundtrip
        let mut bytes = Vec::new();
        ciborium::ser::into_writer(&resource, &mut bytes).unwrap();
        let decoded: Resource = ciborium::de::from_reader(&bytes[..]).unwrap();

        assert_eq!(resource, decoded);
        match decoded.val {
            Some(ResourceValue::Text(text)) => assert_eq!(text, "Hello, world!"),
            _ => panic!("Expected text resource"),
        }
        assert_eq!(decoded.mime, Some("text/plain".to_string()));
    }

    #[test]
    fn test_resource_binary() {
        let resource = Resource {
            val: Some(ResourceValue::Binary(vec![0x01, 0x02, 0x03])),
            mime: Some("application/octet-stream".to_string()),
        };

        // Test CBOR roundtrip
        let mut bytes = Vec::new();
        ciborium::ser::into_writer(&resource, &mut bytes).unwrap();
        let decoded: Resource = ciborium::de::from_reader(&bytes[..]).unwrap();

        assert_eq!(resource, decoded);
        match decoded.val {
            Some(ResourceValue::Binary(data)) => assert_eq!(data, vec![0x01, 0x02, 0x03]),
            _ => panic!("Expected binary resource"),
        }
    }

    #[test]
    fn test_resource_empty() {
        let resource = Resource {
            val: None,
            mime: None,
        };

        // Test CBOR roundtrip
        let mut bytes = Vec::new();
        ciborium::ser::into_writer(&resource, &mut bytes).unwrap();
        let decoded: Resource = ciborium::de::from_reader(&bytes[..]).unwrap();

        assert_eq!(resource, decoded);
        assert!(decoded.val.is_none());
        assert!(decoded.mime.is_none());
    }

    #[test]
    fn test_resource_large_binary() {
        let large_data: Vec<u8> = (0..1000).map(|i| (i % 256) as u8).collect();
        let resource = Resource {
            val: Some(ResourceValue::Binary(large_data.clone())),
            mime: Some("application/octet-stream".to_string()),
        };

        // Test CBOR roundtrip
        let mut bytes = Vec::new();
        ciborium::ser::into_writer(&resource, &mut bytes).unwrap();
        let decoded: Resource = ciborium::de::from_reader(&bytes[..]).unwrap();

        assert_eq!(resource, decoded);
        match decoded.val {
            Some(ResourceValue::Binary(data)) => assert_eq!(data, large_data),
            _ => panic!("Expected binary resource"),
        }
    }
}