Skip to main content

xsd_schema/
embedded.rs

1//! Embedded schema assets
2//!
3//! Contains well-known schemas embedded as static binary data for
4//! offline use without network access.
5
6/// Embedded xml.xsd schema defining xml:lang, xml:space, xml:base attributes.
7/// Source: <http://www.w3.org/2001/xml.xsd>
8pub const XML_XSD: &[u8] = include_bytes!("../assets/xml.xsd");
9
10/// Embedded xlink.xsd schema defining XLink attributes (xlink:type, xlink:href, etc.).
11/// Source: <http://www.w3.org/XML/2008/06/xlink.xsd>
12pub const XLINK_XSD: &[u8] = include_bytes!("../assets/xlink.xsd");
13
14/// Well-known namespace URI for the xml: prefix
15pub const XML_NAMESPACE: &str = "http://www.w3.org/XML/1998/namespace";
16
17/// Well-known namespace URI for the xlink: prefix
18pub const XLINK_NAMESPACE: &str = "http://www.w3.org/1999/xlink";
19
20/// Get embedded schema by namespace URI
21pub fn get_embedded_schema(namespace: &str) -> Option<&'static [u8]> {
22    match namespace {
23        XML_NAMESPACE => Some(XML_XSD),
24        XLINK_NAMESPACE => Some(XLINK_XSD),
25        _ => None,
26    }
27}
28
29/// Check if a namespace has an embedded schema available
30pub fn has_embedded_schema(namespace: &str) -> bool {
31    matches!(namespace, XML_NAMESPACE | XLINK_NAMESPACE)
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn test_xml_xsd_embedded() {
40        let content = XML_XSD;
41        assert!(!content.is_empty());
42        // Verify it's valid XML
43        let xml_str = std::str::from_utf8(content).expect("xml.xsd should be valid UTF-8");
44        assert!(xml_str.contains("targetNamespace=\"http://www.w3.org/XML/1998/namespace\""));
45    }
46
47    #[test]
48    fn test_xlink_xsd_embedded() {
49        let content = XLINK_XSD;
50        assert!(!content.is_empty());
51        let xml_str = std::str::from_utf8(content).expect("xlink.xsd should be valid UTF-8");
52        assert!(xml_str.contains("targetNamespace=\"http://www.w3.org/1999/xlink\""));
53    }
54
55    #[test]
56    fn test_get_embedded_schema() {
57        assert!(get_embedded_schema(XML_NAMESPACE).is_some());
58        assert!(get_embedded_schema(XLINK_NAMESPACE).is_some());
59        assert!(get_embedded_schema("http://example.com/unknown").is_none());
60    }
61
62    #[test]
63    fn test_has_embedded_schema() {
64        assert!(has_embedded_schema(XML_NAMESPACE));
65        assert!(has_embedded_schema(XLINK_NAMESPACE));
66        assert!(!has_embedded_schema("http://example.com/unknown"));
67    }
68}