1use std::fmt;
4use std::str::FromStr;
5
6use kittycad_modeling_cmds::coord::KITTYCAD;
7use kittycad_modeling_cmds::coord::OPENGL;
8use kittycad_modeling_cmds::coord::System;
9use kittycad_modeling_cmds::coord::VULKAN;
10use serde::Deserialize;
11use serde::Serialize;
12
13use crate::KclError;
14use crate::SourceRange;
15use crate::errors::KclErrorDetails;
16use crate::errors::Severity;
17use crate::parsing::ast::types::Annotation;
18use crate::parsing::ast::types::Expr;
19use crate::parsing::ast::types::LiteralValue;
20use crate::parsing::ast::types::Node;
21use crate::parsing::ast::types::ObjectProperty;
22
23pub(super) const SIGNIFICANT_ATTRS: [&str; 3] = [SETTINGS, NO_PRELUDE, WARNINGS];
25
26pub(crate) const SETTINGS: &str = "settings";
27pub(crate) const SETTINGS_UNIT_LENGTH: &str = "defaultLengthUnit";
28pub(crate) const SETTINGS_UNIT_ANGLE: &str = "defaultAngleUnit";
29pub(crate) const SETTINGS_VERSION: &str = "kclVersion";
30pub(crate) const SETTINGS_EXPERIMENTAL_FEATURES: &str = "experimentalFeatures";
31
32pub(super) const NO_PRELUDE: &str = "no_std";
33pub(crate) const DEPRECATED: &str = "deprecated";
34pub(crate) const DEPRECATED_SINCE: &str = "deprecated_since";
35pub(crate) const DOC_CATEGORY: &str = "doc_category";
36pub(crate) const EXPERIMENTAL: &str = "experimental";
37pub(crate) const INCLUDE_IN_FEATURE_TREE: &str = "feature_tree";
38
39pub(super) const IMPORT_FORMAT: &str = "format";
40pub(super) const IMPORT_COORDS: &str = "coords";
41pub(super) const IMPORT_COORDS_VALUES: [(&str, &System); 3] =
42 [("zoo", KITTYCAD), ("opengl", OPENGL), ("vulkan", VULKAN)];
43pub(super) const IMPORT_LENGTH_UNIT: &str = "lengthUnit";
44
45pub(crate) const IMPL: &str = "impl";
46pub(crate) const IMPL_RUST: &str = "std_rust";
47pub(crate) const IMPL_CONSTRAINT: &str = "std_rust_constraint";
48pub(crate) const IMPL_CONSTRAINABLE: &str = "std_constrainable";
49pub(crate) const IMPL_RUST_CONSTRAINABLE: &str = "std_rust_constrainable";
50pub(crate) const IMPL_KCL: &str = "kcl";
51pub(crate) const IMPL_PRIMITIVE: &str = "primitive";
52pub(super) const IMPL_VALUES: [&str; 6] = [
53 IMPL_RUST,
54 IMPL_KCL,
55 IMPL_PRIMITIVE,
56 IMPL_CONSTRAINT,
57 IMPL_CONSTRAINABLE,
58 IMPL_RUST_CONSTRAINABLE,
59];
60
61pub(crate) const WARNINGS: &str = "warnings";
62pub(crate) const WARN_ALLOW: &str = "allow";
63pub(crate) const WARN_DENY: &str = "deny";
64pub(crate) const WARN_WARN: &str = "warn";
65pub(super) const WARN_LEVELS: [&str; 3] = [WARN_ALLOW, WARN_DENY, WARN_WARN];
66pub(crate) const WARN_UNKNOWN_UNITS: &str = "unknownUnits";
67pub(crate) const WARN_ANGLE_UNITS: &str = "angleUnits";
68pub(crate) const WARN_UNKNOWN_ATTR: &str = "unknownAttribute";
69pub(crate) const WARN_MOD_RETURN_VALUE: &str = "moduleReturnValue";
70pub(crate) const WARN_DEPRECATED: &str = "deprecated";
71pub(crate) const WARN_IGNORED_Z_AXIS: &str = "ignoredZAxis";
72pub(crate) const WARN_SOLVER: &str = "solver";
73pub(crate) const WARN_SHOULD_BE_PERCENTAGE: &str = "shouldBePercentage";
74pub(crate) const WARN_INVALID_MATH: &str = "invalidMath";
75pub(crate) const WARN_CSG_NO_INTERSECTION: &str = "csgNoIntersection";
76pub(crate) const WARN_UNNECESSARY_CLOSE: &str = "unnecessaryClose";
77pub(crate) const WARN_UNUSED_TAGS: &str = "unusedTags";
78pub(crate) const WARN_NOT_YET_SUPPORTED: &str = "notYetSupported";
79pub(super) const WARN_VALUES: [&str; 12] = [
80 WARN_UNKNOWN_UNITS,
81 WARN_ANGLE_UNITS,
82 WARN_UNKNOWN_ATTR,
83 WARN_MOD_RETURN_VALUE,
84 WARN_DEPRECATED,
85 WARN_IGNORED_Z_AXIS,
86 WARN_SOLVER,
87 WARN_SHOULD_BE_PERCENTAGE,
88 WARN_INVALID_MATH,
89 WARN_UNNECESSARY_CLOSE,
90 WARN_NOT_YET_SUPPORTED,
91 WARN_CSG_NO_INTERSECTION,
92];
93
94#[derive(Clone, Copy, Eq, PartialEq, Debug, Deserialize, Serialize, ts_rs::TS)]
95#[ts(export)]
96#[serde(tag = "type")]
97pub enum WarningLevel {
98 Allow,
99 Warn,
100 Deny,
101}
102
103impl WarningLevel {
104 pub(crate) fn severity(self) -> Option<Severity> {
105 match self {
106 WarningLevel::Allow => None,
107 WarningLevel::Warn => Some(Severity::Warning),
108 WarningLevel::Deny => Some(Severity::Error),
109 }
110 }
111
112 pub(crate) fn as_str(self) -> &'static str {
113 match self {
114 WarningLevel::Allow => WARN_ALLOW,
115 WarningLevel::Warn => WARN_WARN,
116 WarningLevel::Deny => WARN_DENY,
117 }
118 }
119}
120
121impl FromStr for WarningLevel {
122 type Err = ();
123
124 fn from_str(s: &str) -> Result<Self, Self::Err> {
125 match s {
126 WARN_ALLOW => Ok(Self::Allow),
127 WARN_WARN => Ok(Self::Warn),
128 WARN_DENY => Ok(Self::Deny),
129 _ => Err(()),
130 }
131 }
132}
133
134#[derive(Clone, Copy, Eq, PartialEq, Debug, Default)]
135pub enum Impl {
136 #[default]
137 Kcl,
138 KclConstrainable,
139 Rust,
140 RustConstrainable,
141 RustConstraint,
142 Primitive,
143}
144
145impl FromStr for Impl {
146 type Err = ();
147
148 fn from_str(s: &str) -> Result<Self, Self::Err> {
149 match s {
150 IMPL_RUST => Ok(Self::Rust),
151 IMPL_CONSTRAINT => Ok(Self::RustConstraint),
152 IMPL_CONSTRAINABLE => Ok(Self::KclConstrainable),
153 IMPL_RUST_CONSTRAINABLE => Ok(Self::RustConstrainable),
154 IMPL_KCL => Ok(Self::Kcl),
155 IMPL_PRIMITIVE => Ok(Self::Primitive),
156 _ => Err(()),
157 }
158 }
159}
160
161pub(crate) fn settings_completion_text() -> String {
162 format!("@{SETTINGS}({SETTINGS_UNIT_LENGTH} = mm, {SETTINGS_VERSION} = 1.0)")
163}
164
165pub(super) fn is_significant(attr: &&Node<Annotation>) -> bool {
166 match attr.name() {
167 Some(name) => SIGNIFICANT_ATTRS.contains(&name),
168 None => true,
169 }
170}
171
172pub(super) fn expect_properties<'a>(
173 for_key: &'static str,
174 annotation: &'a Node<Annotation>,
175) -> Result<&'a [Node<ObjectProperty>], KclError> {
176 assert_eq!(annotation.name().unwrap(), for_key);
177 Ok(&**annotation.properties.as_ref().ok_or_else(|| {
178 KclError::new_semantic(KclErrorDetails::new(
179 format!("Empty `{for_key}` annotation"),
180 vec![annotation.as_source_range()],
181 ))
182 })?)
183}
184
185pub(super) fn expect_ident(expr: &Expr) -> Result<&str, KclError> {
186 if let Expr::Name(name) = expr
187 && let Some(name) = name.local_ident()
188 {
189 return Ok(*name);
190 }
191
192 Err(KclError::new_semantic(KclErrorDetails::new(
193 "Unexpected settings value, expected a simple name, e.g., `mm`".to_owned(),
194 vec![expr.into()],
195 )))
196}
197
198pub(super) fn many_of(
199 expr: &Expr,
200 of: &[&'static str],
201 source_range: SourceRange,
202) -> Result<Vec<&'static str>, KclError> {
203 const UNEXPECTED_MSG: &str = "Unexpected warnings value, expected a name or array of names, e.g., `unknownUnits` or `[unknownUnits, deprecated]`";
204
205 let values = match expr {
206 Expr::Name(name) => {
207 if let Some(name) = name.local_ident() {
208 vec![*name]
209 } else {
210 return Err(KclError::new_semantic(KclErrorDetails::new(
211 UNEXPECTED_MSG.to_owned(),
212 vec![expr.into()],
213 )));
214 }
215 }
216 Expr::ArrayExpression(e) => {
217 let mut result = Vec::new();
218 for e in &e.elements {
219 if let Expr::Name(name) = e
220 && let Some(name) = name.local_ident()
221 {
222 result.push(*name);
223 continue;
224 }
225 return Err(KclError::new_semantic(KclErrorDetails::new(
226 UNEXPECTED_MSG.to_owned(),
227 vec![e.into()],
228 )));
229 }
230 result
231 }
232 _ => {
233 return Err(KclError::new_semantic(KclErrorDetails::new(
234 UNEXPECTED_MSG.to_owned(),
235 vec![expr.into()],
236 )));
237 }
238 };
239
240 values
241 .into_iter()
242 .map(|v| {
243 of.iter()
244 .find(|vv| **vv == v)
245 .ok_or_else(|| {
246 KclError::new_semantic(KclErrorDetails::new(
247 format!("Unexpected warning value: `{v}`; accepted values: {}", of.join(", "),),
248 vec![source_range],
249 ))
250 })
251 .copied()
252 })
253 .collect::<Result<Vec<&str>, KclError>>()
254}
255
256pub(super) fn expect_number(expr: &Expr) -> Result<String, KclError> {
258 if let Expr::Literal(lit) = expr
259 && let LiteralValue::Number { .. } = &lit.value
260 {
261 return Ok(lit.raw.clone());
262 }
263
264 Err(KclError::new_semantic(KclErrorDetails::new(
265 "Unexpected settings value, expected a number, e.g., `1.0`".to_owned(),
266 vec![expr.into()],
267 )))
268}
269
270#[derive(Debug, Clone, Eq, PartialEq)]
271pub struct FnAttrs {
272 pub impl_: Impl,
273 pub deprecated: bool,
274 pub deprecated_since: Option<VersionConstraint>,
277 pub experimental: bool,
278 pub include_in_feature_tree: bool,
279}
280
281impl Default for FnAttrs {
282 fn default() -> Self {
283 Self {
284 impl_: Impl::default(),
285 deprecated: false,
286 deprecated_since: None,
287 experimental: false,
288 include_in_feature_tree: true,
289 }
290 }
291}
292
293#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
304pub struct VersionConstraint(Vec<u32>);
305
306impl VersionConstraint {
307 pub fn parse(s: &str) -> Option<Self> {
310 let parts: Vec<u32> = s
311 .split('.')
312 .map(|p| p.parse::<u32>().ok())
313 .collect::<Option<Vec<_>>>()?;
314 if parts.is_empty() { None } else { Some(Self(parts)) }
315 }
316}
317
318impl fmt::Display for VersionConstraint {
319 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
320 let mut first = true;
321 for n in &self.0 {
322 if !first {
323 f.write_str(".")?;
324 }
325 write!(f, "{n}")?;
326 first = false;
327 }
328 Ok(())
329 }
330}
331
332pub(crate) fn version_ge(version: &str, constraint: &VersionConstraint) -> bool {
336 let Some(parsed): Option<Vec<u32>> = version.split('.').map(|p| p.parse::<u32>().ok()).collect() else {
337 return false;
338 };
339 parsed >= constraint.0
340}
341
342pub(super) fn get_fn_attrs(
343 annotations: &[Node<Annotation>],
344 source_range: SourceRange,
345) -> Result<Option<FnAttrs>, KclError> {
346 let mut found_attrs = false;
347 let mut fn_attrs = FnAttrs::default();
348 for attr in annotations {
349 if attr.name.is_some() || attr.properties.is_none() {
350 continue;
351 }
352 for p in attr.properties.as_ref().unwrap() {
353 if &*p.key.name == IMPL
354 && let Some(s) = p.value.ident_name()
355 {
356 found_attrs = true;
357 fn_attrs.impl_ = Impl::from_str(s).map_err(|_| {
358 KclError::new_semantic(KclErrorDetails::new(
359 format!(
360 "Invalid value for {} attribute, expected one of: {}",
361 IMPL,
362 IMPL_VALUES.join(", ")
363 ),
364 vec![source_range],
365 ))
366 })?;
367 continue;
368 }
369
370 if &*p.key.name == DEPRECATED
371 && let Some(b) = p.value.literal_bool()
372 {
373 found_attrs = true;
374 fn_attrs.deprecated = b;
375 continue;
376 }
377
378 if &*p.key.name == DEPRECATED_SINCE {
379 let Some(s) = p.value.literal_str() else {
380 return Err(KclError::new_semantic(KclErrorDetails::new(
381 format!("Expected a version string for {DEPRECATED_SINCE}, e.g., \"2.0\""),
382 vec![source_range],
383 )));
384 };
385 let Some(constraint) = VersionConstraint::parse(s) else {
386 return Err(KclError::new_semantic(KclErrorDetails::new(
387 format!(
388 "Invalid version string for {DEPRECATED_SINCE}: `{s}`; expected a dotted integer version, e.g., \"2.0\""
389 ),
390 vec![source_range],
391 )));
392 };
393 found_attrs = true;
394 fn_attrs.deprecated_since = Some(constraint);
395 continue;
396 }
397
398 if &*p.key.name == DOC_CATEGORY {
400 continue;
401 }
402
403 if &*p.key.name == EXPERIMENTAL
404 && let Some(b) = p.value.literal_bool()
405 {
406 found_attrs = true;
407 fn_attrs.experimental = b;
408 continue;
409 }
410
411 if &*p.key.name == INCLUDE_IN_FEATURE_TREE
412 && let Some(b) = p.value.literal_bool()
413 {
414 found_attrs = true;
415 fn_attrs.include_in_feature_tree = b;
416 continue;
417 }
418
419 return Err(KclError::new_semantic(KclErrorDetails::new(
420 format!(
421 "Invalid attribute, expected one of: {IMPL}, {DEPRECATED}, {DEPRECATED_SINCE}, {DOC_CATEGORY}, {EXPERIMENTAL}, {INCLUDE_IN_FEATURE_TREE}, found `{}`",
422 &*p.key.name,
423 ),
424 vec![source_range],
425 )));
426 }
427 }
428
429 Ok(if found_attrs { Some(fn_attrs) } else { None })
430}
431
432#[cfg(test)]
433mod tests {
434 use super::*;
435
436 fn vc(s: &str) -> VersionConstraint {
437 VersionConstraint::parse(s).unwrap()
438 }
439
440 #[test]
441 fn version_constraint_parse_handles_typical_inputs() {
442 assert_eq!(VersionConstraint::parse("1.0"), Some(VersionConstraint(vec![1, 0])));
443 assert_eq!(VersionConstraint::parse("2"), Some(VersionConstraint(vec![2])));
444 assert_eq!(
445 VersionConstraint::parse("2.1.3"),
446 Some(VersionConstraint(vec![2, 1, 3]))
447 );
448 assert_eq!(VersionConstraint::parse(""), None);
449 assert_eq!(VersionConstraint::parse("1.x"), None);
450 assert_eq!(VersionConstraint::parse("1.-1"), None);
451 }
452
453 #[test]
454 fn version_constraint_display_round_trips() {
455 assert_eq!(vc("1.0").to_string(), "1.0");
456 assert_eq!(vc("2.1.3").to_string(), "2.1.3");
457 assert_eq!(vc("2").to_string(), "2");
458 }
459
460 #[test]
461 fn version_ge_compares_components_numerically() {
462 assert!(version_ge("1.0", &vc("1.0")));
463 assert!(version_ge("2.0", &vc("1.0")));
464 assert!(version_ge("2.0", &vc("2.0")));
465 assert!(version_ge("10.0", &vc("2.0")));
466 assert!(version_ge("2.1", &vc("2.0")));
467 assert!(!version_ge("1.0", &vc("2.0")));
468 assert!(!version_ge("2.0", &vc("2.1")));
469 assert!(!version_ge("1.99", &vc("2.0")));
470 assert!(!version_ge("bogus", &vc("1.0")));
472 }
473}