1#[cfg(feature = "artifact-graph")]
2use std::collections::BTreeMap;
3use std::{str::FromStr, sync::Arc};
4
5use anyhow::Result;
6use indexmap::IndexMap;
7use kittycad_modeling_cmds::units::{UnitAngle, UnitLength};
8use serde::{Deserialize, Serialize};
9use uuid::Uuid;
10
11use crate::{
12 CompilationError, EngineManager, ExecutorContext, KclErrorWithOutputs, MockConfig, SourceRange,
13 collections::AhashIndexSet,
14 errors::{KclError, KclErrorDetails, Severity},
15 exec::DefaultPlanes,
16 execution::{
17 EnvironmentRef, ExecOutcome, ExecutorSettings, KclValue, SketchVarId, UnsolvedSegment, annotations,
18 cad_op::Operation,
19 id_generator::IdGenerator,
20 memory::{ProgramMemory, Stack},
21 types::NumericType,
22 },
23 front::ObjectId,
24 modules::{ModuleId, ModuleInfo, ModuleLoader, ModulePath, ModuleRepr, ModuleSource},
25 parsing::ast::types::{Annotation, NodeRef},
26};
27#[cfg(feature = "artifact-graph")]
28use crate::{
29 execution::{Artifact, ArtifactCommand, ArtifactGraph, ArtifactId, ProgramLookup, sketch_solve::Solved},
30 front::{Number, Object},
31 id::IncIdGenerator,
32};
33
34#[derive(Debug, Clone)]
36pub struct ExecState {
37 pub(super) global: GlobalState,
38 pub(super) mod_local: ModuleState,
39}
40
41pub type ModuleInfoMap = IndexMap<ModuleId, ModuleInfo>;
42
43#[derive(Debug, Clone)]
44pub(super) struct GlobalState {
45 pub path_to_source_id: IndexMap<ModulePath, ModuleId>,
47 pub id_to_source: IndexMap<ModuleId, ModuleSource>,
49 pub module_infos: ModuleInfoMap,
51 pub mod_loader: ModuleLoader,
53 pub errors: Vec<CompilationError>,
55 pub artifacts: ArtifactState,
57 pub root_module_artifacts: ModuleArtifactState,
59 #[cfg(feature = "artifact-graph")]
61 pub segment_ids_edited: AhashIndexSet<ObjectId>,
62}
63
64#[cfg(feature = "artifact-graph")]
65#[derive(Debug, Clone, Default)]
66pub(super) struct ArtifactState {
67 pub artifacts: IndexMap<ArtifactId, Artifact>,
70 pub graph: ArtifactGraph,
72}
73
74#[cfg(not(feature = "artifact-graph"))]
75#[derive(Debug, Clone, Default)]
76pub(super) struct ArtifactState {}
77
78#[cfg(feature = "artifact-graph")]
80#[derive(Debug, Clone, Default, PartialEq, Serialize)]
81pub struct ModuleArtifactState {
82 pub artifacts: IndexMap<ArtifactId, Artifact>,
84 #[serde(skip)]
87 pub unprocessed_commands: Vec<ArtifactCommand>,
88 pub commands: Vec<ArtifactCommand>,
90 pub operations: Vec<Operation>,
93 pub object_id_generator: IncIdGenerator<usize>,
95 pub scene_objects: Vec<Object>,
97 pub source_range_to_object: BTreeMap<SourceRange, ObjectId>,
100 pub var_solutions: Vec<(SourceRange, Number)>,
102}
103
104#[cfg(not(feature = "artifact-graph"))]
105#[derive(Debug, Clone, Default, PartialEq, Serialize)]
106pub struct ModuleArtifactState {}
107
108#[derive(Debug, Clone)]
109pub(super) struct ModuleState {
110 pub id_generator: IdGenerator,
112 pub stack: Stack,
113 pub(super) call_stack_size: usize,
117 pub pipe_value: Option<KclValue>,
120 pub being_declared: Option<String>,
124 pub sketch_block: Option<SketchBlockState>,
126 pub inside_stdlib: bool,
129 pub stdlib_entry_source_range: Option<SourceRange>,
131 pub module_exports: Vec<String>,
133 pub settings: MetaSettings,
135 pub freedom_analysis: bool,
139 pub(super) explicit_length_units: bool,
140 pub(super) path: ModulePath,
141 pub artifacts: ModuleArtifactState,
143
144 pub(super) allowed_warnings: Vec<&'static str>,
145 pub(super) denied_warnings: Vec<&'static str>,
146}
147
148#[derive(Debug, Clone, Default)]
149pub(crate) struct SketchBlockState {
150 pub sketch_vars: Vec<KclValue>,
151 #[cfg(feature = "artifact-graph")]
152 pub sketch_constraints: Vec<ObjectId>,
153 pub solver_constraints: Vec<kcl_ezpz::Constraint>,
154 pub solver_optional_constraints: Vec<kcl_ezpz::Constraint>,
155 pub needed_by_engine: Vec<UnsolvedSegment>,
156}
157
158impl ExecState {
159 pub fn new(exec_context: &super::ExecutorContext) -> Self {
160 ExecState {
161 global: GlobalState::new(&exec_context.settings, Default::default()),
162 mod_local: ModuleState::new(ModulePath::Main, ProgramMemory::new(), Default::default(), 0, true),
163 }
164 }
165
166 pub fn new_sketch_mode(exec_context: &super::ExecutorContext, mock_config: &MockConfig) -> Self {
167 #[cfg(feature = "artifact-graph")]
168 let segment_ids_edited = mock_config.segment_ids_edited.clone();
169 #[cfg(not(feature = "artifact-graph"))]
170 let segment_ids_edited = Default::default();
171 ExecState {
172 global: GlobalState::new(&exec_context.settings, segment_ids_edited),
173 mod_local: ModuleState::new(
174 ModulePath::Main,
175 ProgramMemory::new(),
176 Default::default(),
177 0,
178 mock_config.freedom_analysis,
179 ),
180 }
181 }
182
183 pub(super) fn reset(&mut self, exec_context: &super::ExecutorContext) {
184 let global = GlobalState::new(&exec_context.settings, Default::default());
185
186 *self = ExecState {
187 global,
188 mod_local: ModuleState::new(
189 self.mod_local.path.clone(),
190 ProgramMemory::new(),
191 Default::default(),
192 0,
193 true,
194 ),
195 };
196 }
197
198 pub fn err(&mut self, e: CompilationError) {
200 self.global.errors.push(e);
201 }
202
203 pub fn warn(&mut self, mut e: CompilationError, name: &'static str) {
205 debug_assert!(annotations::WARN_VALUES.contains(&name));
206
207 if self.mod_local.allowed_warnings.contains(&name) {
208 return;
209 }
210
211 if self.mod_local.denied_warnings.contains(&name) {
212 e.severity = Severity::Error;
213 } else {
214 e.severity = Severity::Warning;
215 }
216
217 self.global.errors.push(e);
218 }
219
220 pub fn warn_experimental(&mut self, feature_name: &str, source_range: SourceRange) {
221 let Some(severity) = self.mod_local.settings.experimental_features.severity() else {
222 return;
223 };
224 let error = CompilationError {
225 source_range,
226 message: format!("Use of {feature_name} is experimental and may change or be removed."),
227 suggestion: None,
228 severity,
229 tag: crate::errors::Tag::None,
230 };
231
232 self.global.errors.push(error);
233 }
234
235 pub fn clear_units_warnings(&mut self, source_range: &SourceRange) {
236 self.global.errors = std::mem::take(&mut self.global.errors)
237 .into_iter()
238 .filter(|e| {
239 e.severity != Severity::Warning
240 || !source_range.contains_range(&e.source_range)
241 || e.tag != crate::errors::Tag::UnknownNumericUnits
242 })
243 .collect();
244 }
245
246 pub fn errors(&self) -> &[CompilationError] {
247 &self.global.errors
248 }
249
250 pub async fn into_exec_outcome(self, main_ref: EnvironmentRef, ctx: &ExecutorContext) -> ExecOutcome {
254 ExecOutcome {
257 variables: self.mod_local.variables(main_ref),
258 filenames: self.global.filenames(),
259 #[cfg(feature = "artifact-graph")]
260 operations: self.global.root_module_artifacts.operations,
261 #[cfg(feature = "artifact-graph")]
262 artifact_graph: self.global.artifacts.graph,
263 #[cfg(feature = "artifact-graph")]
264 scene_objects: self.global.root_module_artifacts.scene_objects,
265 #[cfg(feature = "artifact-graph")]
266 source_range_to_object: self.global.root_module_artifacts.source_range_to_object,
267 #[cfg(feature = "artifact-graph")]
268 var_solutions: self.global.root_module_artifacts.var_solutions,
269 errors: self.global.errors,
270 default_planes: ctx.engine.get_default_planes().read().await.clone(),
271 }
272 }
273
274 pub(crate) fn stack(&self) -> &Stack {
275 &self.mod_local.stack
276 }
277
278 pub(crate) fn mut_stack(&mut self) -> &mut Stack {
279 &mut self.mod_local.stack
280 }
281
282 pub(super) fn inc_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
285 if self.mod_local.call_stack_size >= 50 {
288 return Err(KclError::MaxCallStack {
289 details: KclErrorDetails::new("maximum call stack size exceeded".to_owned(), vec![range]),
290 });
291 }
292 self.mod_local.call_stack_size += 1;
293 Ok(())
294 }
295
296 pub(super) fn dec_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
299 if self.mod_local.call_stack_size == 0 {
301 let message = "call stack size below zero".to_owned();
302 debug_assert!(false, "{message}");
303 return Err(KclError::new_internal(KclErrorDetails::new(message, vec![range])));
304 }
305 self.mod_local.call_stack_size -= 1;
306 Ok(())
307 }
308
309 #[cfg(not(feature = "artifact-graph"))]
310 pub fn next_object_id(&mut self) -> ObjectId {
311 ObjectId(0)
314 }
315
316 #[cfg(feature = "artifact-graph")]
317 pub fn next_object_id(&mut self) -> ObjectId {
318 ObjectId(self.mod_local.artifacts.object_id_generator.next_id())
319 }
320
321 #[cfg(feature = "artifact-graph")]
322 pub fn add_scene_object(&mut self, obj: Object, source_range: SourceRange) -> ObjectId {
323 let id = obj.id;
324 debug_assert!(id.0 == self.mod_local.artifacts.scene_objects.len());
325 self.mod_local.artifacts.scene_objects.push(obj);
326 self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
327 id
328 }
329
330 #[cfg(feature = "artifact-graph")]
333 pub fn add_placeholder_scene_object(&mut self, id: ObjectId, source_range: SourceRange) -> ObjectId {
334 debug_assert!(id.0 == self.mod_local.artifacts.scene_objects.len());
335 self.mod_local
336 .artifacts
337 .scene_objects
338 .push(Object::placeholder(id, source_range));
339 self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
340 id
341 }
342
343 #[cfg(feature = "artifact-graph")]
345 pub fn set_scene_object(&mut self, object: Object) {
346 let id = object.id;
347 self.mod_local.artifacts.scene_objects[id.0] = object;
348 }
349
350 #[cfg(feature = "artifact-graph")]
351 pub fn segment_ids_edited_contains(&self, object_id: &ObjectId) -> bool {
352 self.global.segment_ids_edited.contains(object_id)
353 }
354
355 pub(super) fn is_in_sketch_block(&self) -> bool {
356 self.mod_local.sketch_block.is_some()
357 }
358
359 pub(crate) fn sketch_block_mut(&mut self) -> Option<&mut SketchBlockState> {
360 self.mod_local.sketch_block.as_mut()
361 }
362
363 pub fn next_uuid(&mut self) -> Uuid {
364 self.mod_local.id_generator.next_uuid()
365 }
366
367 #[cfg(feature = "artifact-graph")]
368 pub fn next_artifact_id(&mut self) -> ArtifactId {
369 self.mod_local.id_generator.next_artifact_id()
370 }
371
372 pub fn id_generator(&mut self) -> &mut IdGenerator {
373 &mut self.mod_local.id_generator
374 }
375
376 #[cfg(feature = "artifact-graph")]
377 pub(crate) fn add_artifact(&mut self, artifact: Artifact) {
378 let id = artifact.id();
379 self.mod_local.artifacts.artifacts.insert(id, artifact);
380 }
381
382 pub(crate) fn push_op(&mut self, op: Operation) {
383 #[cfg(feature = "artifact-graph")]
384 self.mod_local.artifacts.operations.push(op);
385 #[cfg(not(feature = "artifact-graph"))]
386 drop(op);
387 }
388
389 #[cfg(feature = "artifact-graph")]
390 pub(crate) fn push_command(&mut self, command: ArtifactCommand) {
391 self.mod_local.artifacts.unprocessed_commands.push(command);
392 #[cfg(not(feature = "artifact-graph"))]
393 drop(command);
394 }
395
396 pub(super) fn next_module_id(&self) -> ModuleId {
397 ModuleId::from_usize(self.global.path_to_source_id.len())
398 }
399
400 pub(super) fn id_for_module(&self, path: &ModulePath) -> Option<ModuleId> {
401 self.global.path_to_source_id.get(path).cloned()
402 }
403
404 pub(super) fn add_path_to_source_id(&mut self, path: ModulePath, id: ModuleId) {
405 debug_assert!(!self.global.path_to_source_id.contains_key(&path));
406 self.global.path_to_source_id.insert(path, id);
407 }
408
409 pub(crate) fn add_root_module_contents(&mut self, program: &crate::Program) {
410 let root_id = ModuleId::default();
411 let path = self
413 .global
414 .path_to_source_id
415 .iter()
416 .find(|(_, v)| **v == root_id)
417 .unwrap()
418 .0
419 .clone();
420 self.add_id_to_source(
421 root_id,
422 ModuleSource {
423 path,
424 source: program.original_file_contents.to_string(),
425 },
426 );
427 }
428
429 pub(super) fn add_id_to_source(&mut self, id: ModuleId, source: ModuleSource) {
430 self.global.id_to_source.insert(id, source);
431 }
432
433 pub(super) fn add_module(&mut self, id: ModuleId, path: ModulePath, repr: ModuleRepr) {
434 debug_assert!(self.global.path_to_source_id.contains_key(&path));
435 let module_info = ModuleInfo { id, repr, path };
436 self.global.module_infos.insert(id, module_info);
437 }
438
439 pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
440 self.global.module_infos.get(&id)
441 }
442
443 #[cfg(all(test, feature = "artifact-graph"))]
444 pub(crate) fn modules(&self) -> &ModuleInfoMap {
445 &self.global.module_infos
446 }
447
448 #[cfg(all(test, feature = "artifact-graph"))]
449 pub(crate) fn root_module_artifact_state(&self) -> &ModuleArtifactState {
450 &self.global.root_module_artifacts
451 }
452
453 pub fn current_default_units(&self) -> NumericType {
454 NumericType::Default {
455 len: self.length_unit(),
456 angle: self.angle_unit(),
457 }
458 }
459
460 pub fn length_unit(&self) -> UnitLength {
461 self.mod_local.settings.default_length_units
462 }
463
464 pub fn angle_unit(&self) -> UnitAngle {
465 self.mod_local.settings.default_angle_units
466 }
467
468 pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
469 KclError::new_import_cycle(KclErrorDetails::new(
470 format!(
471 "circular import of modules is not allowed: {} -> {}",
472 self.global
473 .mod_loader
474 .import_stack
475 .iter()
476 .map(|p| p.to_string_lossy())
477 .collect::<Vec<_>>()
478 .join(" -> "),
479 path,
480 ),
481 vec![source_range],
482 ))
483 }
484
485 pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
486 self.mod_local.pipe_value.as_ref()
487 }
488
489 pub(crate) fn error_with_outputs(
490 &self,
491 error: KclError,
492 main_ref: Option<EnvironmentRef>,
493 default_planes: Option<DefaultPlanes>,
494 ) -> KclErrorWithOutputs {
495 let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = self
496 .global
497 .path_to_source_id
498 .iter()
499 .map(|(k, v)| ((*v), k.clone()))
500 .collect();
501
502 KclErrorWithOutputs::new(
503 error,
504 self.errors().to_vec(),
505 main_ref
506 .map(|main_ref| self.mod_local.variables(main_ref))
507 .unwrap_or_default(),
508 #[cfg(feature = "artifact-graph")]
509 self.global.root_module_artifacts.operations.clone(),
510 #[cfg(feature = "artifact-graph")]
511 Default::default(),
512 #[cfg(feature = "artifact-graph")]
513 self.global.artifacts.graph.clone(),
514 module_id_to_module_path,
515 self.global.id_to_source.clone(),
516 default_planes,
517 )
518 }
519
520 #[cfg(feature = "artifact-graph")]
521 pub(crate) fn build_program_lookup(
522 &self,
523 current: crate::parsing::ast::types::Node<crate::parsing::ast::types::Program>,
524 ) -> ProgramLookup {
525 ProgramLookup::new(current, self.global.module_infos.clone())
526 }
527
528 #[cfg(feature = "artifact-graph")]
529 pub(crate) async fn build_artifact_graph(
530 &mut self,
531 engine: &Arc<Box<dyn EngineManager>>,
532 program: NodeRef<'_, crate::parsing::ast::types::Program>,
533 ) -> Result<(), KclError> {
534 let mut new_commands = Vec::new();
535 let mut new_exec_artifacts = IndexMap::new();
536 for module in self.global.module_infos.values_mut() {
537 match &mut module.repr {
538 ModuleRepr::Kcl(_, Some(outcome)) => {
539 new_commands.extend(outcome.artifacts.process_commands());
540 new_exec_artifacts.extend(outcome.artifacts.artifacts.clone());
541 }
542 ModuleRepr::Foreign(_, Some((_, module_artifacts))) => {
543 new_commands.extend(module_artifacts.process_commands());
544 new_exec_artifacts.extend(module_artifacts.artifacts.clone());
545 }
546 ModuleRepr::Root | ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
547 }
548 }
549 new_commands.extend(self.global.root_module_artifacts.process_commands());
552 new_exec_artifacts.extend(self.global.root_module_artifacts.artifacts.clone());
555 let new_responses = engine.take_responses().await;
556
557 self.global.artifacts.artifacts.extend(new_exec_artifacts);
560
561 let initial_graph = self.global.artifacts.graph.clone();
562
563 let programs = self.build_program_lookup(program.clone());
565 let graph_result = crate::execution::artifact::build_artifact_graph(
566 &new_commands,
567 &new_responses,
568 program,
569 &mut self.global.artifacts.artifacts,
570 initial_graph,
571 &programs,
572 );
573
574 let artifact_graph = graph_result?;
575 self.global.artifacts.graph = artifact_graph;
576
577 Ok(())
578 }
579
580 #[cfg(not(feature = "artifact-graph"))]
581 pub(crate) async fn build_artifact_graph(
582 &mut self,
583 _engine: &Arc<Box<dyn EngineManager>>,
584 _program: NodeRef<'_, crate::parsing::ast::types::Program>,
585 ) -> Result<(), KclError> {
586 Ok(())
587 }
588}
589
590impl GlobalState {
591 fn new(settings: &ExecutorSettings, segment_ids_edited: AhashIndexSet<ObjectId>) -> Self {
592 #[cfg(not(feature = "artifact-graph"))]
593 drop(segment_ids_edited);
594 let mut global = GlobalState {
595 path_to_source_id: Default::default(),
596 module_infos: Default::default(),
597 artifacts: Default::default(),
598 root_module_artifacts: Default::default(),
599 mod_loader: Default::default(),
600 errors: Default::default(),
601 id_to_source: Default::default(),
602 #[cfg(feature = "artifact-graph")]
603 segment_ids_edited,
604 };
605
606 let root_id = ModuleId::default();
607 let root_path = settings.current_file.clone().unwrap_or_default();
608 global.module_infos.insert(
609 root_id,
610 ModuleInfo {
611 id: root_id,
612 path: ModulePath::Local {
613 value: root_path.clone(),
614 original_import_path: None,
615 },
616 repr: ModuleRepr::Root,
617 },
618 );
619 global.path_to_source_id.insert(
620 ModulePath::Local {
621 value: root_path,
622 original_import_path: None,
623 },
624 root_id,
625 );
626 global
627 }
628
629 pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
630 self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
631 }
632
633 pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
634 self.id_to_source.get(&id)
635 }
636}
637
638impl ArtifactState {
639 #[cfg(feature = "artifact-graph")]
640 pub fn cached_body_items(&self) -> usize {
641 self.graph.item_count
642 }
643
644 pub(crate) fn clear(&mut self) {
645 #[cfg(feature = "artifact-graph")]
646 {
647 self.artifacts.clear();
648 self.graph.clear();
649 }
650 }
651}
652
653impl ModuleArtifactState {
654 pub(crate) fn clear(&mut self) {
655 #[cfg(feature = "artifact-graph")]
656 {
657 self.artifacts.clear();
658 self.unprocessed_commands.clear();
659 self.commands.clear();
660 self.operations.clear();
661 }
662 }
663
664 #[cfg(not(feature = "artifact-graph"))]
665 pub(crate) fn extend(&mut self, _other: ModuleArtifactState) {}
666
667 #[cfg(feature = "artifact-graph")]
669 pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
670 self.artifacts.extend(other.artifacts);
671 self.unprocessed_commands.extend(other.unprocessed_commands);
672 self.commands.extend(other.commands);
673 self.operations.extend(other.operations);
674 self.scene_objects.extend(other.scene_objects);
675 self.source_range_to_object.extend(other.source_range_to_object);
676 self.var_solutions.extend(other.var_solutions);
677 }
678
679 #[cfg(feature = "artifact-graph")]
683 pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
684 let unprocessed = std::mem::take(&mut self.unprocessed_commands);
685 let new_module_commands = unprocessed.clone();
686 self.commands.extend(unprocessed);
687 new_module_commands
688 }
689}
690
691impl ModuleState {
692 pub(super) fn new(
693 path: ModulePath,
694 memory: Arc<ProgramMemory>,
695 module_id: Option<ModuleId>,
696 next_object_id: usize,
697 freedom_analysis: bool,
698 ) -> Self {
699 #[cfg(not(feature = "artifact-graph"))]
700 let _ = next_object_id;
701 ModuleState {
702 id_generator: IdGenerator::new(module_id),
703 stack: memory.new_stack(),
704 call_stack_size: 0,
705 pipe_value: Default::default(),
706 being_declared: Default::default(),
707 sketch_block: Default::default(),
708 stdlib_entry_source_range: Default::default(),
709 module_exports: Default::default(),
710 explicit_length_units: false,
711 path,
712 settings: Default::default(),
713 freedom_analysis,
714 #[cfg(not(feature = "artifact-graph"))]
715 artifacts: Default::default(),
716 #[cfg(feature = "artifact-graph")]
717 artifacts: ModuleArtifactState {
718 object_id_generator: IncIdGenerator::new(next_object_id),
719 ..Default::default()
720 },
721 allowed_warnings: Vec::new(),
722 denied_warnings: Vec::new(),
723 inside_stdlib: false,
724 }
725 }
726
727 pub(super) fn variables(&self, main_ref: EnvironmentRef) -> IndexMap<String, KclValue> {
728 self.stack
729 .find_all_in_env(main_ref)
730 .map(|(k, v)| (k.clone(), v.clone()))
731 .collect()
732 }
733}
734
735impl SketchBlockState {
736 pub(crate) fn next_sketch_var_id(&self) -> SketchVarId {
737 SketchVarId(self.sketch_vars.len())
738 }
739
740 #[cfg(feature = "artifact-graph")]
743 pub(crate) fn var_solutions(
744 &self,
745 solve_outcome: Solved,
746 solution_ty: NumericType,
747 range: SourceRange,
748 ) -> Result<Vec<(SourceRange, Number)>, KclError> {
749 self.sketch_vars
750 .iter()
751 .map(|v| {
752 let Some(sketch_var) = v.as_sketch_var() else {
753 return Err(KclError::new_internal(KclErrorDetails::new(
754 "Expected sketch variable".to_owned(),
755 vec![range],
756 )));
757 };
758 let var_index = sketch_var.id.0;
759 let solved_n = solve_outcome.final_values.get(var_index).ok_or_else(|| {
760 let message = format!("No solution for sketch variable with id {}", var_index);
761 debug_assert!(false, "{}", &message);
762 KclError::new_internal(KclErrorDetails::new(
763 message,
764 sketch_var.meta.iter().map(|m| m.source_range).collect(),
765 ))
766 })?;
767 let solved_value = Number {
768 value: *solved_n,
769 units: solution_ty.try_into().map_err(|_| {
770 KclError::new_internal(KclErrorDetails::new(
771 "Failed to convert numeric type to units".to_owned(),
772 vec![range],
773 ))
774 })?,
775 };
776 let Some(source_range) = sketch_var.meta.first().map(|m| m.source_range) else {
777 return Ok(None);
778 };
779 Ok(Some((source_range, solved_value)))
780 })
781 .filter_map(Result::transpose)
782 .collect::<Result<Vec<_>, KclError>>()
783 }
784}
785
786#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
787#[ts(export)]
788#[serde(rename_all = "camelCase")]
789pub struct MetaSettings {
790 pub default_length_units: UnitLength,
791 pub default_angle_units: UnitAngle,
792 pub experimental_features: annotations::WarningLevel,
793 pub kcl_version: String,
794}
795
796impl Default for MetaSettings {
797 fn default() -> Self {
798 MetaSettings {
799 default_length_units: UnitLength::Millimeters,
800 default_angle_units: UnitAngle::Degrees,
801 experimental_features: annotations::WarningLevel::Deny,
802 kcl_version: "1.0".to_owned(),
803 }
804 }
805}
806
807impl MetaSettings {
808 pub(crate) fn update_from_annotation(
809 &mut self,
810 annotation: &crate::parsing::ast::types::Node<Annotation>,
811 ) -> Result<(bool, bool), KclError> {
812 let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
813
814 let mut updated_len = false;
815 let mut updated_angle = false;
816 for p in properties {
817 match &*p.inner.key.name {
818 annotations::SETTINGS_UNIT_LENGTH => {
819 let value = annotations::expect_ident(&p.inner.value)?;
820 let value = super::types::length_from_str(value, annotation.as_source_range())?;
821 self.default_length_units = value;
822 updated_len = true;
823 }
824 annotations::SETTINGS_UNIT_ANGLE => {
825 let value = annotations::expect_ident(&p.inner.value)?;
826 let value = super::types::angle_from_str(value, annotation.as_source_range())?;
827 self.default_angle_units = value;
828 updated_angle = true;
829 }
830 annotations::SETTINGS_VERSION => {
831 let value = annotations::expect_number(&p.inner.value)?;
832 self.kcl_version = value;
833 }
834 annotations::SETTINGS_EXPERIMENTAL_FEATURES => {
835 let value = annotations::expect_ident(&p.inner.value)?;
836 let value = annotations::WarningLevel::from_str(value).map_err(|_| {
837 KclError::new_semantic(KclErrorDetails::new(
838 format!(
839 "Invalid value for {} settings property, expected one of: {}",
840 annotations::SETTINGS_EXPERIMENTAL_FEATURES,
841 annotations::WARN_LEVELS.join(", ")
842 ),
843 annotation.as_source_ranges(),
844 ))
845 })?;
846 self.experimental_features = value;
847 }
848 name => {
849 return Err(KclError::new_semantic(KclErrorDetails::new(
850 format!(
851 "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
852 annotations::SETTINGS_UNIT_LENGTH,
853 annotations::SETTINGS_UNIT_ANGLE
854 ),
855 vec![annotation.as_source_range()],
856 )));
857 }
858 }
859 }
860
861 Ok((updated_len, updated_angle))
862 }
863}