use std::fmt::{Debug, Display};
use std::str::FromStr;
const XML_VERSION_1_0: &str = "1.0";
const XML_VERSION_1_1: &str = "1.1";
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Version {
Version10,
Version11,
}
impl FromStr for Version {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
XML_VERSION_1_0 => Ok(Self::Version10),
XML_VERSION_1_1 => Ok(Self::Version11),
_ => Err(anyhow::anyhow!("Invalid XML version")),
}
}
}
impl Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let v = match *self {
Self::Version10 => XML_VERSION_1_0,
Self::Version11 => XML_VERSION_1_1,
};
write!(f, "{}", v)
}
}
impl Debug for Version {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let v = match *self {
Self::Version10 => XML_VERSION_1_0,
Self::Version11 => XML_VERSION_1_1,
};
write!(f, "{}", v)
}
}
#[derive(Clone, Debug)]
pub struct Attribute {
pub name: String,
pub value: String,
}
#[derive(Clone, Debug)]
pub enum ElementType {
Open,
Close,
}
#[derive(Clone, Debug)]
pub struct Element {
pub name: String,
pub attributes: Vec<Attribute>,
pub r#type: ElementType,
}
#[derive(Debug)]
pub enum Node {
Declaration {
version: Version,
encoding: String,
},
Element(Element),
CData(String),
Comment(String),
Characters(String),
}