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