Skip to main content

fop_core/xml/
namespace.rs

1//! Namespace definitions for XSL-FO
2
3/// XSL-FO namespace URIs
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Namespace {
6    /// XSL-FO 1.1 namespace: <http://www.w3.org/1999/XSL/Format>
7    Fo,
8    /// FOP extensions namespace
9    FoxExtensions,
10    /// SVG namespace
11    Svg,
12    /// Other/unknown namespace
13    Other,
14}
15
16impl Namespace {
17    /// XSL-FO 1.1 namespace URI
18    pub const FO_URI: &'static str = "http://www.w3.org/1999/XSL/Format";
19
20    /// FOP extensions namespace URI
21    pub const FOX_URI: &'static str = "http://xmlgraphics.apache.org/fop/extensions";
22
23    /// SVG namespace URI
24    pub const SVG_URI: &'static str = "http://www.w3.org/2000/svg";
25
26    /// Parse a namespace URI
27    pub fn from_uri(uri: &str) -> Self {
28        match uri {
29            Self::FO_URI => Namespace::Fo,
30            Self::FOX_URI => Namespace::FoxExtensions,
31            Self::SVG_URI => Namespace::Svg,
32            _ => Namespace::Other,
33        }
34    }
35
36    /// Get the namespace URI
37    pub fn uri(self) -> &'static str {
38        match self {
39            Namespace::Fo => Self::FO_URI,
40            Namespace::FoxExtensions => Self::FOX_URI,
41            Namespace::Svg => Self::SVG_URI,
42            Namespace::Other => "",
43        }
44    }
45
46    /// Check if this is the FO namespace
47    #[inline]
48    pub fn is_fo(self) -> bool {
49        matches!(self, Namespace::Fo)
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_namespace_parsing() {
59        assert_eq!(
60            Namespace::from_uri("http://www.w3.org/1999/XSL/Format"),
61            Namespace::Fo
62        );
63        assert_eq!(
64            Namespace::from_uri("http://xmlgraphics.apache.org/fop/extensions"),
65            Namespace::FoxExtensions
66        );
67        assert_eq!(
68            Namespace::from_uri("http://www.w3.org/2000/svg"),
69            Namespace::Svg
70        );
71        assert_eq!(Namespace::from_uri("http://example.com"), Namespace::Other);
72    }
73
74    #[test]
75    fn test_namespace_uri() {
76        assert_eq!(Namespace::Fo.uri(), "http://www.w3.org/1999/XSL/Format");
77        assert!(Namespace::Fo.is_fo());
78        assert!(!Namespace::Svg.is_fo());
79    }
80}
81
82// ===== ADDITIONAL TESTS =====
83#[cfg(test)]
84mod additional_tests {
85    use super::*;
86
87    #[test]
88    fn test_namespace_fo_uri_constant() {
89        assert_eq!(Namespace::FO_URI, "http://www.w3.org/1999/XSL/Format");
90    }
91
92    #[test]
93    fn test_namespace_fox_uri_constant() {
94        assert_eq!(
95            Namespace::FOX_URI,
96            "http://xmlgraphics.apache.org/fop/extensions"
97        );
98    }
99
100    #[test]
101    fn test_namespace_svg_uri_constant() {
102        assert_eq!(Namespace::SVG_URI, "http://www.w3.org/2000/svg");
103    }
104
105    #[test]
106    fn test_namespace_fox_is_not_fo() {
107        assert!(!Namespace::FoxExtensions.is_fo());
108    }
109
110    #[test]
111    fn test_namespace_other_is_not_fo() {
112        assert!(!Namespace::Other.is_fo());
113    }
114
115    #[test]
116    fn test_namespace_svg_is_not_fo() {
117        assert!(!Namespace::Svg.is_fo());
118    }
119
120    #[test]
121    fn test_namespace_fo_is_fo() {
122        assert!(Namespace::Fo.is_fo());
123    }
124
125    #[test]
126    fn test_namespace_from_empty_uri() {
127        assert_eq!(Namespace::from_uri(""), Namespace::Other);
128    }
129
130    #[test]
131    fn test_namespace_from_partial_uri() {
132        assert_eq!(Namespace::from_uri("http://www.w3.org"), Namespace::Other);
133    }
134
135    #[test]
136    fn test_namespace_equality() {
137        assert_eq!(Namespace::Fo, Namespace::Fo);
138        assert_eq!(Namespace::Svg, Namespace::Svg);
139        assert_ne!(Namespace::Fo, Namespace::Svg);
140        assert_ne!(Namespace::FoxExtensions, Namespace::Other);
141    }
142
143    #[test]
144    fn test_namespace_svg_uri() {
145        assert_eq!(Namespace::Svg.uri(), "http://www.w3.org/2000/svg");
146    }
147
148    #[test]
149    fn test_namespace_fox_uri() {
150        assert_eq!(
151            Namespace::FoxExtensions.uri(),
152            "http://xmlgraphics.apache.org/fop/extensions"
153        );
154    }
155
156    #[test]
157    fn test_namespace_other_uri_empty() {
158        assert_eq!(Namespace::Other.uri(), "");
159    }
160}