cadi_core/
ast.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5pub struct CadlDocument {
6    pub interfaces: Vec<InterfaceDef>,
7    pub implementations: Vec<ImplDef>,
8    pub constraints: Vec<ConstraintDef>,
9    pub top_level_annotations: Vec<Annotation>,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
13pub struct InterfaceDef {
14    pub name: String,
15    pub methods: Vec<MethodDef>,
16    pub annotations: Vec<Annotation>,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
20pub struct ImplDef {
21    pub name: String, // e.g. VideoCodec.webgl1
22    pub attributes: HashMap<String, Value>,
23    pub annotations: Vec<Annotation>,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
27pub struct ConstraintDef {
28    pub name: Option<String>,
29    pub rules: Vec<String>, // simplified for now
30    #[serde(flatten)]
31    pub other: HashMap<String, Value>,
32}
33
34
35#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
36pub struct MethodDef {
37    pub name: String,
38    pub params: Vec<ParamDef>,
39    pub return_type: String,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
43pub struct ParamDef {
44    pub name: String,
45    pub type_name: String,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
49pub enum Annotation {
50    Contract(ContractDef),
51    Effects(EffectsDef),
52    Abi(AbiDef),
53    DataFormat(DataFormatDef),
54    Resources(ResourcesDef),
55    Protocol(ProtocolDef),
56    Numerical(NumericalDef),
57    Observability(ObservabilityDef),
58    Permissions(PermissionsDef),
59    Unknown(String, HashMap<String, Value>),
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
63pub enum Value {
64    String(String),
65    Number(f64),
66    Bool(bool),
67    Array(Vec<Value>),
68    Object(HashMap<String, Value>),
69}
70
71// @contract
72#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
73pub struct ContractDef {
74    pub codec: Option<String>,
75    pub profile: Option<String>,
76    pub container: Option<String>,
77    pub ensures: Vec<String>,
78    pub complexity: HashMap<String, String>,
79    #[serde(flatten)]
80    pub other: HashMap<String, Value>,
81}
82
83// @effects
84#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
85pub struct EffectsDef {
86    pub concurrency: Option<String>,
87    pub io: Vec<String>,
88    pub memory: Option<String>,
89    pub mutates: Vec<String>,
90    pub reads: Vec<String>,
91    pub blocking: Option<String>,
92    pub async_exec: Option<String>,
93    #[serde(flatten)]
94    pub other: HashMap<String, Value>,
95}
96
97// @abi
98#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
99pub struct AbiDef {
100    pub string_encoding: Option<String>,
101    pub memory_ownership: HashMap<String, String>,
102    pub error_model: Option<String>,
103    #[serde(flatten)]
104    pub other: HashMap<String, Value>,
105}
106
107// @data_format
108#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
109pub struct DataFormatDef {
110    pub input: HashMap<String, DataFormatSpec>,
111    pub output: HashMap<String, DataFormatSpec>,
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
115pub struct DataFormatSpec {
116    pub format: Option<String>,
117    pub schema: Option<String>,
118    pub value_range: Option<Vec<f64>>, // Simple numeric range for now
119    #[serde(flatten)]
120    pub other: HashMap<String, Value>,
121}
122
123// @resources
124#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
125pub struct ResourcesDef {
126    pub requires: Vec<String>,
127    pub memory: HashMap<String, String>,
128    pub cpu_time: Option<String>,
129    pub gpu: HashMap<String, String>,
130    #[serde(flatten)]
131    pub other: HashMap<String, Value>,
132}
133
134// @protocol
135#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
136pub struct ProtocolDef {
137    pub states: Vec<String>,
138    pub initial: Option<String>,
139    pub transitions: Vec<String>,
140    #[serde(flatten)]
141    pub other: HashMap<String, Value>,
142}
143
144// @numerical
145#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
146pub struct NumericalDef {
147    pub precision: Option<String>,
148    pub error_bounds: HashMap<String, String>,
149    #[serde(flatten)]
150    pub other: HashMap<String, Value>,
151}
152
153// @observability
154#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
155pub struct ObservabilityDef {
156    pub logging: HashMap<String, Value>,
157    pub metrics: HashMap<String, Value>,
158    pub tracing: HashMap<String, Value>,
159}
160
161// @permissions
162#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
163pub struct PermissionsDef {
164    pub allow: Vec<String>,
165    pub deny: Vec<String>,
166    pub sandbox: HashMap<String, String>,
167}