use core::hash::{Hash, Hasher};
use std::fmt::{Display, Formatter};
#[derive(Clone, Debug)]
pub struct QualifiedName {
nsuri: Option<String>,
prefix: Option<String>,
localname: String,
}
impl QualifiedName {
pub fn new(nsuri: Option<String>, prefix: Option<String>, localname: String) -> QualifiedName {
QualifiedName {
nsuri,
prefix,
localname,
}
}
pub fn get_localname(&self) -> String {
self.localname.clone()
}
}
impl Display for QualifiedName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut result = String::new();
if let Some(p) = self.prefix.as_ref() {
result.push_str(p.as_str());
result.push(':');
}
result.push_str(self.localname.as_str());
write!(f, "{}", result)
}
}
impl PartialEq for QualifiedName {
fn eq(&self, other: &QualifiedName) -> bool {
self.nsuri.as_ref().map_or_else(
|| {
other
.nsuri
.as_ref()
.map_or_else(|| self.localname.eq(other.localname.as_str()), |_| false)
},
|ns| {
other.nsuri.as_ref().map_or_else(
|| false,
|ons| ns.eq(ons.as_str()) && self.localname.eq(other.localname.as_str()),
)
},
)
}
}
impl Eq for QualifiedName {}
impl Hash for QualifiedName {
fn hash<H: Hasher>(&self, state: &mut H) {
if let Some(ref ns) = self.nsuri {
ns.hash(state);
}
self.localname.hash(state);
}
}