1use indexmap::IndexMap;
2use kcl_error::SourceRange;
3use serde::Serialize;
4
5use super::ArtifactId;
6use crate::ModuleId;
7use crate::NumericType;
8use crate::ast::ItemVisibility;
9use crate::ast::node_path::NodePath;
10use crate::front::ObjectId;
11
12#[allow(clippy::large_enum_variant)]
15#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
16#[ts(export_to = "Operation.ts")]
17#[serde(tag = "type")]
18pub enum Operation {
19 #[serde(rename_all = "camelCase")]
20 StdLibCall {
21 name: String,
22 unlabeled_arg: Option<OpArg>,
24 labeled_args: IndexMap<String, OpArg>,
26 node_path: NodePath,
28 source_range: SourceRange,
30 #[serde(default, skip_serializing_if = "Option::is_none")]
33 stdlib_entry_source_range: Option<SourceRange>,
34 #[serde(default, skip_serializing_if = "is_false")]
36 is_error: bool,
37 },
38 #[serde(rename_all = "camelCase")]
39 VariableDeclaration {
40 name: String,
42 value: OpKclValue,
44 visibility: ItemVisibility,
47 node_path: NodePath,
49 source_range: SourceRange,
51 },
52 #[serde(rename_all = "camelCase")]
53 GroupBegin {
54 group: Group,
56 node_path: NodePath,
58 source_range: SourceRange,
60 },
61 #[serde(rename_all = "camelCase")]
62 ModuleInstance {
63 name: String,
65 module_id: ModuleId,
67 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
69 glob: bool,
70 node_path: NodePath,
72 source_range: SourceRange,
74 },
75 GroupEnd,
76}
77
78impl Operation {
79 pub fn set_std_lib_call_is_error(&mut self, is_err: bool) {
81 match self {
82 Self::StdLibCall { is_error, .. } => *is_error = is_err,
83 Self::VariableDeclaration { .. }
84 | Self::GroupBegin { .. }
85 | Self::ModuleInstance { .. }
86 | Self::GroupEnd => {}
87 }
88 }
89}
90
91#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
92#[ts(export_to = "Operation.ts")]
93#[serde(tag = "type")]
94#[cfg_attr(not(target_arch = "wasm32"), expect(clippy::large_enum_variant))]
95pub enum Group {
96 #[serde(rename_all = "camelCase")]
98 FunctionCall {
99 name: Option<String>,
102 function_source_range: SourceRange,
105 unlabeled_arg: Option<OpArg>,
107 labeled_args: IndexMap<String, OpArg>,
109 },
110 #[allow(dead_code)]
112 #[serde(rename_all = "camelCase")]
113 SketchBlock {
114 sketch_id: ObjectId,
116 },
117}
118
119#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
121#[ts(export_to = "Operation.ts")]
122#[serde(rename_all = "camelCase")]
123pub struct OpArg {
124 value: OpKclValue,
127 source_range: SourceRange,
130}
131
132impl OpArg {
133 pub fn new(value: OpKclValue, source_range: SourceRange) -> Self {
134 Self { value, source_range }
135 }
136}
137
138fn is_false(b: &bool) -> bool {
139 !*b
140}
141
142#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
145#[ts(export_to = "Operation.ts")]
146#[serde(tag = "type")]
147pub enum OpKclValue {
148 Uuid {
149 value: ::uuid::Uuid,
150 },
151 Bool {
152 value: bool,
153 },
154 Number {
155 value: f64,
156 ty: NumericType,
157 },
158 String {
159 value: String,
160 },
161 Enum {
164 enum_name: String,
165 variant: String,
166 },
167 SketchVar {
168 value: f64,
169 ty: NumericType,
170 },
171 Array {
172 value: Vec<OpKclValue>,
173 },
174 Object {
175 value: OpKclObjectFields,
176 },
177 TagIdentifier {
178 value: String,
180 artifact_id: Option<ArtifactId>,
182 },
183 TagDeclarator {
184 name: String,
185 },
186 GdtAnnotation {
187 artifact_id: ArtifactId,
188 },
189 Plane {
190 artifact_id: ArtifactId,
191 },
192 Face {
193 artifact_id: ArtifactId,
194 },
195 Sketch {
196 value: Box<OpSketch>,
197 },
198 Segment {
199 artifact_id: ArtifactId,
200 },
201 Solid {
202 value: Box<OpSolid>,
203 },
204 Helix {
205 value: Box<OpHelix>,
206 },
207 ImportedGeometry {
208 artifact_id: ArtifactId,
209 },
210 Function {},
211 Module {},
212 Type {},
213 KclNone {},
214 BoundedEdge {},
215}
216
217pub type OpKclObjectFields = IndexMap<String, OpKclValue>;
218
219#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
220#[ts(export_to = "Operation.ts")]
221#[serde(rename_all = "camelCase")]
222pub struct OpSketch {
223 artifact_id: ArtifactId,
224}
225
226impl OpSketch {
227 pub fn new(artifact_id: ArtifactId) -> Self {
228 Self { artifact_id }
229 }
230}
231
232#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
233#[ts(export_to = "Operation.ts")]
234#[serde(rename_all = "camelCase")]
235pub struct OpSolid {
236 artifact_id: ArtifactId,
237}
238
239impl OpSolid {
240 pub fn new(artifact_id: ArtifactId) -> Self {
241 Self { artifact_id }
242 }
243}
244
245#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
246#[ts(export_to = "Operation.ts")]
247#[serde(rename_all = "camelCase")]
248pub struct OpHelix {
249 artifact_id: ArtifactId,
250}
251
252impl OpHelix {
253 pub fn new(artifact_id: ArtifactId) -> Self {
254 Self { artifact_id }
255 }
256}