1use std::error::Error;
4use std::ffi::{CStr, CString};
5use std::hash::{Hash, Hasher};
6use std::ptr;
7use std::str;
8
9use crate::bindings::*;
10use crate::c_helpers::*;
11use crate::tree::node::Node;
12
13#[derive(Clone)]
15pub struct Namespace {
16 pub(crate) ns_ptr: xmlNsPtr,
18}
19
20impl PartialEq for Namespace {
21 fn eq(&self, other: &Self) -> bool {
22 self.get_prefix() == other.get_prefix() && self.get_href() == other.get_href()
23 }
24}
25
26impl Eq for Namespace {}
27
28impl Hash for Namespace {
29 fn hash<H: Hasher>(&self, state: &mut H) {
30 self.get_prefix().hash(state);
31 self.get_href().hash(state);
32 }
33}
34
35impl Namespace {
36 pub fn new(
38 prefix: &str,
39 href: &str,
40 node: &mut Node,
41 ) -> Result<Self, Box<dyn Error + Send + Sync>> {
42 let c_href = CString::new(href).unwrap();
43 let c_prefix = CString::new(prefix).unwrap();
44 let c_prefix_ptr = if prefix.is_empty() {
45 ptr::null()
46 } else {
47 c_prefix.as_ptr()
48 };
49
50 unsafe {
51 let ns = xmlNewNs(
52 node.node_ptr_mut()?,
53 c_href.as_bytes().as_ptr(),
54 c_prefix_ptr as *const u8,
55 );
56 if ns.is_null() {
57 Err(From::from("xmlNewNs returned NULL"))
58 } else {
59 Ok(Namespace { ns_ptr: ns })
60 }
61 }
62 }
63
64 pub fn ns_ptr(&self) -> xmlNsPtr {
66 self.ns_ptr
67 }
68
69 pub fn ns_ptr_mut(&mut self) -> xmlNsPtr {
71 self.ns_ptr
72 }
73 pub fn get_prefix(&self) -> String {
75 unsafe {
76 let prefix_ptr = xmlNsPrefix(self.ns_ptr());
77 if prefix_ptr.is_null() {
78 String::new()
79 } else {
80 let c_prefix = CStr::from_ptr(prefix_ptr);
81 c_prefix.to_string_lossy().into_owned()
82 }
83 }
84 }
85
86 pub fn get_href(&self) -> String {
88 unsafe {
89 let href_ptr = xmlNsHref(self.ns_ptr());
90 if href_ptr.is_null() {
91 String::new()
92 } else {
93 let c_href = CStr::from_ptr(href_ptr);
94 c_href.to_string_lossy().into_owned()
95 }
96 }
97 }
98
99 pub fn free(&mut self) {
101 unsafe { xmlFreeNs(self.ns_ptr()) }
102 }
103}