use std::{fmt::Display, hash::Hash};
use crate::unique_string::UniqueString;
#[derive(Debug)]
pub struct InvalidQName;
impl Display for InvalidQName {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "InvalidQName")
}
}
impl std::error::Error for InvalidQName {}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct NamespaceUri(pub(crate) UniqueString);
impl NamespaceUri {
#[must_use]
#[inline]
pub fn empty() -> Self {
Self(UniqueString::empty())
}
pub fn to_string(&self) -> String {
self.0.to_string()
}
}
impl TryFrom<&str> for NamespaceUri {
type Error = InvalidQName;
#[inline]
fn try_from(value: &str) -> Result<Self, Self::Error> {
if check_normalized_value(value) {
Ok(Self(UniqueString::from(value)))
} else {
Err(InvalidQName)
}
}
}
impl PartialEq<String> for NamespaceUri {
#[inline]
fn eq(&self, other: &String) -> bool {
self.0.to_string().eq(other)
}
}
impl PartialEq<NamespaceUri> for String {
#[inline]
fn eq(&self, other: &NamespaceUri) -> bool {
self.eq(&other.to_string())
}
}
impl PartialEq<str> for NamespaceUri {
#[inline]
fn eq(&self, other: &str) -> bool {
self.0.to_string().eq(other)
}
}
impl PartialEq<NamespaceUri> for str {
#[inline]
fn eq(&self, other: &NamespaceUri) -> bool {
self.eq(&other.0.to_string())
}
}
fn is_ncname_start_char(c: char) -> bool {
c.is_ascii_alphabetic()
|| c == '_'
|| (c >= 0xC0 as char && c <= '\u{2FF}' && c != 0xD7 as char && c != 0xF7 as char)
|| (('\u{370}'..='\u{1FFF}').contains(&c) && c != '\u{37E}')
|| ('\u{200C}'..='\u{200D}').contains(&c)
|| ('\u{2070}'..='\u{218F}').contains(&c)
|| ('\u{2C00}'..='\u{2FEF}').contains(&c)
|| ('\u{3001}'..='\u{D7FF}').contains(&c)
|| ('\u{F900}'..='\u{FDCF}').contains(&c)
|| ('\u{FDF0}'..='\u{FFFD}').contains(&c)
|| ('\u{10000}'..='\u{EFFFF}').contains(&c)
}
fn is_ncname_char(c: char) -> bool {
is_ncname_start_char(c)
|| c == '-'
|| c == '.'
|| c.is_ascii_digit()
|| c == 0xb7 as char
|| ('\u{300}'..='\u{36F}').contains(&c)
|| c == '\u{203F}'
|| c == '\u{2040}'
}
fn parse_ncname(input: &str) -> Result<(&str, &str), ()> {
let mut chars = input.char_indices();
if let Some((_, c)) = chars.next() {
if !is_ncname_start_char(c) {
return Err(());
}
} else {
return Err(());
}
for (end, c) in chars {
if !is_ncname_char(c) {
return Ok((&input[..end], &input[end..]));
}
}
Ok((input, ""))
}
fn check_normalized_value(input: &str) -> bool {
if input.starts_with(' ') || input.contains(" ") || input.ends_with(' ') {
return false;
}
if input
.bytes()
.any(|b| b == b'\t' || b == b'\r' || b == b'\n')
{
return false;
}
true
}
#[derive(Clone, Debug, Eq, Hash)]
pub struct NcName(pub(crate) UniqueString);
impl NcName {
pub fn to_string(&self) -> String {
self.0.to_string()
}
}
impl TryFrom<&str> for NcName {
type Error = InvalidQName;
#[inline]
fn try_from(value: &str) -> Result<Self, Self::Error> {
if let Ok((ncname, left)) = parse_ncname(value) {
if left.is_empty() {
return Ok(Self(ncname.into()));
}
}
Err(InvalidQName)
}
}
impl PartialEq<NcName> for NcName {
#[inline]
fn eq(&self, other: &NcName) -> bool {
self.0.eq(&other.0)
}
}
impl PartialEq<String> for NcName {
#[inline]
fn eq(&self, other: &String) -> bool {
self.0.to_string().eq(other)
}
}
impl PartialEq<NcName> for String {
#[inline]
fn eq(&self, other: &NcName) -> bool {
self.eq(&other.0.to_string())
}
}
impl PartialEq<str> for NcName {
#[inline]
fn eq(&self, other: &str) -> bool {
self.0.to_string().eq(other)
}
}
impl PartialEq<NcName> for str {
#[inline]
fn eq(&self, other: &NcName) -> bool {
self.eq(&other.0.to_string())
}
}
impl PartialOrd for NcName {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.to_string().partial_cmp(&other.to_string())
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NamespacePrefix(pub(crate) String);
impl NamespacePrefix {
#[inline]
#[must_use]
pub fn empty() -> Self {
Self(String::new())
}
#[must_use]
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn to_string(&self) -> String {
self.0.clone()
}
pub fn to_ncname(&self) -> NcName {
NcName::try_from(self.0.as_str()).unwrap()
}
}
impl TryFrom<&str> for NamespacePrefix {
type Error = InvalidQName;
#[inline]
fn try_from(value: &str) -> Result<Self, Self::Error> {
if value.is_empty() {
return Ok(Self::empty());
}
if value == "xmlns" {
return Err(InvalidQName);
}
if let Ok((ncname, left)) = parse_ncname(value) {
if left.is_empty() {
return Ok(Self(ncname.into()));
}
}
Err(InvalidQName)
}
}
#[test]
fn test_parse_valid_ncname() {
let (ncname, left) = parse_ncname("ok").unwrap();
assert_eq!(ncname, "ok");
assert_eq!(left, "");
}
#[test]
fn test_parse_invalid_prefix() {
let prefix_result = NamespacePrefix::try_from("xmlns");
assert!(prefix_result.is_err())
}
#[test]
fn test_parse_valid_ncname_with_extra() {
let (ncname, left) = parse_ncname("ok!").unwrap();
assert_eq!(ncname, "ok");
assert_eq!(left, "!");
}
#[test]
fn test_parse_invalid_ncname_with_extra() {
assert!(parse_ncname("!").is_err());
assert!(parse_ncname(":").is_err());
assert!(parse_ncname(" ").is_err());
}
#[test]
fn test_parse_valid_normalized_attribute_value() {
assert_eq!(check_normalized_value(""), true);
assert_eq!(check_normalized_value("ok"), true);
assert_eq!(check_normalized_value(" "), false);
assert_eq!(check_normalized_value(" a"), false);
assert_eq!(check_normalized_value("a "), false);
assert_eq!(check_normalized_value("a b"), true);
assert_eq!(check_normalized_value("a b"), false);
assert_eq!(check_normalized_value("a\tb"), false);
assert_eq!(check_normalized_value("note-♩"), true);
}