Skip to main content

fastxdr/ast/
node.rs

1use super::*;
2#[derive(Debug, PartialEq)]
3pub(crate) enum Node<'a> {
4    Ident(&'a str),
5    Type(BasicType),
6    Option(Vec<Node<'a>>),
7    Struct(Struct),
8    Union(Union),
9    UnionCase(Vec<Node<'a>>),
10    UnionDefault(Vec<Node<'a>>),
11    UnionVoid,
12    StructDataField(Vec<Node<'a>>),
13    UnionDataField(Vec<Node<'a>>),
14    Array(Vec<Node<'a>>),
15    ArrayVariable(&'a str),
16    ArrayFixed(&'a str),
17    Typedef(Typedef),
18    Constant(Vec<Node<'a>>),
19    Enum(Enum),
20    EnumVariant(Vec<Node<'a>>),
21    Root(Vec<Node<'a>>),
22
23    EOF,
24}
25
26impl<'a> Node<'a> {
27    pub(crate) fn ident_str(&'a self) -> &'a str {
28        match self {
29            Node::Ident(v) => match v.trim() {
30                "type" => "type_t",
31                v => v,
32            },
33            Node::Type(v) => v.as_str(),
34            Node::Option(v) => v[0].ident_str(),
35            _ => panic!("not an ident"),
36        }
37    }
38
39    #[cfg(test)]
40    pub(crate) fn into_inner(self) -> Vec<Node<'a>> {
41        match self {
42            Self::Option(v) => v,
43            Self::UnionCase(v) => v,
44            Self::UnionDefault(v) => v,
45            Self::StructDataField(v) => v,
46            Self::UnionDataField(v) => v,
47            Self::Array(v) => v,
48            Self::Constant(v) => v,
49            Self::EnumVariant(v) => v,
50            Self::Root(v) => v,
51            _ => panic!("no node inner"),
52        }
53    }
54}