1use std::collections::HashMap;
4
5#[derive(Debug, Clone)]
7pub struct Namespace {
8 prefix: String,
9 uri: String,
10}
11
12impl Namespace {
13 pub fn new(prefix: &str, uri: &str) -> Self {
15 Namespace {
16 prefix: prefix.to_string(),
17 uri: uri.to_string(),
18 }
19 }
20
21 pub fn prefix(&self) -> &str {
23 &self.prefix
24 }
25
26 pub fn uri(&self) -> &str {
28 &self.uri
29 }
30}
31
32pub struct NamespaceRegistry {
34 namespaces: HashMap<String, String>,
35}
36
37impl NamespaceRegistry {
38 pub fn new() -> Self {
40 let mut namespaces = HashMap::new();
41
42 namespaces.insert("p".to_string(), "http://schemas.openxmlformats.org/presentationml/2006/main".to_string());
44 namespaces.insert("a".to_string(), "http://schemas.openxmlformats.org/drawingml/2006/main".to_string());
45 namespaces.insert("r".to_string(), "http://schemas.openxmlformats.org/officeDocument/2006/relationships".to_string());
46 namespaces.insert("rel".to_string(), "http://schemas.openxmlformats.org/package/2006/relationships".to_string());
47 namespaces.insert("c".to_string(), "http://schemas.openxmlformats.org/drawingml/2006/chart".to_string());
48
49 NamespaceRegistry { namespaces }
50 }
51
52 pub fn register(&mut self, prefix: &str, uri: &str) {
54 self.namespaces.insert(prefix.to_string(), uri.to_string());
55 }
56
57 pub fn get(&self, prefix: &str) -> Option<&str> {
59 self.namespaces.get(prefix).map(|s| s.as_str())
60 }
61
62 pub fn all(&self) -> &HashMap<String, String> {
64 &self.namespaces
65 }
66}
67
68impl Default for NamespaceRegistry {
69 fn default() -> Self {
70 Self::new()
71 }
72}
73
74pub const PML: &str = "http://schemas.openxmlformats.org/presentationml/2006/main";
76pub const DML: &str = "http://schemas.openxmlformats.org/drawingml/2006/main";
77pub const RELATIONSHIPS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
78pub const PACKAGE_RELATIONSHIPS: &str = "http://schemas.openxmlformats.org/package/2006/relationships";
79pub const CHART: &str = "http://schemas.openxmlformats.org/drawingml/2006/chart";
80pub const CORE_PROPERTIES: &str = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties";
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn test_namespace_creation() {
88 let ns = Namespace::new("p", PML);
89 assert_eq!(ns.prefix(), "p");
90 assert_eq!(ns.uri(), PML);
91 }
92
93 #[test]
94 fn test_namespace_clone() {
95 let ns1 = Namespace::new("a", DML);
96 let ns2 = ns1.clone();
97 assert_eq!(ns1.prefix(), ns2.prefix());
98 assert_eq!(ns1.uri(), ns2.uri());
99 }
100
101 #[test]
102 fn test_registry_new() {
103 let registry = NamespaceRegistry::new();
104 assert!(registry.get("p").is_some());
105 assert!(registry.get("a").is_some());
106 assert!(registry.get("r").is_some());
107 assert!(registry.get("c").is_some());
108 }
109
110 #[test]
111 fn test_registry_default() {
112 let registry = NamespaceRegistry::default();
113 assert_eq!(registry.get("p"), Some(PML));
114 assert_eq!(registry.get("a"), Some(DML));
115 }
116
117 #[test]
118 fn test_registry_get() {
119 let registry = NamespaceRegistry::new();
120 assert_eq!(registry.get("p"), Some(PML));
121 assert_eq!(registry.get("a"), Some(DML));
122 assert_eq!(registry.get("r"), Some(RELATIONSHIPS));
123 assert_eq!(registry.get("c"), Some(CHART));
124 assert_eq!(registry.get("unknown"), None);
125 }
126
127 #[test]
128 fn test_registry_register() {
129 let mut registry = NamespaceRegistry::new();
130 registry.register("custom", "http://example.com/custom");
131 assert_eq!(registry.get("custom"), Some("http://example.com/custom"));
132 }
133
134 #[test]
135 fn test_registry_all() {
136 let registry = NamespaceRegistry::new();
137 let all = registry.all();
138 assert!(all.len() >= 5);
139 assert!(all.contains_key("p"));
140 assert!(all.contains_key("a"));
141 }
142
143 #[test]
144 fn test_namespace_constants() {
145 assert!(PML.contains("presentationml"));
146 assert!(DML.contains("drawingml"));
147 assert!(RELATIONSHIPS.contains("relationships"));
148 assert!(CHART.contains("chart"));
149 assert!(CORE_PROPERTIES.contains("core-properties"));
150 }
151
152 #[test]
153 fn test_registry_override() {
154 let mut registry = NamespaceRegistry::new();
155 let original = registry.get("p").unwrap().to_string();
156 registry.register("p", "http://custom.com/pml");
157 assert_eq!(registry.get("p"), Some("http://custom.com/pml"));
158 assert_ne!(registry.get("p"), Some(original.as_str()));
159 }
160}