| 1 |
|
| 2 |
|
| 3 |
use std::error::Error; |
| 4 |
use std::ffi::{CStr, CString}; |
| 5 |
use std::hash::{Hash, Hasher}; |
| 6 |
use std::ptr; |
| 7 |
use std::str; |
| 8 |
|
| 9 |
use crate::bindings::*; |
| 10 |
use crate::c_helpers::*; |
| 11 |
use crate::tree::node::Node; |
| 12 |
|
| 13 |
|
| 14 |
#[derive(Clone)] |
| 15 |
pub struct Namespace { |
| 16 |
|
| 17 |
pub(crate) ns_ptr: xmlNsPtr, |
| 18 |
} |
| 19 |
|
| 20 |
impl 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 |
|
| 26 |
impl Eq for Namespace {} |
| 27 |
|
| 28 |
impl 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 |
|
| 35 |
impl Namespace { |
| 36 |
|
| 37 |
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 |
|
| 65 |
pub fn ns_ptr(&self) -> xmlNsPtr { |
| 66 |
self.ns_ptr |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
pub fn ns_ptr_mut(&mut self) -> xmlNsPtr { |
| 71 |
self.ns_ptr |
| 72 |
} |
| 73 |
|
| 74 |
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 |
|
| 87 |
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 |
|
| 100 |
pub fn free(&mut self) { |
| 101 |
unsafe { xmlFreeNs(self.ns_ptr()) } |
| 102 |
} |
| 103 |
} |