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#[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 SketchVar {
162 value: f64,
163 ty: NumericType,
164 },
165 Array {
166 value: Vec<OpKclValue>,
167 },
168 Object {
169 value: OpKclObjectFields,
170 },
171 TagIdentifier {
172 value: String,
174 artifact_id: Option<ArtifactId>,
176 },
177 TagDeclarator {
178 name: String,
179 },
180 GdtAnnotation {
181 artifact_id: ArtifactId,
182 },
183 Plane {
184 artifact_id: ArtifactId,
185 },
186 Face {
187 artifact_id: ArtifactId,
188 },
189 Sketch {
190 value: Box<OpSketch>,
191 },
192 Segment {
193 artifact_id: ArtifactId,
194 },
195 Solid {
196 value: Box<OpSolid>,
197 },
198 Helix {
199 value: Box<OpHelix>,
200 },
201 ImportedGeometry {
202 artifact_id: ArtifactId,
203 },
204 Function {},
205 Module {},
206 Type {},
207 KclNone {},
208 BoundedEdge {},
209}
210
211pub type OpKclObjectFields = IndexMap<String, OpKclValue>;
212
213#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
214#[ts(export_to = "Operation.ts")]
215#[serde(rename_all = "camelCase")]
216pub struct OpSketch {
217 artifact_id: ArtifactId,
218}
219
220impl OpSketch {
221 pub fn new(artifact_id: ArtifactId) -> Self {
222 Self { artifact_id }
223 }
224}
225
226#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
227#[ts(export_to = "Operation.ts")]
228#[serde(rename_all = "camelCase")]
229pub struct OpSolid {
230 artifact_id: ArtifactId,
231}
232
233impl OpSolid {
234 pub fn new(artifact_id: ArtifactId) -> Self {
235 Self { artifact_id }
236 }
237}
238
239#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
240#[ts(export_to = "Operation.ts")]
241#[serde(rename_all = "camelCase")]
242pub struct OpHelix {
243 artifact_id: ArtifactId,
244}
245
246impl OpHelix {
247 pub fn new(artifact_id: ArtifactId) -> Self {
248 Self { artifact_id }
249 }
250}