Skip to main content

kcl_api/
cad_op.rs

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/// A CAD modeling operation for display in the feature tree, AKA operations
15/// timeline.
16#[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        /// The unlabeled argument to the function.
25        unlabeled_arg: Option<OpArg>,
26        /// The labeled keyword arguments to the function.
27        labeled_args: IndexMap<String, OpArg>,
28        /// The node path of the operation in the source code.
29        node_path: NodePath,
30        /// The true source range of the operation in the source code.
31        source_range: SourceRange,
32        /// The source range that's the boundary of calling the standard
33        /// library.
34        #[serde(default, skip_serializing_if = "Option::is_none")]
35        stdlib_entry_source_range: Option<SourceRange>,
36        /// True if the operation resulted in an error.
37        #[serde(default, skip_serializing_if = "is_false")]
38        is_error: bool,
39    },
40    #[serde(rename_all = "camelCase")]
41    VariableDeclaration {
42        /// The variable name.
43        name: String,
44        /// The value of the variable.
45        value: OpKclValue,
46        /// The visibility modifier of the variable, e.g. `export`.  `Default`
47        /// means there is no visibility modifier.
48        visibility: ItemVisibility,
49        /// The node path of the operation in the source code.
50        node_path: NodePath,
51        /// The source range of the operation in the source code.
52        source_range: SourceRange,
53    },
54    #[serde(rename_all = "camelCase")]
55    GroupBegin {
56        /// The details of the group.
57        group: Group,
58        /// The node path of the operation in the source code.
59        node_path: NodePath,
60        /// The source range of the operation in the source code.
61        source_range: SourceRange,
62    },
63    #[serde(rename_all = "camelCase")]
64    ModuleInstance {
65        /// The name of the module being used.
66        name: String,
67        /// The ID of the module which can be used to determine its path.
68        module_id: ModuleId,
69        /// Whether this is a glob import (`import * from "foo.kcl"`).
70        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
71        glob: bool,
72        /// The node path of the operation in the source code.
73        node_path: NodePath,
74        /// The source range of the operation in the source code.
75        source_range: SourceRange,
76    },
77    GroupEnd,
78}
79
80impl Operation {
81    /// If the variant is `StdLibCall`, set the `is_error` field.
82    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    /// A function call.
99    #[serde(rename_all = "camelCase")]
100    FunctionCall {
101        /// The name of the user-defined function being called.  Anonymous
102        /// functions have no name.
103        name: Option<String>,
104        /// The location of the function being called so that there's enough
105        /// info to go to its definition.
106        function_source_range: SourceRange,
107        /// The unlabeled argument to the function.
108        unlabeled_arg: Option<OpArg>,
109        /// The labeled keyword arguments to the function.
110        labeled_args: IndexMap<String, OpArg>,
111    },
112    /// A sketch block.
113    #[allow(dead_code)]
114    #[serde(rename_all = "camelCase")]
115    SketchBlock {
116        /// The ID of the sketch this group wraps.
117        sketch_id: ObjectId,
118    },
119}
120
121/// An argument to a CAD modeling operation.
122#[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    /// The runtime value of the argument.  Instead of using [`KclValue`], we
127    /// refer to scene objects using their [`ArtifactId`]s.
128    value: OpKclValue,
129    /// The KCL code expression for the argument.  This is used in the UI so
130    /// that the user can edit the expression.
131    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/// A KCL value used in Operations.  `ArtifactId`s are used to refer to the
145/// actual scene objects.  Any data not needed in the UI may be omitted.
146#[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    /// Shown in the feature tree by nominal identity, e.g. `Color::Red`, not by
164    /// the variant's representation.
165    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        /// The name of the tag identifier.
181        value: String,
182        /// The artifact ID of the object it refers to.
183        artifact_id: Option<ArtifactId>,
184    },
185    TagDeclarator {
186        name: String,
187    },
188    GdtAnnotation {
189        artifact_id: ArtifactId,
190    },
191    /// A camera view argument. Fieldless because a camera registers no
192    /// artifact: the marker only separates a passed camera from an omitted one.
193    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}