1use std::collections::HashMap;
2
3use anyhow::Result;
4use indexmap::IndexMap;
5use kittycad_modeling_cmds::units::UnitLength;
6use serde::Serialize;
7
8use crate::CompilationIssue;
9use crate::KclError;
10use crate::ModuleId;
11use crate::SourceRange;
12use crate::errors::KclErrorDetails;
13use crate::execution::AbstractSegment;
14use crate::execution::BoundedEdge;
15use crate::execution::EnvironmentRef;
16use crate::execution::ExecState;
17use crate::execution::Face;
18use crate::execution::GdtAnnotation;
19use crate::execution::Geometry;
20use crate::execution::GeometryWithImportedGeometry;
21use crate::execution::Helix;
22use crate::execution::ImportedGeometry;
23use crate::execution::Metadata;
24use crate::execution::Plane;
25use crate::execution::Segment;
26use crate::execution::SegmentRepr;
27use crate::execution::Sketch;
28use crate::execution::SketchConstraint;
29use crate::execution::SketchVar;
30use crate::execution::SketchVarId;
31use crate::execution::Solid;
32use crate::execution::TagIdentifier;
33use crate::execution::UnsolvedExpr;
34use crate::execution::annotations::FnAttrs;
35use crate::execution::annotations::SETTINGS;
36use crate::execution::annotations::SETTINGS_UNIT_LENGTH;
37use crate::execution::annotations::VersionConstraint;
38use crate::execution::annotations::{self};
39use crate::execution::types::NumericType;
40use crate::execution::types::PrimitiveType;
41use crate::execution::types::RuntimeType;
42use crate::parsing::ast::types::DefaultParamVal;
43use crate::parsing::ast::types::FunctionExpression;
44use crate::parsing::ast::types::KclNone;
45use crate::parsing::ast::types::Literal;
46use crate::parsing::ast::types::LiteralValue;
47use crate::parsing::ast::types::Node;
48use crate::parsing::ast::types::NumericLiteral;
49use crate::parsing::ast::types::TagDeclarator;
50use crate::parsing::ast::types::TagNode;
51use crate::parsing::ast::types::Type;
52use crate::std::StdFnProps;
53use crate::std::args::TyF64;
54
55pub type KclObjectFields = HashMap<String, KclValue>;
56
57#[derive(Debug, Clone, Serialize, PartialEq, ts_rs::TS)]
59#[ts(export)]
60#[serde(tag = "type")]
61pub enum KclValue {
62 Uuid {
63 value: ::uuid::Uuid,
64 #[serde(skip)]
65 meta: Vec<Metadata>,
66 },
67 Bool {
68 value: bool,
69 #[serde(skip)]
70 meta: Vec<Metadata>,
71 },
72 Number {
73 value: f64,
74 ty: NumericType,
75 #[serde(skip)]
76 meta: Vec<Metadata>,
77 },
78 String {
79 value: String,
80 #[serde(skip)]
81 meta: Vec<Metadata>,
82 },
83 SketchVar {
84 value: Box<SketchVar>,
85 },
86 SketchConstraint {
87 value: Box<SketchConstraint>,
88 },
89 Tuple {
90 value: Vec<KclValue>,
91 #[serde(skip)]
92 meta: Vec<Metadata>,
93 },
94 HomArray {
96 value: Vec<KclValue>,
97 #[serde(skip)]
99 ty: RuntimeType,
100 },
101 Object {
102 value: KclObjectFields,
103 constrainable: bool,
104 #[serde(skip)]
105 meta: Vec<Metadata>,
106 },
107 TagIdentifier(Box<TagIdentifier>),
108 TagDeclarator(crate::parsing::ast::types::BoxNode<TagDeclarator>),
109 GdtAnnotation {
110 value: Box<GdtAnnotation>,
111 },
112 Plane {
113 value: Box<Plane>,
114 },
115 Face {
116 value: Box<Face>,
117 },
118 BoundedEdge {
119 value: BoundedEdge,
120 meta: Vec<Metadata>,
121 },
122 Segment {
123 value: Box<AbstractSegment>,
124 },
125 Sketch {
126 value: Box<Sketch>,
127 },
128 Solid {
129 value: Box<Solid>,
130 },
131 Helix {
132 value: Box<Helix>,
133 },
134 ImportedGeometry(ImportedGeometry),
135 Function {
136 #[serde(serialize_with = "function_value_stub")]
137 #[ts(type = "null")]
138 value: Box<FunctionSource>,
139 #[serde(skip)]
140 meta: Vec<Metadata>,
141 },
142 Module {
143 value: ModuleId,
144 #[serde(skip)]
145 meta: Vec<Metadata>,
146 },
147 #[ts(skip)]
148 Type {
149 #[serde(skip)]
150 value: TypeDef,
151 experimental: bool,
152 #[serde(skip)]
153 meta: Vec<Metadata>,
154 },
155 KclNone {
156 value: KclNone,
157 #[serde(skip)]
158 meta: Vec<Metadata>,
159 },
160}
161
162fn function_value_stub<S>(_value: &FunctionSource, serializer: S) -> Result<S::Ok, S::Error>
163where
164 S: serde::Serializer,
165{
166 serializer.serialize_unit()
167}
168
169#[derive(Debug, Clone, PartialEq)]
170pub struct NamedParam {
171 pub experimental: bool,
172 pub deprecated_since: Option<VersionConstraint>,
174 pub default_value: Option<DefaultParamVal>,
175 pub ty: Option<Type>,
176}
177
178#[derive(Debug, Clone, PartialEq)]
179pub struct FunctionSource {
180 pub input_arg: Option<(String, Option<Type>)>,
181 pub named_args: IndexMap<String, NamedParam>,
182 pub return_type: Option<Node<Type>>,
183 pub deprecated: bool,
184 pub deprecated_since: Option<VersionConstraint>,
188 pub experimental: bool,
189 pub include_in_feature_tree: bool,
190 pub std_props: Option<StdFnProps>,
191 pub body: FunctionBody,
192 pub ast: crate::parsing::ast::types::BoxNode<FunctionExpression>,
193}
194
195pub struct KclFunctionSourceParams {
196 pub std_props: Option<StdFnProps>,
197 pub experimental: bool,
198 pub include_in_feature_tree: bool,
199}
200
201impl FunctionSource {
202 pub fn rust(
203 func: crate::std::StdFn,
204 ast: Box<Node<FunctionExpression>>,
205 props: StdFnProps,
206 attrs: FnAttrs,
207 ) -> Self {
208 let (input_arg, named_args) = Self::args_from_ast(&ast);
209
210 FunctionSource {
211 input_arg,
212 named_args,
213 return_type: ast.return_type.clone(),
214 deprecated: attrs.deprecated,
215 deprecated_since: attrs.deprecated_since,
216 experimental: attrs.experimental,
217 include_in_feature_tree: attrs.include_in_feature_tree,
218 std_props: Some(props),
219 body: FunctionBody::Rust(func),
220 ast,
221 }
222 }
223
224 pub fn kcl(ast: Box<Node<FunctionExpression>>, memory: EnvironmentRef, params: KclFunctionSourceParams) -> Self {
225 let KclFunctionSourceParams {
226 std_props,
227 experimental,
228 include_in_feature_tree,
229 } = params;
230 let (input_arg, named_args) = Self::args_from_ast(&ast);
231 FunctionSource {
232 input_arg,
233 named_args,
234 return_type: ast.return_type.clone(),
235 deprecated: false,
236 deprecated_since: None,
237 experimental,
238 include_in_feature_tree,
239 std_props,
240 body: FunctionBody::Kcl(memory),
241 ast,
242 }
243 }
244
245 #[expect(clippy::type_complexity)]
246 fn args_from_ast(ast: &FunctionExpression) -> (Option<(String, Option<Type>)>, IndexMap<String, NamedParam>) {
247 let mut input_arg = None;
248 let mut named_args = IndexMap::new();
249 for p in &ast.params {
250 if !p.labeled {
251 input_arg = Some((
252 p.identifier.name.clone(),
253 p.param_type.as_ref().map(|t| t.inner.clone()),
254 ));
255 continue;
256 }
257
258 named_args.insert(
259 p.identifier.name.clone(),
260 NamedParam {
261 experimental: p.experimental,
262 deprecated_since: p.deprecated_since.clone(),
263 default_value: p.default_value.clone(),
264 ty: p.param_type.as_ref().map(|t| t.inner.clone()),
265 },
266 );
267 }
268
269 (input_arg, named_args)
270 }
271
272 pub(crate) fn is_std(&self) -> bool {
273 self.std_props.is_some()
274 }
275}
276
277#[derive(Debug, Clone, PartialEq)]
278#[allow(unpredictable_function_pointer_comparisons)]
281pub enum FunctionBody {
282 Rust(crate::std::StdFn),
283 Kcl(EnvironmentRef),
284}
285
286#[derive(Debug, Clone, PartialEq)]
287pub enum TypeDef {
288 RustRepr(PrimitiveType, StdFnProps),
289 Alias(RuntimeType),
290}
291
292impl From<Vec<GdtAnnotation>> for KclValue {
293 fn from(mut values: Vec<GdtAnnotation>) -> Self {
294 if values.len() == 1 {
295 let value = values.pop().expect("Just checked len == 1");
296 KclValue::GdtAnnotation { value: Box::new(value) }
297 } else {
298 KclValue::HomArray {
299 value: values
300 .into_iter()
301 .map(|s| KclValue::GdtAnnotation { value: Box::new(s) })
302 .collect(),
303 ty: RuntimeType::Primitive(PrimitiveType::GdtAnnotation),
304 }
305 }
306 }
307}
308
309impl From<Vec<Sketch>> for KclValue {
310 fn from(mut eg: Vec<Sketch>) -> Self {
311 if eg.len() == 1
312 && let Some(s) = eg.pop()
313 {
314 KclValue::Sketch { value: Box::new(s) }
315 } else {
316 KclValue::HomArray {
317 value: eg
318 .into_iter()
319 .map(|s| KclValue::Sketch { value: Box::new(s) })
320 .collect(),
321 ty: RuntimeType::Primitive(PrimitiveType::Sketch),
322 }
323 }
324 }
325}
326
327impl From<Vec<Solid>> for KclValue {
328 fn from(mut eg: Vec<Solid>) -> Self {
329 if eg.len() == 1
330 && let Some(s) = eg.pop()
331 {
332 KclValue::Solid { value: Box::new(s) }
333 } else {
334 KclValue::HomArray {
335 value: eg.into_iter().map(|s| KclValue::Solid { value: Box::new(s) }).collect(),
336 ty: RuntimeType::Primitive(PrimitiveType::Solid),
337 }
338 }
339 }
340}
341
342impl From<KclValue> for Vec<SourceRange> {
343 fn from(item: KclValue) -> Self {
344 match item {
345 KclValue::TagDeclarator(t) => vec![SourceRange::new(t.start, t.end, t.module_id)],
346 KclValue::TagIdentifier(t) => to_vec_sr(&t.meta),
347 KclValue::GdtAnnotation { value } => to_vec_sr(&value.meta),
348 KclValue::Solid { value } => to_vec_sr(&value.meta),
349 KclValue::Sketch { value } => to_vec_sr(&value.meta),
350 KclValue::Helix { value } => to_vec_sr(&value.meta),
351 KclValue::ImportedGeometry(i) => to_vec_sr(&i.meta),
352 KclValue::Function { meta, .. } => to_vec_sr(&meta),
353 KclValue::Plane { value } => to_vec_sr(&value.meta),
354 KclValue::Face { value } => to_vec_sr(&value.meta),
355 KclValue::Segment { value } => to_vec_sr(&value.meta),
356 KclValue::Bool { meta, .. } => to_vec_sr(&meta),
357 KclValue::Number { meta, .. } => to_vec_sr(&meta),
358 KclValue::String { meta, .. } => to_vec_sr(&meta),
359 KclValue::SketchVar { value, .. } => to_vec_sr(&value.meta),
360 KclValue::SketchConstraint { value, .. } => to_vec_sr(&value.meta),
361 KclValue::Tuple { meta, .. } => to_vec_sr(&meta),
362 KclValue::HomArray { value, .. } => value.iter().flat_map(Into::<Vec<SourceRange>>::into).collect(),
363 KclValue::Object { meta, .. } => to_vec_sr(&meta),
364 KclValue::Module { meta, .. } => to_vec_sr(&meta),
365 KclValue::Uuid { meta, .. } => to_vec_sr(&meta),
366 KclValue::Type { meta, .. } => to_vec_sr(&meta),
367 KclValue::KclNone { meta, .. } => to_vec_sr(&meta),
368 KclValue::BoundedEdge { meta, .. } => to_vec_sr(&meta),
369 }
370 }
371}
372
373fn to_vec_sr(meta: &[Metadata]) -> Vec<SourceRange> {
374 meta.iter().map(|m| m.source_range).collect()
375}
376
377impl From<&KclValue> for Vec<SourceRange> {
378 fn from(item: &KclValue) -> Self {
379 match item {
380 KclValue::TagDeclarator(t) => vec![SourceRange::new(t.start, t.end, t.module_id)],
381 KclValue::TagIdentifier(t) => to_vec_sr(&t.meta),
382 KclValue::GdtAnnotation { value } => to_vec_sr(&value.meta),
383 KclValue::Solid { value } => to_vec_sr(&value.meta),
384 KclValue::Sketch { value } => to_vec_sr(&value.meta),
385 KclValue::Helix { value } => to_vec_sr(&value.meta),
386 KclValue::ImportedGeometry(i) => to_vec_sr(&i.meta),
387 KclValue::Function { meta, .. } => to_vec_sr(meta),
388 KclValue::Plane { value } => to_vec_sr(&value.meta),
389 KclValue::Face { value } => to_vec_sr(&value.meta),
390 KclValue::Segment { value } => to_vec_sr(&value.meta),
391 KclValue::Bool { meta, .. } => to_vec_sr(meta),
392 KclValue::Number { meta, .. } => to_vec_sr(meta),
393 KclValue::String { meta, .. } => to_vec_sr(meta),
394 KclValue::SketchVar { value, .. } => to_vec_sr(&value.meta),
395 KclValue::SketchConstraint { value, .. } => to_vec_sr(&value.meta),
396 KclValue::Uuid { meta, .. } => to_vec_sr(meta),
397 KclValue::Tuple { meta, .. } => to_vec_sr(meta),
398 KclValue::HomArray { value, .. } => value.iter().flat_map(Into::<Vec<SourceRange>>::into).collect(),
399 KclValue::Object { meta, .. } => to_vec_sr(meta),
400 KclValue::Module { meta, .. } => to_vec_sr(meta),
401 KclValue::KclNone { meta, .. } => to_vec_sr(meta),
402 KclValue::Type { meta, .. } => to_vec_sr(meta),
403 KclValue::BoundedEdge { meta, .. } => to_vec_sr(meta),
404 }
405 }
406}
407
408impl From<&KclValue> for SourceRange {
409 fn from(item: &KclValue) -> Self {
410 let v: Vec<_> = item.into();
411 v.into_iter().next().unwrap_or_default()
412 }
413}
414
415impl KclValue {
416 pub(crate) fn metadata(&self) -> Vec<Metadata> {
417 match self {
418 KclValue::Uuid { value: _, meta } => meta.clone(),
419 KclValue::Bool { value: _, meta } => meta.clone(),
420 KclValue::Number { meta, .. } => meta.clone(),
421 KclValue::String { value: _, meta } => meta.clone(),
422 KclValue::SketchVar { value, .. } => value.meta.clone(),
423 KclValue::SketchConstraint { value, .. } => value.meta.clone(),
424 KclValue::Tuple { value: _, meta } => meta.clone(),
425 KclValue::HomArray { value, .. } => value.iter().flat_map(|v| v.metadata()).collect(),
426 KclValue::Object { meta, .. } => meta.clone(),
427 KclValue::TagIdentifier(x) => x.meta.clone(),
428 KclValue::TagDeclarator(x) => vec![x.metadata()],
429 KclValue::GdtAnnotation { value } => value.meta.clone(),
430 KclValue::Plane { value } => value.meta.clone(),
431 KclValue::Face { value } => value.meta.clone(),
432 KclValue::Segment { value } => value.meta.clone(),
433 KclValue::Sketch { value } => value.meta.clone(),
434 KclValue::Solid { value } => value.meta.clone(),
435 KclValue::Helix { value } => value.meta.clone(),
436 KclValue::ImportedGeometry(x) => x.meta.clone(),
437 KclValue::Function { meta, .. } => meta.clone(),
438 KclValue::Module { meta, .. } => meta.clone(),
439 KclValue::KclNone { meta, .. } => meta.clone(),
440 KclValue::Type { meta, .. } => meta.clone(),
441 KclValue::BoundedEdge { meta, .. } => meta.clone(),
442 }
443 }
444
445 #[allow(unused)]
446 pub(crate) fn none() -> Self {
447 Self::KclNone {
448 value: Default::default(),
449 meta: Default::default(),
450 }
451 }
452
453 pub(crate) fn show_variable_in_feature_tree(&self) -> bool {
457 match self {
458 KclValue::Uuid { .. } => false,
459 KclValue::Bool { .. } | KclValue::Number { .. } | KclValue::String { .. } => true,
460 KclValue::SketchVar { .. }
461 | KclValue::SketchConstraint { .. }
462 | KclValue::Tuple { .. }
463 | KclValue::HomArray { .. }
464 | KclValue::Object { .. }
465 | KclValue::TagIdentifier(_)
466 | KclValue::TagDeclarator(_)
467 | KclValue::GdtAnnotation { .. }
468 | KclValue::Plane { .. }
469 | KclValue::Face { .. }
470 | KclValue::Segment { .. }
471 | KclValue::Sketch { .. }
472 | KclValue::Solid { .. }
473 | KclValue::Helix { .. }
474 | KclValue::ImportedGeometry(_)
475 | KclValue::Function { .. }
476 | KclValue::Module { .. }
477 | KclValue::Type { .. }
478 | KclValue::BoundedEdge { .. }
479 | KclValue::KclNone { .. } => false,
480 }
481 }
482
483 pub(crate) fn human_friendly_type(&self) -> String {
486 match self {
487 KclValue::Uuid { .. } => "a unique ID (uuid)".to_owned(),
488 KclValue::TagDeclarator(_) => "a tag declarator".to_owned(),
489 KclValue::TagIdentifier(_) => "a tag identifier".to_owned(),
490 KclValue::GdtAnnotation { .. } => "an annotation".to_owned(),
491 KclValue::Solid { .. } => "a solid".to_owned(),
492 KclValue::Sketch { .. } => "a sketch".to_owned(),
493 KclValue::Helix { .. } => "a helix".to_owned(),
494 KclValue::ImportedGeometry(_) => "an imported geometry".to_owned(),
495 KclValue::Function { .. } => "a function".to_owned(),
496 KclValue::Plane { .. } => "a plane".to_owned(),
497 KclValue::Face { .. } => "a face".to_owned(),
498 KclValue::Segment { .. } => "a segment".to_owned(),
499 KclValue::Bool { .. } => "a boolean (`true` or `false`)".to_owned(),
500 KclValue::Number {
501 ty: NumericType::Unknown,
502 ..
503 } => "a number with unknown units".to_owned(),
504 KclValue::Number {
505 ty: NumericType::Known(units),
506 ..
507 } => format!("a number ({units})"),
508 KclValue::Number { .. } => "a number".to_owned(),
509 KclValue::String { .. } => "a string".to_owned(),
510 KclValue::SketchVar { .. } => "a sketch variable".to_owned(),
511 KclValue::SketchConstraint { .. } => "a sketch constraint".to_owned(),
512 KclValue::Object { .. } => "an object".to_owned(),
513 KclValue::Module { .. } => "a module".to_owned(),
514 KclValue::Type { .. } => "a type".to_owned(),
515 KclValue::KclNone { .. } => "none".to_owned(),
516 KclValue::BoundedEdge { .. } => "a bounded edge".to_owned(),
517 KclValue::Tuple { value, .. } | KclValue::HomArray { value, .. } => {
518 if value.is_empty() {
519 "an empty array".to_owned()
520 } else {
521 const MAX: usize = 3;
523
524 let len = value.len();
525 let element_tys = value
526 .iter()
527 .take(MAX)
528 .map(|elem| elem.principal_type_string())
529 .collect::<Vec<_>>()
530 .join(", ");
531 let mut result = format!("an array of {element_tys}");
532 if len > MAX {
533 result.push_str(&format!(", ... with {len} values"));
534 }
535 if len == 1 {
536 result.push_str(" with 1 value");
537 }
538 result
539 }
540 }
541 }
542 }
543
544 pub(crate) fn from_sketch_var_literal(
545 literal: &Node<NumericLiteral>,
546 id: SketchVarId,
547 exec_state: &ExecState,
548 ) -> Self {
549 let meta = vec![literal.metadata()];
550 let ty = NumericType::from_parsed(literal.suffix, &exec_state.mod_local.settings);
551 KclValue::SketchVar {
552 value: Box::new(SketchVar {
553 id,
554 initial_value: literal.value,
555 meta,
556 ty,
557 }),
558 }
559 }
560
561 pub(crate) fn from_literal(literal: Node<Literal>, exec_state: &mut ExecState) -> Self {
562 let meta = vec![literal.metadata()];
563 match literal.inner.value {
564 LiteralValue::Number { value, suffix } => {
565 let ty = NumericType::from_parsed(suffix, &exec_state.mod_local.settings);
566 if let NumericType::Default { len, .. } = &ty
567 && !exec_state.mod_local.explicit_length_units
568 && *len != UnitLength::Millimeters
569 {
570 exec_state.warn(
571 CompilationIssue::err(
572 literal.as_source_range(),
573 "Project-wide units are deprecated. Prefer to use per-file default units.",
574 )
575 .with_suggestion(
576 "Fix by adding per-file settings",
577 format!("@{SETTINGS}({SETTINGS_UNIT_LENGTH} = {len})\n"),
578 Some(SourceRange::new(0, 0, literal.module_id)),
580 crate::errors::Tag::Deprecated,
581 ),
582 annotations::WARN_DEPRECATED,
583 );
584 }
585 KclValue::Number { value, meta, ty }
586 }
587 LiteralValue::String(value) => KclValue::String { value, meta },
588 LiteralValue::Bool(value) => KclValue::Bool { value, meta },
589 }
590 }
591
592 pub(crate) fn from_default_param(param: DefaultParamVal, exec_state: &mut ExecState) -> Self {
593 match param {
594 DefaultParamVal::Literal(lit) => Self::from_literal(lit, exec_state),
595 DefaultParamVal::KclNone(value) => KclValue::KclNone {
596 value,
597 meta: Default::default(),
598 },
599 }
600 }
601
602 pub(crate) fn map_env_ref(&self, old_env: EnvironmentRef, new_env: EnvironmentRef) -> Self {
603 let mut result = self.clone();
604 if let KclValue::Function { ref mut value, .. } = result
605 && let FunctionSource {
606 body: FunctionBody::Kcl(memory),
607 ..
608 } = &mut **value
609 {
610 memory.replace_env(old_env, new_env);
611 }
612
613 result
614 }
615
616 pub(crate) fn map_env_ref_and_epoch(&self, old_env: EnvironmentRef, new_env: EnvironmentRef) -> Self {
617 let mut result = self.clone();
618 if let KclValue::Function { ref mut value, .. } = result
619 && let FunctionSource {
620 body: FunctionBody::Kcl(memory),
621 ..
622 } = &mut **value
623 {
624 memory.replace_env_and_epoch(old_env, new_env);
625 }
626
627 result
628 }
629
630 pub const fn from_number_with_type(f: f64, ty: NumericType, meta: Vec<Metadata>) -> Self {
631 Self::Number { value: f, meta, ty }
632 }
633
634 pub fn from_point2d(p: [f64; 2], ty: NumericType, meta: Vec<Metadata>) -> Self {
636 let [x, y] = p;
637 Self::Tuple {
638 value: vec![
639 Self::Number {
640 value: x,
641 meta: meta.clone(),
642 ty,
643 },
644 Self::Number {
645 value: y,
646 meta: meta.clone(),
647 ty,
648 },
649 ],
650 meta,
651 }
652 }
653
654 pub fn from_point3d(p: [f64; 3], ty: NumericType, meta: Vec<Metadata>) -> Self {
656 let [x, y, z] = p;
657 Self::Tuple {
658 value: vec![
659 Self::Number {
660 value: x,
661 meta: meta.clone(),
662 ty,
663 },
664 Self::Number {
665 value: y,
666 meta: meta.clone(),
667 ty,
668 },
669 Self::Number {
670 value: z,
671 meta: meta.clone(),
672 ty,
673 },
674 ],
675 meta,
676 }
677 }
678
679 pub(crate) fn array_from_point2d(p: [f64; 2], ty: NumericType, meta: Vec<Metadata>) -> Self {
681 let [x, y] = p;
682 Self::HomArray {
683 value: vec![
684 Self::Number {
685 value: x,
686 meta: meta.clone(),
687 ty,
688 },
689 Self::Number { value: y, meta, ty },
690 ],
691 ty: ty.into(),
692 }
693 }
694
695 pub fn array_from_point3d(p: [f64; 3], ty: NumericType, meta: Vec<Metadata>) -> Self {
697 let [x, y, z] = p;
698 Self::HomArray {
699 value: vec![
700 Self::Number {
701 value: x,
702 meta: meta.clone(),
703 ty,
704 },
705 Self::Number {
706 value: y,
707 meta: meta.clone(),
708 ty,
709 },
710 Self::Number { value: z, meta, ty },
711 ],
712 ty: ty.into(),
713 }
714 }
715
716 pub(crate) fn from_unsolved_expr(expr: UnsolvedExpr, meta: Vec<Metadata>) -> Self {
717 match expr {
718 UnsolvedExpr::Known(v) => crate::execution::KclValue::Number {
719 value: v.n,
720 ty: v.ty,
721 meta,
722 },
723 UnsolvedExpr::Unknown(var_id) => crate::execution::KclValue::SketchVar {
724 value: Box::new(SketchVar {
725 id: var_id,
726 initial_value: Default::default(),
727 ty: Default::default(),
729 meta,
730 }),
731 },
732 }
733 }
734
735 pub(crate) fn as_usize(&self) -> Option<usize> {
736 match self {
737 KclValue::Number { value, .. } => crate::try_f64_to_usize(*value),
738 _ => None,
739 }
740 }
741
742 pub fn as_int(&self) -> Option<i64> {
743 match self {
744 KclValue::Number { value, .. } => crate::try_f64_to_i64(*value),
745 _ => None,
746 }
747 }
748
749 pub fn as_int_with_ty(&self) -> Option<(i64, NumericType)> {
750 match self {
751 KclValue::Number { value, ty, .. } => crate::try_f64_to_i64(*value).map(|i| (i, *ty)),
752 _ => None,
753 }
754 }
755
756 pub fn as_object(&self) -> Option<&KclObjectFields> {
757 match self {
758 KclValue::Object { value, .. } => Some(value),
759 _ => None,
760 }
761 }
762
763 pub fn into_object(self) -> Option<KclObjectFields> {
764 match self {
765 KclValue::Object { value, .. } => Some(value),
766 _ => None,
767 }
768 }
769
770 pub fn as_unsolved_expr(&self) -> Option<UnsolvedExpr> {
771 match self {
772 KclValue::Number { value, ty, .. } => Some(UnsolvedExpr::Known(TyF64::new(*value, *ty))),
773 KclValue::SketchVar { value, .. } => Some(UnsolvedExpr::Unknown(value.id)),
774 _ => None,
775 }
776 }
777
778 pub fn to_sketch_expr(&self) -> Option<crate::front::Expr> {
779 match self {
780 KclValue::Number { value, ty, .. } => Some(crate::front::Expr::Number(crate::front::Number {
781 value: *value,
782 units: (*ty).try_into().ok()?,
783 })),
784 KclValue::SketchVar { value, .. } => Some(crate::front::Expr::Var(crate::front::Number {
785 value: value.initial_value,
786 units: value.ty.try_into().ok()?,
787 })),
788 _ => None,
789 }
790 }
791
792 pub fn as_str(&self) -> Option<&str> {
793 match self {
794 KclValue::String { value, .. } => Some(value),
795 _ => None,
796 }
797 }
798
799 pub fn into_array(self) -> Vec<KclValue> {
800 match self {
801 KclValue::Tuple { value, .. } | KclValue::HomArray { value, .. } => value,
802 _ => vec![self],
803 }
804 }
805
806 pub fn as_point2d(&self) -> Option<[TyF64; 2]> {
807 let value = match self {
808 KclValue::Tuple { value, .. } | KclValue::HomArray { value, .. } => value,
809 _ => return None,
810 };
811
812 let [x, y] = value.as_slice() else {
813 return None;
814 };
815 let x = x.as_ty_f64()?;
816 let y = y.as_ty_f64()?;
817 Some([x, y])
818 }
819
820 pub fn as_point3d(&self) -> Option<[TyF64; 3]> {
821 let value = match self {
822 KclValue::Tuple { value, .. } | KclValue::HomArray { value, .. } => value,
823 _ => return None,
824 };
825
826 let [x, y, z] = value.as_slice() else {
827 return None;
828 };
829 let x = x.as_ty_f64()?;
830 let y = y.as_ty_f64()?;
831 let z = z.as_ty_f64()?;
832 Some([x, y, z])
833 }
834
835 pub fn as_uuid(&self) -> Option<uuid::Uuid> {
836 match self {
837 KclValue::Uuid { value, .. } => Some(*value),
838 _ => None,
839 }
840 }
841
842 pub fn as_plane(&self) -> Option<&Plane> {
843 match self {
844 KclValue::Plane { value, .. } => Some(value),
845 _ => None,
846 }
847 }
848
849 pub fn as_solid(&self) -> Option<&Solid> {
850 match self {
851 KclValue::Solid { value, .. } => Some(value),
852 _ => None,
853 }
854 }
855
856 pub fn as_sketch(&self) -> Option<&Sketch> {
857 match self {
858 KclValue::Sketch { value, .. } => Some(value),
859 _ => None,
860 }
861 }
862
863 pub fn as_mut_sketch(&mut self) -> Option<&mut Sketch> {
864 match self {
865 KclValue::Sketch { value } => Some(value),
866 _ => None,
867 }
868 }
869
870 pub fn as_sketch_var(&self) -> Option<&SketchVar> {
871 match self {
872 KclValue::SketchVar { value, .. } => Some(value),
873 _ => None,
874 }
875 }
876
877 pub fn as_segment(&self) -> Option<&Segment> {
879 match self {
880 KclValue::Segment { value, .. } => match &value.repr {
881 SegmentRepr::Solved { segment } => Some(segment),
882 _ => None,
883 },
884 _ => None,
885 }
886 }
887
888 pub fn into_segment(self) -> Option<Segment> {
890 match self {
891 KclValue::Segment { value, .. } => match value.repr {
892 SegmentRepr::Solved { segment } => Some(*segment),
893 _ => None,
894 },
895 _ => None,
896 }
897 }
898
899 pub fn as_mut_tag(&mut self) -> Option<&mut TagIdentifier> {
900 match self {
901 KclValue::TagIdentifier(value) => Some(value),
902 _ => None,
903 }
904 }
905
906 #[cfg(test)]
907 pub fn as_f64(&self) -> Option<f64> {
908 match self {
909 KclValue::Number { value, .. } => Some(*value),
910 _ => None,
911 }
912 }
913
914 pub fn as_ty_f64(&self) -> Option<TyF64> {
915 match self {
916 KclValue::Number { value, ty, .. } => Some(TyF64::new(*value, *ty)),
917 _ => None,
918 }
919 }
920
921 pub fn as_bool(&self) -> Option<bool> {
922 match self {
923 KclValue::Bool { value, .. } => Some(*value),
924 _ => None,
925 }
926 }
927
928 pub fn as_function(&self) -> Option<&FunctionSource> {
930 match self {
931 KclValue::Function { value, .. } => Some(value),
932 _ => None,
933 }
934 }
935
936 pub fn get_tag_identifier(&self) -> Result<TagIdentifier, KclError> {
938 match self {
939 KclValue::TagIdentifier(t) => Ok(*t.clone()),
940 _ => Err(KclError::new_semantic(KclErrorDetails::new(
941 format!("Not a tag identifier: {self:?}"),
942 self.clone().into(),
943 ))),
944 }
945 }
946
947 pub fn get_tag_declarator(&self) -> Result<TagNode, KclError> {
949 match self {
950 KclValue::TagDeclarator(t) => Ok((**t).clone()),
951 _ => Err(KclError::new_semantic(KclErrorDetails::new(
952 format!("Not a tag declarator: {self:?}"),
953 self.clone().into(),
954 ))),
955 }
956 }
957
958 pub fn get_bool(&self) -> Result<bool, KclError> {
960 self.as_bool().ok_or_else(|| {
961 KclError::new_type(KclErrorDetails::new(
962 format!("Expected bool, found {}", self.human_friendly_type()),
963 self.into(),
964 ))
965 })
966 }
967
968 pub fn is_unknown_number(&self) -> bool {
969 match self {
970 KclValue::Number { ty, .. } => !ty.is_fully_specified(),
971 _ => false,
972 }
973 }
974
975 pub fn value_str(&self) -> Option<String> {
976 match self {
977 KclValue::Bool { value, .. } => Some(format!("{value}")),
978 KclValue::Number { value, .. } => Some(format!("{value}")),
980 KclValue::String { value, .. } => Some(format!("'{value}'")),
981 KclValue::SketchVar { value, .. } => Some(format!("var {}", value.initial_value)),
983 KclValue::Uuid { value, .. } => Some(format!("{value}")),
984 KclValue::TagDeclarator(tag) => Some(format!("${}", tag.name)),
985 KclValue::TagIdentifier(tag) => Some(format!("${}", tag.value)),
986 KclValue::Tuple { .. } => Some("[...]".to_owned()),
988 KclValue::HomArray { .. } => Some("[...]".to_owned()),
989 KclValue::Object { .. } => Some("{ ... }".to_owned()),
990 KclValue::Module { .. }
991 | KclValue::GdtAnnotation { .. }
992 | KclValue::SketchConstraint { .. }
993 | KclValue::Solid { .. }
994 | KclValue::Sketch { .. }
995 | KclValue::Helix { .. }
996 | KclValue::ImportedGeometry(_)
997 | KclValue::Function { .. }
998 | KclValue::Plane { .. }
999 | KclValue::Face { .. }
1000 | KclValue::Segment { .. }
1001 | KclValue::KclNone { .. }
1002 | KclValue::BoundedEdge { .. }
1003 | KclValue::Type { .. } => None,
1004 }
1005 }
1006}
1007
1008impl From<Geometry> for KclValue {
1009 fn from(value: Geometry) -> Self {
1010 match value {
1011 Geometry::Sketch(x) => Self::Sketch { value: Box::new(x) },
1012 Geometry::Solid(x) => Self::Solid { value: Box::new(x) },
1013 }
1014 }
1015}
1016
1017impl From<GeometryWithImportedGeometry> for KclValue {
1018 fn from(value: GeometryWithImportedGeometry) -> Self {
1019 match value {
1020 GeometryWithImportedGeometry::Sketch(x) => Self::Sketch { value: Box::new(x) },
1021 GeometryWithImportedGeometry::Solid(x) => Self::Solid { value: Box::new(x) },
1022 GeometryWithImportedGeometry::ImportedGeometry(x) => Self::ImportedGeometry(*x),
1023 }
1024 }
1025}
1026
1027impl From<Vec<GeometryWithImportedGeometry>> for KclValue {
1028 fn from(mut values: Vec<GeometryWithImportedGeometry>) -> Self {
1029 if values.len() == 1
1030 && let Some(v) = values.pop()
1031 {
1032 KclValue::from(v)
1033 } else {
1034 KclValue::HomArray {
1035 value: values.into_iter().map(KclValue::from).collect(),
1036 ty: RuntimeType::Union(vec![
1037 RuntimeType::Primitive(PrimitiveType::Sketch),
1038 RuntimeType::Primitive(PrimitiveType::Solid),
1039 RuntimeType::Primitive(PrimitiveType::ImportedGeometry),
1040 ]),
1041 }
1042 }
1043 }
1044}
1045
1046#[cfg(test)]
1047mod tests {
1048 use super::*;
1049 use crate::exec::UnitType;
1050
1051 #[test]
1052 fn test_human_friendly_type() {
1053 let len = KclValue::Number {
1054 value: 1.0,
1055 ty: NumericType::Known(UnitType::GenericLength),
1056 meta: vec![],
1057 };
1058 assert_eq!(len.human_friendly_type(), "a number (Length)".to_string());
1059
1060 let unknown = KclValue::Number {
1061 value: 1.0,
1062 ty: NumericType::Unknown,
1063 meta: vec![],
1064 };
1065 assert_eq!(unknown.human_friendly_type(), "a number with unknown units".to_string());
1066
1067 let mm = KclValue::Number {
1068 value: 1.0,
1069 ty: NumericType::Known(UnitType::Length(UnitLength::Millimeters)),
1070 meta: vec![],
1071 };
1072 assert_eq!(mm.human_friendly_type(), "a number (mm)".to_string());
1073
1074 let array1_mm = KclValue::HomArray {
1075 value: vec![mm.clone()],
1076 ty: RuntimeType::any(),
1077 };
1078 assert_eq!(
1079 array1_mm.human_friendly_type(),
1080 "an array of `number(mm)` with 1 value".to_string()
1081 );
1082
1083 let array2_mm = KclValue::HomArray {
1084 value: vec![mm.clone(), mm.clone()],
1085 ty: RuntimeType::any(),
1086 };
1087 assert_eq!(
1088 array2_mm.human_friendly_type(),
1089 "an array of `number(mm)`, `number(mm)`".to_string()
1090 );
1091
1092 let array3_mm = KclValue::HomArray {
1093 value: vec![mm.clone(), mm.clone(), mm.clone()],
1094 ty: RuntimeType::any(),
1095 };
1096 assert_eq!(
1097 array3_mm.human_friendly_type(),
1098 "an array of `number(mm)`, `number(mm)`, `number(mm)`".to_string()
1099 );
1100
1101 let inches = KclValue::Number {
1102 value: 1.0,
1103 ty: NumericType::Known(UnitType::Length(UnitLength::Inches)),
1104 meta: vec![],
1105 };
1106 let array4 = KclValue::HomArray {
1107 value: vec![mm.clone(), mm.clone(), inches, mm],
1108 ty: RuntimeType::any(),
1109 };
1110 assert_eq!(
1111 array4.human_friendly_type(),
1112 "an array of `number(mm)`, `number(mm)`, `number(in)`, ... with 4 values".to_string()
1113 );
1114
1115 let empty_array = KclValue::HomArray {
1116 value: vec![],
1117 ty: RuntimeType::any(),
1118 };
1119 assert_eq!(empty_array.human_friendly_type(), "an empty array".to_string());
1120
1121 let array_nested = KclValue::HomArray {
1122 value: vec![array2_mm],
1123 ty: RuntimeType::any(),
1124 };
1125 assert_eq!(
1126 array_nested.human_friendly_type(),
1127 "an array of `[any; 2]` with 1 value".to_string()
1128 );
1129 }
1130}