Skip to main content

oak_idl/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4/// Root of the IDL AST.
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct IdlRoot {
8    /// Items defined at the top level.
9    pub items: Vec<IdlItem>,
10}
11
12/// A top-level item in an IDL file.
13#[derive(Debug, Clone, PartialEq, Eq, Hash)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub enum IdlItem {
16    /// A module definition.
17    Module(Module),
18    /// An interface definition.
19    Interface(Interface),
20    /// A struct definition.
21    Struct(Struct),
22    /// An enum definition.
23    Enum(Enum),
24    /// A type alias definition.
25    Typedef(Typedef),
26    /// A constant definition.
27    Const(Const),
28}
29
30/// A module in IDL.
31#[derive(Debug, Clone, PartialEq, Eq, Hash)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33pub struct Module {
34    /// The name of the module.
35    pub name: String,
36    /// Items contained within the module.
37    pub items: Vec<IdlItem>,
38    /// The source range of this module.
39    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
40    pub span: Range<usize>,
41}
42
43/// An interface in IDL.
44#[derive(Debug, Clone, PartialEq, Eq, Hash)]
45#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
46pub struct Interface {
47    /// The name of the interface.
48    pub name: String,
49    /// Members of the interface (attributes and operations).
50    pub members: Vec<IdlMember>,
51    /// The source range of this interface.
52    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
53    pub span: Range<usize>,
54}
55
56/// A member of an IDL interface.
57#[derive(Debug, Clone, PartialEq, Eq, Hash)]
58#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
59pub enum IdlMember {
60    /// An attribute.
61    Attribute(Attribute),
62    /// An operation (method).
63    Operation(Operation),
64}
65
66/// An attribute in an interface.
67#[derive(Debug, Clone, PartialEq, Eq, Hash)]
68#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
69pub struct Attribute {
70    /// The name of the attribute.
71    pub name: String,
72    /// The type of the attribute.
73    pub type_name: String,
74    /// Whether the attribute is read-only.
75    pub readonly: bool,
76}
77
78/// An operation (method) in an interface.
79#[derive(Debug, Clone, PartialEq, Eq, Hash)]
80#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
81pub struct Operation {
82    /// The name of the operation.
83    pub name: String,
84    /// The return type of the operation.
85    pub return_type: String,
86    /// Parameters of the operation.
87    pub params: Vec<Param>,
88}
89
90/// A parameter of an operation.
91#[derive(Debug, Clone, PartialEq, Eq, Hash)]
92#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
93pub struct Param {
94    /// The name of the parameter.
95    pub name: String,
96    /// The type of the parameter.
97    pub type_name: String,
98    /// The direction of the parameter (in, out, inout).
99    pub direction: ParamDirection,
100}
101
102/// Parameter passing direction.
103#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
104#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
105pub enum ParamDirection {
106    /// Input parameter.
107    In,
108    /// Output parameter.
109    Out,
110    /// Input/Output parameter.
111    Inout,
112}
113
114/// A struct definition in IDL.
115#[derive(Debug, Clone, PartialEq, Eq, Hash)]
116#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
117pub struct Struct {
118    /// The name of the struct.
119    pub name: String,
120    /// Fields of the struct.
121    pub fields: Vec<Field>,
122    /// The source range of this struct.
123    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
124    pub span: Range<usize>,
125}
126
127/// A field in a struct.
128#[derive(Debug, Clone, PartialEq, Eq, Hash)]
129#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
130pub struct Field {
131    /// The name of the field.
132    pub name: String,
133    /// The type of the field.
134    pub type_name: String,
135}
136
137/// An enum definition in IDL.
138#[derive(Debug, Clone, PartialEq, Eq, Hash)]
139#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
140pub struct Enum {
141    /// The name of the enum.
142    pub name: String,
143    /// Variants of the enum.
144    pub variants: Vec<String>,
145}
146
147/// A type alias in IDL.
148#[derive(Debug, Clone, PartialEq, Eq, Hash)]
149#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
150pub struct Typedef {
151    /// The name of the new type.
152    pub name: String,
153    /// The existing type it aliases.
154    pub type_name: String,
155}
156
157/// A constant definition in IDL.
158#[derive(Debug, Clone, PartialEq, Eq, Hash)]
159#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
160pub struct Const {
161    /// The name of the constant.
162    pub name: String,
163    /// The type of the constant.
164    pub type_name: String,
165    /// The value of the constant.
166    pub value: String,
167}