2023-08-24 15:34:51 -07:00
|
|
|
//! Functions implemented for language execution.
|
|
|
|
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod extrude;
|
2024-03-05 11:52:45 -08:00
|
|
|
pub mod fillet;
|
2024-02-12 12:18:37 -08:00
|
|
|
pub mod import;
|
2023-11-09 09:58:20 -06:00
|
|
|
pub mod kcl_stdlib;
|
2023-09-13 15:09:07 -07:00
|
|
|
pub mod math;
|
2024-02-11 15:08:54 -08:00
|
|
|
pub mod patterns;
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod segment;
|
2023-11-09 09:58:20 -06:00
|
|
|
pub mod shapes;
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod sketch;
|
|
|
|
pub mod utils;
|
2023-08-24 15:34:51 -07:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2023-08-25 13:41:04 -07:00
|
|
|
use anyhow::Result;
|
|
|
|
use derive_docs::stdlib;
|
2023-09-20 18:27:08 -07:00
|
|
|
use kittycad::types::OkWebSocketResponseData;
|
2023-11-08 20:23:59 -06:00
|
|
|
use lazy_static::lazy_static;
|
2023-08-25 13:41:04 -07:00
|
|
|
use parse_display::{Display, FromStr};
|
|
|
|
use schemars::JsonSchema;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2023-08-24 15:34:51 -07:00
|
|
|
use crate::{
|
2024-02-13 10:26:09 -08:00
|
|
|
ast::types::parse_json_number_as_f64,
|
2023-11-08 20:23:59 -06:00
|
|
|
docs::StdLibFn,
|
2023-08-24 15:34:51 -07:00
|
|
|
errors::{KclError, KclErrorDetails},
|
2024-02-11 15:08:54 -08:00
|
|
|
executor::{
|
2024-03-12 12:54:45 -07:00
|
|
|
ExecutorContext, ExtrudeGroup, MemoryItem, Metadata, SketchGroup, SketchGroupSet, SketchSurface, SourceRange,
|
2024-02-15 13:56:31 -08:00
|
|
|
},
|
2024-02-16 16:42:01 -08:00
|
|
|
std::{kcl_stdlib::KclStdLibFn, sketch::SketchOnFaceTag},
|
2023-08-24 15:34:51 -07:00
|
|
|
};
|
|
|
|
|
2023-09-20 18:27:08 -07:00
|
|
|
pub type StdFn = fn(Args) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<MemoryItem, KclError>>>>;
|
2023-09-05 16:02:27 -07:00
|
|
|
pub type FnMap = HashMap<String, StdFn>;
|
2023-08-24 15:34:51 -07:00
|
|
|
|
2023-11-08 20:23:59 -06:00
|
|
|
lazy_static! {
|
|
|
|
static ref CORE_FNS: Vec<Box<dyn StdLibFn>> = vec![
|
|
|
|
Box::new(LegLen),
|
|
|
|
Box::new(LegAngX),
|
|
|
|
Box::new(LegAngY),
|
|
|
|
Box::new(crate::std::extrude::Extrude),
|
|
|
|
Box::new(crate::std::extrude::GetExtrudeWallTransform),
|
|
|
|
Box::new(crate::std::segment::SegEndX),
|
|
|
|
Box::new(crate::std::segment::SegEndY),
|
|
|
|
Box::new(crate::std::segment::LastSegX),
|
|
|
|
Box::new(crate::std::segment::LastSegY),
|
|
|
|
Box::new(crate::std::segment::SegLen),
|
|
|
|
Box::new(crate::std::segment::SegAng),
|
|
|
|
Box::new(crate::std::segment::AngleToMatchLengthX),
|
|
|
|
Box::new(crate::std::segment::AngleToMatchLengthY),
|
|
|
|
Box::new(crate::std::sketch::LineTo),
|
|
|
|
Box::new(crate::std::sketch::Line),
|
|
|
|
Box::new(crate::std::sketch::XLineTo),
|
|
|
|
Box::new(crate::std::sketch::XLine),
|
|
|
|
Box::new(crate::std::sketch::YLineTo),
|
|
|
|
Box::new(crate::std::sketch::YLine),
|
|
|
|
Box::new(crate::std::sketch::AngledLineToX),
|
|
|
|
Box::new(crate::std::sketch::AngledLineToY),
|
|
|
|
Box::new(crate::std::sketch::AngledLine),
|
|
|
|
Box::new(crate::std::sketch::AngledLineOfXLength),
|
|
|
|
Box::new(crate::std::sketch::AngledLineOfYLength),
|
|
|
|
Box::new(crate::std::sketch::AngledLineThatIntersects),
|
|
|
|
Box::new(crate::std::sketch::StartSketchAt),
|
|
|
|
Box::new(crate::std::sketch::StartSketchOn),
|
|
|
|
Box::new(crate::std::sketch::StartProfileAt),
|
|
|
|
Box::new(crate::std::sketch::Close),
|
|
|
|
Box::new(crate::std::sketch::Arc),
|
|
|
|
Box::new(crate::std::sketch::TangentialArc),
|
|
|
|
Box::new(crate::std::sketch::TangentialArcTo),
|
|
|
|
Box::new(crate::std::sketch::BezierCurve),
|
|
|
|
Box::new(crate::std::sketch::Hole),
|
2024-03-12 12:54:45 -07:00
|
|
|
Box::new(crate::std::patterns::PatternLinear2D),
|
|
|
|
Box::new(crate::std::patterns::PatternLinear3D),
|
|
|
|
Box::new(crate::std::patterns::PatternCircular2D),
|
|
|
|
Box::new(crate::std::patterns::PatternCircular3D),
|
2024-03-05 11:52:45 -08:00
|
|
|
Box::new(crate::std::fillet::Fillet),
|
|
|
|
Box::new(crate::std::fillet::GetOppositeEdge),
|
|
|
|
Box::new(crate::std::fillet::GetNextAdjacentEdge),
|
|
|
|
Box::new(crate::std::fillet::GetPreviousAdjacentEdge),
|
2024-02-12 12:18:37 -08:00
|
|
|
Box::new(crate::std::import::Import),
|
2023-11-08 20:23:59 -06:00
|
|
|
Box::new(crate::std::math::Cos),
|
|
|
|
Box::new(crate::std::math::Sin),
|
|
|
|
Box::new(crate::std::math::Tan),
|
|
|
|
Box::new(crate::std::math::Acos),
|
|
|
|
Box::new(crate::std::math::Asin),
|
|
|
|
Box::new(crate::std::math::Atan),
|
|
|
|
Box::new(crate::std::math::Pi),
|
|
|
|
Box::new(crate::std::math::E),
|
|
|
|
Box::new(crate::std::math::Tau),
|
|
|
|
Box::new(crate::std::math::Sqrt),
|
|
|
|
Box::new(crate::std::math::Abs),
|
|
|
|
Box::new(crate::std::math::Floor),
|
|
|
|
Box::new(crate::std::math::Ceil),
|
|
|
|
Box::new(crate::std::math::Min),
|
|
|
|
Box::new(crate::std::math::Max),
|
|
|
|
Box::new(crate::std::math::Pow),
|
|
|
|
Box::new(crate::std::math::Log),
|
|
|
|
Box::new(crate::std::math::Log2),
|
|
|
|
Box::new(crate::std::math::Log10),
|
|
|
|
Box::new(crate::std::math::Ln),
|
2024-03-13 00:33:50 -07:00
|
|
|
Box::new(crate::std::math::ToDegrees),
|
|
|
|
Box::new(crate::std::math::ToRadians),
|
2023-11-08 20:23:59 -06:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name_in_stdlib(name: &str) -> bool {
|
|
|
|
CORE_FNS.iter().any(|f| f.name() == name)
|
|
|
|
}
|
|
|
|
|
2023-08-25 13:41:04 -07:00
|
|
|
pub struct StdLib {
|
2023-11-09 09:58:20 -06:00
|
|
|
pub fns: HashMap<String, Box<dyn StdLibFn>>,
|
|
|
|
pub kcl_fns: HashMap<String, Box<dyn KclStdLibFn>>,
|
2023-11-08 20:23:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for StdLib {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2023-11-09 09:58:20 -06:00
|
|
|
f.debug_struct("StdLib")
|
|
|
|
.field("fns.len()", &self.fns.len())
|
|
|
|
.field("kcl_fns.len()", &self.kcl_fns.len())
|
|
|
|
.finish()
|
2023-11-08 20:23:59 -06:00
|
|
|
}
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StdLib {
|
|
|
|
pub fn new() -> Self {
|
2023-11-08 20:23:59 -06:00
|
|
|
let fns = CORE_FNS
|
|
|
|
.clone()
|
2023-11-07 12:12:18 -06:00
|
|
|
.into_iter()
|
|
|
|
.map(|internal_fn| (internal_fn.name(), internal_fn))
|
|
|
|
.collect();
|
2023-08-25 13:41:04 -07:00
|
|
|
|
2023-11-09 09:58:20 -06:00
|
|
|
let kcl_internal_fns: [Box<dyn KclStdLibFn>; 1] = [Box::<shapes::Circle>::default()];
|
|
|
|
let kcl_fns = kcl_internal_fns
|
|
|
|
.into_iter()
|
|
|
|
.map(|internal_fn| (internal_fn.name(), internal_fn))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Self { fns, kcl_fns }
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
2024-03-01 14:23:30 -08:00
|
|
|
// Get the combined hashmaps.
|
|
|
|
pub fn combined(&self) -> HashMap<String, Box<dyn StdLibFn>> {
|
|
|
|
let mut combined = self.fns.clone();
|
|
|
|
for (k, v) in self.kcl_fns.clone() {
|
|
|
|
combined.insert(k, v.std_lib());
|
|
|
|
}
|
|
|
|
combined
|
|
|
|
}
|
|
|
|
|
2023-11-08 20:23:59 -06:00
|
|
|
pub fn get(&self, name: &str) -> Option<Box<dyn StdLibFn>> {
|
2023-09-05 16:02:27 -07:00
|
|
|
self.fns.get(name).cloned()
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
2023-11-09 09:58:20 -06:00
|
|
|
|
|
|
|
pub fn get_kcl(&self, name: &str) -> Option<Box<dyn KclStdLibFn>> {
|
|
|
|
self.kcl_fns.get(name).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_either(&self, name: &str) -> FunctionKind {
|
|
|
|
if let Some(f) = self.get(name) {
|
|
|
|
FunctionKind::Core(f)
|
|
|
|
} else if let Some(f) = self.get_kcl(name) {
|
|
|
|
FunctionKind::Std(f)
|
|
|
|
} else {
|
|
|
|
FunctionKind::UserDefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains_key(&self, key: &str) -> bool {
|
|
|
|
self.fns.contains_key(key) || self.kcl_fns.contains_key(key)
|
|
|
|
}
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for StdLib {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
|
Remove just one enum (#1096)
# Problem
This is my proposal for fixing #1107 . I've only done it for one stdlib function, `tangentialArcTo` -- if y'all like it, I'll apply this idea to the rest of the stdlib.
Previously, if users want to put a tag on the arc, the function's parameters change type.
```
// Tag missing: first param is array
tangentialArcTo([x, y], %)
// Tag present: first param is object
tangentialArcTo({to: [x, y], tag: "myTag"}, %)
```
# Solution
My proposal in #1006 is that KCL should have optional values. This means we can change the stdlib `tangentialArcTo` function to use them. In this PR, the calls are now like
```
// Tag missing: first param is array
tangentialArcTo([x, y], %)
// Tag present: first param is array still, but we now pass a tag at the end.
tangentialArcTo([x, y], %, "myTag")
```
This adds an "option" type to KCL typesystem, but it's not really revealed to users (no KCL types are revealed to users right now, they write untyped code and only interact with types when they get type errors upon executing programs). Also adds a None type, which is the default case of the Optional enum.
2023-12-18 23:49:32 -06:00
|
|
|
#[derive(Debug)]
|
2023-11-09 09:58:20 -06:00
|
|
|
pub enum FunctionKind {
|
|
|
|
Core(Box<dyn StdLibFn>),
|
|
|
|
Std(Box<dyn KclStdLibFn>),
|
|
|
|
UserDefined,
|
|
|
|
}
|
|
|
|
|
2023-09-20 18:27:08 -07:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Args {
|
2023-08-24 15:34:51 -07:00
|
|
|
pub args: Vec<MemoryItem>,
|
|
|
|
pub source_range: SourceRange,
|
2023-10-05 14:27:48 -07:00
|
|
|
pub ctx: ExecutorContext,
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
|
2023-09-20 18:27:08 -07:00
|
|
|
impl Args {
|
2023-10-05 14:27:48 -07:00
|
|
|
pub fn new(args: Vec<MemoryItem>, source_range: SourceRange, ctx: ExecutorContext) -> Self {
|
2023-08-24 15:34:51 -07:00
|
|
|
Self {
|
|
|
|
args,
|
|
|
|
source_range,
|
2023-10-05 14:27:48 -07:00
|
|
|
ctx,
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
}
|
2023-08-29 16:31:19 -07:00
|
|
|
|
2023-09-20 18:27:08 -07:00
|
|
|
pub async fn send_modeling_cmd(
|
|
|
|
&self,
|
|
|
|
id: uuid::Uuid,
|
|
|
|
cmd: kittycad::types::ModelingCmd,
|
|
|
|
) -> Result<OkWebSocketResponseData, KclError> {
|
2023-10-05 14:27:48 -07:00
|
|
|
self.ctx.engine.send_modeling_cmd(id, self.source_range, cmd).await
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_user_val_from_json(&self, j: serde_json::Value) -> Result<MemoryItem, KclError> {
|
2023-09-12 18:10:27 -07:00
|
|
|
Ok(MemoryItem::UserVal(crate::executor::UserVal {
|
2023-08-24 15:34:51 -07:00
|
|
|
value: j,
|
|
|
|
meta: vec![Metadata {
|
|
|
|
source_range: self.source_range,
|
|
|
|
}],
|
2023-09-12 18:10:27 -07:00
|
|
|
}))
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_user_val_from_f64(&self, f: f64) -> Result<MemoryItem, KclError> {
|
2023-08-29 14:12:48 -07:00
|
|
|
self.make_user_val_from_json(serde_json::Value::Number(serde_json::Number::from_f64(f).ok_or_else(
|
|
|
|
|| {
|
2023-08-24 15:34:51 -07:00
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Failed to convert `{}` to a number", f),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
2023-08-29 14:12:48 -07:00
|
|
|
},
|
|
|
|
)?))
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
|
2023-09-13 15:09:07 -07:00
|
|
|
fn get_number(&self) -> Result<f64, KclError> {
|
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a number as the first argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
parse_json_number_as_f64(&first_value, self.source_range)
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:34:51 -07:00
|
|
|
fn get_number_array(&self) -> Result<Vec<f64>, KclError> {
|
|
|
|
let mut numbers: Vec<f64> = Vec::new();
|
|
|
|
for arg in &self.args {
|
|
|
|
let parsed = arg.get_json_value()?;
|
|
|
|
numbers.push(parse_json_number_as_f64(&parsed, self.source_range)?);
|
|
|
|
}
|
|
|
|
Ok(numbers)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_hypotenuse_leg(&self) -> Result<(f64, f64), KclError> {
|
|
|
|
let numbers = self.get_number_array()?;
|
|
|
|
|
|
|
|
if numbers.len() != 2 {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a number array of length 2, found `{:?}`", numbers),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((numbers[0], numbers[1]))
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:20:14 -07:00
|
|
|
fn get_segment_name_sketch_group(&self) -> Result<(String, Box<SketchGroup>), KclError> {
|
2023-08-24 15:34:51 -07:00
|
|
|
// Iterate over our args, the first argument should be a UserVal with a string value.
|
|
|
|
// The second argument should be a SketchGroup.
|
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a string as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let segment_name = if let serde_json::Value::String(s) = first_value {
|
|
|
|
s.to_string()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a string as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
let second_value = self.args.get(1).ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the second argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let sketch_group = if let MemoryItem::SketchGroup(sg) = second_value {
|
|
|
|
sg.clone()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the second argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((segment_name, sketch_group))
|
|
|
|
}
|
|
|
|
|
2024-02-11 15:08:54 -08:00
|
|
|
fn get_sketch_groups(&self) -> Result<(SketchGroupSet, Box<SketchGroup>), KclError> {
|
2023-10-13 12:02:46 -07:00
|
|
|
let first_value = self.args.first().ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a SketchGroup as the first argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
2024-02-11 15:08:54 -08:00
|
|
|
let sketch_set = if let MemoryItem::SketchGroup(sg) = first_value {
|
|
|
|
SketchGroupSet::SketchGroup(sg.clone())
|
|
|
|
} else if let MemoryItem::SketchGroups { value } = first_value {
|
|
|
|
SketchGroupSet::SketchGroups(value.clone())
|
2023-10-13 12:02:46 -07:00
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2024-02-11 15:08:54 -08:00
|
|
|
message: format!(
|
|
|
|
"Expected a SketchGroup or Vector of SketchGroups as the first argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
2023-10-13 12:02:46 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
let second_value = self.args.get(1).ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a SketchGroup as the second argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
2024-02-11 15:08:54 -08:00
|
|
|
let sketch_group = if let MemoryItem::SketchGroup(sg) = second_value {
|
2023-10-13 12:02:46 -07:00
|
|
|
sg.clone()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a SketchGroup as the second argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2024-02-11 15:08:54 -08:00
|
|
|
Ok((sketch_set, sketch_group))
|
2023-10-13 12:02:46 -07:00
|
|
|
}
|
|
|
|
|
2023-09-19 14:20:14 -07:00
|
|
|
fn get_sketch_group(&self) -> Result<Box<SketchGroup>, KclError> {
|
2023-08-24 15:34:51 -07:00
|
|
|
let first_value = self.args.first().ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let sketch_group = if let MemoryItem::SketchGroup(sg) = first_value {
|
|
|
|
sg.clone()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(sketch_group)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, KclError> {
|
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a struct as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let data: T = serde_json::from_value(first_value).map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Failed to deserialize struct from JSON: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
2024-02-12 12:18:37 -08:00
|
|
|
fn get_import_data(&self) -> Result<(String, Option<crate::std::import::ImportFormat>), KclError> {
|
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a struct as the first argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
let data: String = serde_json::from_value(first_value).map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a file path string: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if let Some(second_value) = self.args.get(1) {
|
|
|
|
let options: crate::std::import::ImportFormat = serde_json::from_value(second_value.get_json_value()?)
|
|
|
|
.map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected input format data: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
Ok((data, Some(options)))
|
|
|
|
} else {
|
|
|
|
Ok((data, None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 12:35:56 -08:00
|
|
|
fn get_sketch_group_and_optional_tag(&self) -> Result<(Box<SketchGroup>, Option<String>), KclError> {
|
|
|
|
let first_value = self.args.first().ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a SketchGroup as the first argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let sketch_group = if let MemoryItem::SketchGroup(sg) = first_value {
|
|
|
|
sg.clone()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a SketchGroup as the first argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(second_value) = self.args.get(1) {
|
|
|
|
let tag: String = serde_json::from_value(second_value.get_json_value()?).map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Failed to deserialize String from JSON: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
Ok((sketch_group, Some(tag)))
|
|
|
|
} else {
|
|
|
|
Ok((sketch_group, None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 10:26:09 -08:00
|
|
|
fn get_data_and_optional_tag<T: serde::de::DeserializeOwned>(
|
|
|
|
&self,
|
|
|
|
) -> Result<(T, Option<SketchOnFaceTag>), KclError> {
|
2024-02-12 18:08:42 -08:00
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a struct as the first argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let data: T = serde_json::from_value(first_value).map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Failed to deserialize struct from JSON: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if let Some(second_value) = self.args.get(1) {
|
2024-02-13 10:26:09 -08:00
|
|
|
let tag: SketchOnFaceTag = serde_json::from_value(second_value.get_json_value()?).map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Failed to deserialize SketchOnFaceTag from JSON: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
Ok((data, Some(tag)))
|
2024-02-12 18:08:42 -08:00
|
|
|
} else {
|
|
|
|
Ok((data, None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:20:14 -07:00
|
|
|
fn get_data_and_sketch_group<T: serde::de::DeserializeOwned>(&self) -> Result<(T, Box<SketchGroup>), KclError> {
|
2023-08-24 15:34:51 -07:00
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a struct as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let data: T = serde_json::from_value(first_value).map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Failed to deserialize struct from JSON: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let second_value = self.args.get(1).ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the second argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let sketch_group = if let MemoryItem::SketchGroup(sg) = second_value {
|
|
|
|
sg.clone()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the second argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((data, sketch_group))
|
|
|
|
}
|
|
|
|
|
2024-02-12 18:08:42 -08:00
|
|
|
fn get_data_and_sketch_surface<T: serde::de::DeserializeOwned>(&self) -> Result<(T, SketchSurface), KclError> {
|
2023-10-05 14:27:48 -07:00
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a struct as the first argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let data: T = serde_json::from_value(first_value).map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Failed to deserialize struct from JSON: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let second_value = self.args.get(1).ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a Plane as the second argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
2024-02-12 18:08:42 -08:00
|
|
|
let sketch_surface = if let MemoryItem::Plane(p) = second_value {
|
|
|
|
SketchSurface::Plane(p.clone())
|
|
|
|
} else if let MemoryItem::Face(face) = second_value {
|
|
|
|
SketchSurface::Face(face.clone())
|
2023-10-05 14:27:48 -07:00
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2024-02-12 18:08:42 -08:00
|
|
|
message: format!(
|
|
|
|
"Expected a plane or face (SketchSurface) as the second argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
2023-10-05 14:27:48 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2024-02-12 18:08:42 -08:00
|
|
|
Ok((data, sketch_surface))
|
2023-10-05 14:27:48 -07:00
|
|
|
}
|
|
|
|
|
2024-03-05 11:52:45 -08:00
|
|
|
fn get_data_and_extrude_group<T: serde::de::DeserializeOwned>(&self) -> Result<(T, Box<ExtrudeGroup>), KclError> {
|
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a struct as the first argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let data: T = serde_json::from_value(first_value).map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Failed to deserialize struct from JSON: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let second_value = self.args.get(1).ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!(
|
|
|
|
"Expected an ExtrudeGroup as the second argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let extrude_group = if let MemoryItem::ExtrudeGroup(eg) = second_value {
|
|
|
|
eg.clone()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!(
|
|
|
|
"Expected an ExtrudeGroup as the second argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((data, extrude_group))
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:20:14 -07:00
|
|
|
fn get_segment_name_to_number_sketch_group(&self) -> Result<(String, f64, Box<SketchGroup>), KclError> {
|
2023-08-24 15:34:51 -07:00
|
|
|
// Iterate over our args, the first argument should be a UserVal with a string value.
|
|
|
|
// The second argument should be a number.
|
|
|
|
// The third argument should be a SketchGroup.
|
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a string as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let segment_name = if let serde_json::Value::String(s) = first_value {
|
|
|
|
s.to_string()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a string as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
let second_value = self
|
|
|
|
.args
|
|
|
|
.get(1)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a number as the second argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let to_number = parse_json_number_as_f64(&second_value, self.source_range)?;
|
|
|
|
|
|
|
|
let third_value = self.args.get(2).ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the third argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let sketch_group = if let MemoryItem::SketchGroup(sg) = third_value {
|
|
|
|
sg.clone()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the third argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((segment_name, to_number, sketch_group))
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:20:14 -07:00
|
|
|
fn get_number_sketch_group(&self) -> Result<(f64, Box<SketchGroup>), KclError> {
|
2023-08-24 15:34:51 -07:00
|
|
|
// Iterate over our args, the first argument should be a number.
|
|
|
|
// The second argument should be a SketchGroup.
|
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a number as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let number = parse_json_number_as_f64(&first_value, self.source_range)?;
|
|
|
|
|
|
|
|
let second_value = self.args.get(1).ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the second argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let sketch_group = if let MemoryItem::SketchGroup(sg) = second_value {
|
|
|
|
sg.clone()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a SketchGroup as the second argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((number, sketch_group))
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:20:14 -07:00
|
|
|
fn get_path_name_extrude_group(&self) -> Result<(String, Box<ExtrudeGroup>), KclError> {
|
2023-08-24 15:34:51 -07:00
|
|
|
// Iterate over our args, the first argument should be a UserVal with a string value.
|
|
|
|
// The second argument should be a ExtrudeGroup.
|
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a string as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let path_name = if let serde_json::Value::String(s) = first_value {
|
|
|
|
s.to_string()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2023-08-29 14:12:48 -07:00
|
|
|
message: format!("Expected a string as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
let second_value = self.args.get(1).ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!(
|
|
|
|
"Expected a ExtrudeGroup as the second argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let extrude_group = if let MemoryItem::ExtrudeGroup(sg) = second_value {
|
|
|
|
sg.clone()
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!(
|
|
|
|
"Expected a ExtrudeGroup as the second argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((path_name, extrude_group))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the length of the given leg.
|
2023-09-20 18:27:08 -07:00
|
|
|
pub async fn leg_length(args: Args) -> Result<MemoryItem, KclError> {
|
2023-08-24 15:34:51 -07:00
|
|
|
let (hypotenuse, leg) = args.get_hypotenuse_leg()?;
|
2023-08-25 13:41:04 -07:00
|
|
|
let result = inner_leg_length(hypotenuse, leg);
|
2023-08-24 15:34:51 -07:00
|
|
|
args.make_user_val_from_f64(result)
|
|
|
|
}
|
|
|
|
|
2023-08-25 13:41:04 -07:00
|
|
|
/// Returns the length of the given leg.
|
2024-03-13 12:56:46 -07:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// legLen(5, 3)
|
|
|
|
/// ```
|
2023-08-25 13:41:04 -07:00
|
|
|
#[stdlib {
|
|
|
|
name = "legLen",
|
|
|
|
}]
|
|
|
|
fn inner_leg_length(hypotenuse: f64, leg: f64) -> f64 {
|
|
|
|
(hypotenuse.powi(2) - f64::min(hypotenuse.abs(), leg.abs()).powi(2)).sqrt()
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:34:51 -07:00
|
|
|
/// Returns the angle of the given leg for x.
|
2023-09-20 18:27:08 -07:00
|
|
|
pub async fn leg_angle_x(args: Args) -> Result<MemoryItem, KclError> {
|
2023-08-24 15:34:51 -07:00
|
|
|
let (hypotenuse, leg) = args.get_hypotenuse_leg()?;
|
2023-08-25 13:41:04 -07:00
|
|
|
let result = inner_leg_angle_x(hypotenuse, leg);
|
2023-08-24 15:34:51 -07:00
|
|
|
args.make_user_val_from_f64(result)
|
|
|
|
}
|
|
|
|
|
2023-08-25 13:41:04 -07:00
|
|
|
/// Returns the angle of the given leg for x.
|
2024-03-13 12:56:46 -07:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// legAngX(5, 3)
|
|
|
|
/// ```
|
2023-08-25 13:41:04 -07:00
|
|
|
#[stdlib {
|
|
|
|
name = "legAngX",
|
|
|
|
}]
|
|
|
|
fn inner_leg_angle_x(hypotenuse: f64, leg: f64) -> f64 {
|
2023-09-13 22:25:41 -06:00
|
|
|
(leg.min(hypotenuse) / hypotenuse).acos().to_degrees()
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
2023-08-24 15:34:51 -07:00
|
|
|
/// Returns the angle of the given leg for y.
|
2023-09-20 18:27:08 -07:00
|
|
|
pub async fn leg_angle_y(args: Args) -> Result<MemoryItem, KclError> {
|
2023-08-24 15:34:51 -07:00
|
|
|
let (hypotenuse, leg) = args.get_hypotenuse_leg()?;
|
2023-08-25 13:41:04 -07:00
|
|
|
let result = inner_leg_angle_y(hypotenuse, leg);
|
2023-08-24 15:34:51 -07:00
|
|
|
args.make_user_val_from_f64(result)
|
|
|
|
}
|
2023-08-25 13:41:04 -07:00
|
|
|
|
|
|
|
/// Returns the angle of the given leg for y.
|
2024-03-13 12:56:46 -07:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// legAngY(5, 3)
|
|
|
|
/// ```
|
2023-08-25 13:41:04 -07:00
|
|
|
#[stdlib {
|
|
|
|
name = "legAngY",
|
|
|
|
}]
|
|
|
|
fn inner_leg_angle_y(hypotenuse: f64, leg: f64) -> f64 {
|
2023-09-13 22:25:41 -06:00
|
|
|
(leg.min(hypotenuse) / hypotenuse).asin().to_degrees()
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The primitive types that can be used in a KCL file.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
|
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
#[display(style = "lowercase")]
|
|
|
|
pub enum Primitive {
|
|
|
|
/// A boolean value.
|
|
|
|
Bool,
|
|
|
|
/// A number value.
|
|
|
|
Number,
|
|
|
|
/// A string value.
|
|
|
|
String,
|
|
|
|
/// A uuid value.
|
|
|
|
Uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-09-05 16:02:27 -07:00
|
|
|
use itertools::Itertools;
|
2023-08-25 13:41:04 -07:00
|
|
|
|
2023-09-17 21:57:43 -07:00
|
|
|
use crate::std::StdLib;
|
|
|
|
|
2023-08-25 13:41:04 -07:00
|
|
|
#[test]
|
|
|
|
fn test_generate_stdlib_markdown_docs() {
|
|
|
|
let stdlib = StdLib::new();
|
2024-03-01 14:23:30 -08:00
|
|
|
let combined = stdlib.combined();
|
2023-08-25 13:41:04 -07:00
|
|
|
let mut buf = String::new();
|
|
|
|
|
2024-03-13 14:22:22 -07:00
|
|
|
buf.push_str(
|
|
|
|
r#"---
|
|
|
|
title: "KCL Standard Library"
|
|
|
|
excerpt: "Documentation for the KCL standard library for the Zoo Modeling App."
|
|
|
|
layout: manual
|
|
|
|
---
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
2023-08-25 13:41:04 -07:00
|
|
|
|
|
|
|
// Generate a table of contents.
|
|
|
|
buf.push_str("## Table of Contents\n\n");
|
|
|
|
|
2024-03-13 15:01:35 -07:00
|
|
|
buf.push_str("* [Types](kcl/types)\n");
|
|
|
|
buf.push_str("* [Known Issues](kcl/KNOWN-ISSUES)\n");
|
2023-08-25 13:41:04 -07:00
|
|
|
|
2024-03-01 14:23:30 -08:00
|
|
|
for key in combined.keys().sorted() {
|
|
|
|
let internal_fn = combined.get(key).unwrap();
|
2023-08-25 13:41:04 -07:00
|
|
|
if internal_fn.unpublished() || internal_fn.deprecated() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-03-13 15:01:35 -07:00
|
|
|
buf.push_str(&format!("* [`{}`](kcl/{})\n", internal_fn.name(), internal_fn.name()));
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
2024-03-13 14:22:22 -07:00
|
|
|
// Write the index.
|
|
|
|
expectorate::assert_contents("../../../docs/kcl/index.md", &buf);
|
2023-08-25 13:41:04 -07:00
|
|
|
|
2024-03-01 14:23:30 -08:00
|
|
|
for key in combined.keys().sorted() {
|
2024-03-13 14:22:22 -07:00
|
|
|
let mut buf = String::new();
|
2024-03-01 14:23:30 -08:00
|
|
|
let internal_fn = combined.get(key).unwrap();
|
2023-08-25 13:41:04 -07:00
|
|
|
if internal_fn.unpublished() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut fn_docs = String::new();
|
|
|
|
|
2024-03-13 14:22:22 -07:00
|
|
|
fn_docs.push_str(&format!(
|
|
|
|
r#"---
|
|
|
|
title: "{}"
|
|
|
|
excerpt: "{}"
|
|
|
|
layout: manual
|
|
|
|
---
|
|
|
|
|
|
|
|
"#,
|
|
|
|
internal_fn.name(),
|
|
|
|
internal_fn.summary()
|
|
|
|
));
|
|
|
|
|
2023-08-25 13:41:04 -07:00
|
|
|
if internal_fn.deprecated() {
|
2024-03-13 14:22:22 -07:00
|
|
|
fn_docs.push_str("**WARNING:** This function is deprecated.\n\n");
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn_docs.push_str(&format!("{}\n\n", internal_fn.summary()));
|
|
|
|
fn_docs.push_str(&format!("{}\n\n", internal_fn.description()));
|
|
|
|
|
2024-03-13 15:43:42 -07:00
|
|
|
fn_docs.push_str("```js\n");
|
2023-08-31 22:19:23 -07:00
|
|
|
let signature = internal_fn.fn_signature();
|
|
|
|
fn_docs.push_str(&signature);
|
2023-08-25 13:41:04 -07:00
|
|
|
fn_docs.push_str("\n```\n\n");
|
|
|
|
|
2024-03-13 12:56:46 -07:00
|
|
|
if !internal_fn.examples().is_empty() {
|
2024-03-13 14:22:22 -07:00
|
|
|
fn_docs.push_str("### Examples\n\n");
|
2024-03-13 12:56:46 -07:00
|
|
|
|
|
|
|
for example in internal_fn.examples() {
|
2024-03-13 15:43:42 -07:00
|
|
|
fn_docs.push_str("```js\n");
|
2024-03-13 12:56:46 -07:00
|
|
|
fn_docs.push_str(&example);
|
|
|
|
fn_docs.push_str("\n```\n\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 14:22:22 -07:00
|
|
|
fn_docs.push_str("### Arguments\n\n");
|
2023-08-25 13:41:04 -07:00
|
|
|
for arg in internal_fn.args() {
|
|
|
|
let (format, should_be_indented) = arg.get_type_string().unwrap();
|
2024-03-07 12:35:56 -08:00
|
|
|
let optional_string = if arg.required { " (REQUIRED)" } else { " (OPTIONAL)" }.to_string();
|
2023-08-25 13:41:04 -07:00
|
|
|
if let Some(description) = arg.description() {
|
2024-03-07 12:35:56 -08:00
|
|
|
fn_docs.push_str(&format!(
|
|
|
|
"* `{}`: `{}` - {}{}\n",
|
|
|
|
arg.name, arg.type_, description, optional_string
|
|
|
|
));
|
2023-08-25 13:41:04 -07:00
|
|
|
} else {
|
2024-03-07 12:35:56 -08:00
|
|
|
fn_docs.push_str(&format!("* `{}`: `{}`{}\n", arg.name, arg.type_, optional_string));
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if should_be_indented {
|
2024-03-13 15:43:42 -07:00
|
|
|
fn_docs.push_str(&format!("```js\n{}\n```\n", format));
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 16:02:27 -07:00
|
|
|
if let Some(return_type) = internal_fn.return_value() {
|
2024-03-13 14:22:22 -07:00
|
|
|
fn_docs.push_str("\n### Returns\n\n");
|
2023-09-05 16:02:27 -07:00
|
|
|
if let Some(description) = return_type.description() {
|
2024-03-13 14:22:22 -07:00
|
|
|
fn_docs.push_str(&format!("`{}` - {}\n", return_type.type_, description));
|
2023-09-05 16:02:27 -07:00
|
|
|
} else {
|
2024-03-13 14:22:22 -07:00
|
|
|
fn_docs.push_str(&format!("`{}`\n", return_type.type_));
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
2023-08-25 13:41:04 -07:00
|
|
|
|
2023-09-05 16:02:27 -07:00
|
|
|
let (format, should_be_indented) = return_type.get_type_string().unwrap();
|
|
|
|
if should_be_indented {
|
2024-03-13 15:43:42 -07:00
|
|
|
fn_docs.push_str(&format!("```js\n{}\n```\n", format));
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn_docs.push_str("\n\n\n");
|
|
|
|
|
|
|
|
buf.push_str(&fn_docs);
|
|
|
|
|
2024-03-13 14:22:22 -07:00
|
|
|
// Write the file.
|
|
|
|
expectorate::assert_contents(&format!("../../../docs/kcl/{}.md", internal_fn.name()), &buf);
|
|
|
|
}
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_stdlib_json_schema() {
|
|
|
|
let stdlib = StdLib::new();
|
2024-03-01 14:23:30 -08:00
|
|
|
let combined = stdlib.combined();
|
2023-08-25 13:41:04 -07:00
|
|
|
|
|
|
|
let mut json_data = vec![];
|
|
|
|
|
2024-03-01 14:23:30 -08:00
|
|
|
for key in combined.keys().sorted() {
|
|
|
|
let internal_fn = combined.get(key).unwrap();
|
2023-08-25 13:41:04 -07:00
|
|
|
json_data.push(internal_fn.to_json().unwrap());
|
|
|
|
}
|
|
|
|
expectorate::assert_contents(
|
2023-09-13 11:59:21 -07:00
|
|
|
"../../../docs/kcl/std.json",
|
2023-08-25 13:41:04 -07:00
|
|
|
&serde_json::to_string_pretty(&json_data).unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|