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