use indexmap::IndexMap;
use kcl_error::SourceRange;
use serde::Serialize;
use super::ArtifactId;
use crate::ModuleId;
use crate::NumericType;
use crate::ast::ItemVisibility;
use crate::ast::node_path::NodePath;
use crate::front::ObjectId;
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Operation.ts")]
#[serde(tag = "type")]
pub enum Operation {
#[serde(rename_all = "camelCase")]
StdLibCall {
name: String,
unlabeled_arg: Option<OpArg>,
labeled_args: IndexMap<String, OpArg>,
node_path: NodePath,
source_range: SourceRange,
#[serde(default, skip_serializing_if = "Option::is_none")]
stdlib_entry_source_range: Option<SourceRange>,
#[serde(default, skip_serializing_if = "is_false")]
is_error: bool,
},
#[serde(rename_all = "camelCase")]
VariableDeclaration {
name: String,
value: OpKclValue,
visibility: ItemVisibility,
node_path: NodePath,
source_range: SourceRange,
},
#[serde(rename_all = "camelCase")]
GroupBegin {
group: Group,
node_path: NodePath,
source_range: SourceRange,
},
#[serde(rename_all = "camelCase")]
ModuleInstance {
name: String,
module_id: ModuleId,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
glob: bool,
node_path: NodePath,
source_range: SourceRange,
},
GroupEnd,
}
impl Operation {
pub fn set_std_lib_call_is_error(&mut self, is_err: bool) {
match self {
Self::StdLibCall { is_error, .. } => *is_error = is_err,
Self::VariableDeclaration { .. }
| Self::GroupBegin { .. }
| Self::ModuleInstance { .. }
| Self::GroupEnd => {}
}
}
}
#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Operation.ts")]
#[serde(tag = "type")]
#[expect(clippy::large_enum_variant)]
pub enum Group {
#[serde(rename_all = "camelCase")]
FunctionCall {
name: Option<String>,
function_source_range: SourceRange,
unlabeled_arg: Option<OpArg>,
labeled_args: IndexMap<String, OpArg>,
},
#[allow(dead_code)]
#[serde(rename_all = "camelCase")]
SketchBlock {
sketch_id: ObjectId,
},
}
#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Operation.ts")]
#[serde(rename_all = "camelCase")]
pub struct OpArg {
value: OpKclValue,
source_range: SourceRange,
}
impl OpArg {
pub fn new(value: OpKclValue, source_range: SourceRange) -> Self {
Self { value, source_range }
}
}
fn is_false(b: &bool) -> bool {
!*b
}
#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Operation.ts")]
#[serde(tag = "type")]
pub enum OpKclValue {
Uuid {
value: ::uuid::Uuid,
},
Bool {
value: bool,
},
Number {
value: f64,
ty: NumericType,
},
String {
value: String,
},
SketchVar {
value: f64,
ty: NumericType,
},
Array {
value: Vec<OpKclValue>,
},
Object {
value: OpKclObjectFields,
},
TagIdentifier {
value: String,
artifact_id: Option<ArtifactId>,
},
TagDeclarator {
name: String,
},
GdtAnnotation {
artifact_id: ArtifactId,
},
Plane {
artifact_id: ArtifactId,
},
Face {
artifact_id: ArtifactId,
},
Sketch {
value: Box<OpSketch>,
},
Segment {
artifact_id: ArtifactId,
},
Solid {
value: Box<OpSolid>,
},
Helix {
value: Box<OpHelix>,
},
ImportedGeometry {
artifact_id: ArtifactId,
},
Function {},
Module {},
Type {},
KclNone {},
BoundedEdge {},
}
pub type OpKclObjectFields = IndexMap<String, OpKclValue>;
#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Operation.ts")]
#[serde(rename_all = "camelCase")]
pub struct OpSketch {
artifact_id: ArtifactId,
}
impl OpSketch {
pub fn new(artifact_id: ArtifactId) -> Self {
Self { artifact_id }
}
}
#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Operation.ts")]
#[serde(rename_all = "camelCase")]
pub struct OpSolid {
artifact_id: ArtifactId,
}
impl OpSolid {
pub fn new(artifact_id: ArtifactId) -> Self {
Self { artifact_id }
}
}
#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Operation.ts")]
#[serde(rename_all = "camelCase")]
pub struct OpHelix {
artifact_id: ArtifactId,
}
impl OpHelix {
pub fn new(artifact_id: ArtifactId) -> Self {
Self { artifact_id }
}
}