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 CameraView {},
192 Plane {
193 artifact_id: ArtifactId,
194 },
195 Face {
196 artifact_id: ArtifactId,
197 },
198 Sketch {
199 value: Box<OpSketch>,
200 },
201 Segment {
202 artifact_id: ArtifactId,
203 },
204 Solid {
205 value: Box<OpSolid>,
206 },
207 Helix {
208 value: Box<OpHelix>,
209 },
210 ImportedGeometry {
211 artifact_id: ArtifactId,
212 },
213 Function {},
214 Module {},
215 Type {},
216 KclNone {},
217 BoundedEdge {},
218}
219
220pub type OpKclObjectFields = IndexMap<String, OpKclValue>;
221
222#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
223#[ts(export_to = "Operation.ts")]
224#[serde(rename_all = "camelCase")]
225pub struct OpSketch {
226 artifact_id: ArtifactId,
227}
228
229impl OpSketch {
230 pub fn new(artifact_id: ArtifactId) -> Self {
231 Self { artifact_id }
232 }
233}
234
235#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
236#[ts(export_to = "Operation.ts")]
237#[serde(rename_all = "camelCase")]
238pub struct OpSolid {
239 artifact_id: ArtifactId,
240}
241
242impl OpSolid {
243 pub fn new(artifact_id: ArtifactId) -> Self {
244 Self { artifact_id }
245 }
246}
247
248#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
249#[ts(export_to = "Operation.ts")]
250#[serde(rename_all = "camelCase")]
251pub struct OpHelix {
252 artifact_id: ArtifactId,
253}
254
255impl OpHelix {
256 pub fn new(artifact_id: ArtifactId) -> Self {
257 Self { artifact_id }
258 }
259}