libvirt_codegen/
ast.rs

1//! AST definitions for XDR protocol.
2
3/// Represents a complete XDR protocol definition.
4#[derive(Debug, Clone)]
5pub struct Protocol {
6    pub name: String,
7    pub constants: Vec<Constant>,
8    pub types: Vec<TypeDef>,
9    pub procedures: Vec<Procedure>,
10}
11
12impl Protocol {
13    pub fn new(name: impl Into<String>) -> Self {
14        Self {
15            name: name.into(),
16            constants: Vec::new(),
17            types: Vec::new(),
18            procedures: Vec::new(),
19        }
20    }
21}
22
23/// A constant definition.
24#[derive(Debug, Clone)]
25pub struct Constant {
26    pub name: String,
27    pub value: ConstValue,
28}
29
30/// Constant value.
31#[derive(Debug, Clone)]
32pub enum ConstValue {
33    Int(i64),
34    Ident(String),
35}
36
37/// Type definition.
38#[derive(Debug, Clone)]
39pub enum TypeDef {
40    Struct(StructDef),
41    Enum(EnumDef),
42    Union(UnionDef),
43    Typedef(TypedefDef),
44}
45
46/// Struct definition.
47#[derive(Debug, Clone)]
48pub struct StructDef {
49    pub name: String,
50    pub fields: Vec<Field>,
51}
52
53/// Struct field.
54#[derive(Debug, Clone)]
55pub struct Field {
56    pub name: String,
57    pub ty: Type,
58}
59
60/// Enum definition.
61#[derive(Debug, Clone)]
62pub struct EnumDef {
63    pub name: String,
64    pub variants: Vec<EnumVariant>,
65}
66
67/// Enum variant.
68#[derive(Debug, Clone)]
69pub struct EnumVariant {
70    pub name: String,
71    pub value: Option<ConstValue>,
72}
73
74/// Union definition (discriminated union).
75#[derive(Debug, Clone)]
76pub struct UnionDef {
77    pub name: String,
78    pub discriminant: Field,
79    pub cases: Vec<UnionCase>,
80    pub default: Option<Box<Type>>,
81}
82
83/// Union case.
84#[derive(Debug, Clone)]
85pub struct UnionCase {
86    pub values: Vec<ConstValue>,
87    pub field: Option<Field>,
88}
89
90/// Typedef definition.
91#[derive(Debug, Clone)]
92pub struct TypedefDef {
93    pub name: String,
94    pub target: Type,
95}
96
97/// Type representation.
98#[derive(Debug, Clone)]
99pub enum Type {
100    /// void
101    Void,
102    /// int
103    Int,
104    /// unsigned int
105    UInt,
106    /// hyper
107    Hyper,
108    /// unsigned hyper
109    UHyper,
110    /// float
111    Float,
112    /// double
113    Double,
114    /// bool
115    Bool,
116    /// string<N> or string<>
117    String { max_len: Option<u32> },
118    /// opaque<N> or opaque[N]
119    Opaque { len: LengthSpec },
120    /// T<N> or T[N] (array)
121    Array {
122        elem: Box<Type>,
123        len: LengthSpec,
124    },
125    /// T * (optional)
126    Optional(Box<Type>),
127    /// Named type reference
128    Named(String),
129}
130
131/// Length specification for arrays and opaque data.
132#[derive(Debug, Clone)]
133pub enum LengthSpec {
134    /// Fixed length [N]
135    Fixed(u32),
136    /// Variable length <N> or <>
137    Variable { max: Option<u32> },
138}
139
140/// RPC procedure definition.
141#[derive(Debug, Clone)]
142pub struct Procedure {
143    pub name: String,
144    pub number: u32,
145    pub args: Option<String>,
146    pub ret: Option<String>,
147    pub priority: Priority,
148}
149
150/// Procedure priority.
151#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
152pub enum Priority {
153    #[default]
154    Low,
155    High,
156}