use crate::NameTest;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AxisName {
Ancestor,
AncestorOrSelf,
Attribute,
Child,
Descendant,
DescendantOrSelf,
Following,
FollowingSibling,
Namespace,
Parent,
Preceding,
PrecedingSibling,
SelfAxis
}
impl AxisName {
pub fn principal_node_type(&self) -> PrincipalNodeType {
match *self {
AxisName::Attribute => PrincipalNodeType::Attribute,
AxisName::Namespace => PrincipalNodeType::Namespace,
_ => PrincipalNodeType::Element,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum PrincipalNodeType {
Attribute,
Namespace,
Element
}
#[derive(Debug, Clone, PartialEq)]
pub enum NodeType {
Comment,
Text,
ProcessingInstruction(Option<String>),
Node
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Operator {
And,
Or,
Mod,
Div,
Star,
ForwardSlash,
DoubleForwardSlash,
Pipe,
Plus,
Minus,
Equal,
DoesNotEqual,
LessThan,
LessThanOrEqual,
GreaterThan,
GreaterThanOrEqual
}
#[derive(Debug, Clone, PartialEq)]
pub enum ExprToken {
LeftParen,
RightParen,
LeftBracket,
RightBracket,
Period,
ParentNode,
AtSign,
Comma,
LocationStep,
Axis(AxisName),
Number(f64),
Literal(String),
NameTest(NameTest),
NodeType(NodeType),
Operator(Operator),
FunctionName(String),
VariableReference(String)
}
impl ExprToken {
pub fn is_node_type(&self) -> bool {
matches!(self, ExprToken::NodeType(_))
}
pub fn is_name_test(&self) -> bool {
matches!(self, ExprToken::NameTest(_))
}
pub fn is_operator(&self) -> bool {
matches!(self, ExprToken::Operator(_))
}
pub fn is_axis(&self) -> bool {
matches!(self, ExprToken::Axis(_))
}
pub fn is_literal(&self) -> bool {
matches!(self, ExprToken::Literal(_))
}
pub fn is_number(&self) -> bool {
matches!(self, ExprToken::Number(_))
}
pub fn is_function_name(&self) -> bool {
matches!(self, ExprToken::FunctionName(_))
}
}
macro_rules! from_impl {
($struct:ident, $enum:ident) => {
impl Into<ExprToken> for $struct {
fn into(self) -> ExprToken {
ExprToken::$enum(self)
}
}
impl Into<ExprToken> for &$struct {
fn into(self) -> ExprToken {
ExprToken::$enum(self.clone())
}
}
impl Into<Option<$struct>> for ExprToken {
fn into(self) -> Option<$struct> {
match self {
ExprToken::$enum(op) => Some(op),
_ => None
}
}
}
};
}
from_impl!(AxisName, Axis);
from_impl!(f64, Number);
from_impl!(String, Literal);
from_impl!(NameTest, NameTest);
from_impl!(NodeType, NodeType);
from_impl!(Operator, Operator);