2023-08-24 15:34:51 -07:00
|
|
|
//! Functions implemented for language execution.
|
|
|
|
|
2024-06-17 12:13:19 -07:00
|
|
|
pub mod chamfer;
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod extrude;
|
2024-03-05 11:52:45 -08:00
|
|
|
pub mod fillet;
|
2024-03-25 17:07:53 -07:00
|
|
|
pub mod helix;
|
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;
|
2024-03-26 19:07:16 -07:00
|
|
|
pub mod revolve;
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod segment;
|
2023-11-09 09:58:20 -06:00
|
|
|
pub mod shapes;
|
2024-06-17 13:10:40 -07:00
|
|
|
pub mod shell;
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod sketch;
|
2024-06-24 14:45:07 -07:00
|
|
|
pub mod string_or_struct;
|
2024-04-23 15:59:12 -07:00
|
|
|
pub mod types;
|
2023-08-28 14:58:24 -07:00
|
|
|
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-06-24 14:45:07 -07:00
|
|
|
ast::types::{parse_json_number_as_f64, TagDeclarator},
|
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-06-23 23:04:32 -07:00
|
|
|
ExecutorContext, ExtrudeGroup, ExtrudeGroupSet, ExtrudeSurface, MemoryItem, Metadata, ProgramMemory,
|
2024-06-24 14:45:07 -07:00
|
|
|
SketchGroup, SketchGroupSet, SketchSurface, SourceRange, TagIdentifier,
|
2024-02-15 13:56:31 -08:00
|
|
|
},
|
2024-06-23 23:04:32 -07:00
|
|
|
std::{kcl_stdlib::KclStdLibFn, sketch::FaceTag},
|
2023-08-24 15:34:51 -07:00
|
|
|
};
|
|
|
|
|
2024-04-12 21:32:57 -07:00
|
|
|
pub type StdFn = fn(Args) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<MemoryItem, KclError>> + Send>>;
|
|
|
|
|
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::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),
|
2024-03-13 17:16:57 -07:00
|
|
|
Box::new(crate::std::shapes::Circle),
|
2023-11-08 20:23:59 -06:00
|
|
|
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),
|
2024-05-21 03:44:02 -04:00
|
|
|
Box::new(crate::std::sketch::ProfileStartX),
|
|
|
|
Box::new(crate::std::sketch::ProfileStartY),
|
|
|
|
Box::new(crate::std::sketch::ProfileStart),
|
2023-11-08 20:23:59 -06:00
|
|
|
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-06-17 12:13:19 -07:00
|
|
|
Box::new(crate::std::chamfer::Chamfer),
|
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-03-25 17:07:53 -07:00
|
|
|
Box::new(crate::std::helix::Helix),
|
2024-06-17 13:10:40 -07:00
|
|
|
Box::new(crate::std::shell::Shell),
|
2024-03-26 19:07:16 -07:00
|
|
|
Box::new(crate::std::revolve::Revolve),
|
|
|
|
Box::new(crate::std::revolve::GetEdge),
|
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)
|
|
|
|
}
|
|
|
|
|
2024-06-24 22:39:04 -07:00
|
|
|
pub fn get_stdlib_fn(name: &str) -> Option<Box<dyn StdLibFn>> {
|
|
|
|
CORE_FNS.iter().find(|f| f.name() == name).cloned()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-03-13 17:16:57 -07:00
|
|
|
let kcl_internal_fns: [Box<dyn KclStdLibFn>; 0] = [];
|
2023-11-09 09:58:20 -06:00
|
|
|
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,
|
2024-06-23 19:19:24 -07:00
|
|
|
pub current_program_memory: ProgramMemory,
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
|
2023-09-20 18:27:08 -07:00
|
|
|
impl Args {
|
2024-06-23 19:19:24 -07:00
|
|
|
pub fn new(
|
|
|
|
args: Vec<MemoryItem>,
|
|
|
|
source_range: SourceRange,
|
|
|
|
ctx: ExecutorContext,
|
|
|
|
current_program_memory: ProgramMemory,
|
|
|
|
) -> Self {
|
2023-08-24 15:34:51 -07:00
|
|
|
Self {
|
|
|
|
args,
|
|
|
|
source_range,
|
2023-10-05 14:27:48 -07:00
|
|
|
ctx,
|
2024-06-23 19:19:24 -07:00
|
|
|
current_program_memory,
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
}
|
2023-08-29 16:31:19 -07:00
|
|
|
|
2024-06-19 13:57:50 -07:00
|
|
|
// Add a modeling command to the batch but don't fire it right away.
|
|
|
|
pub async fn batch_modeling_cmd(
|
|
|
|
&self,
|
|
|
|
id: uuid::Uuid,
|
|
|
|
cmd: kittycad::types::ModelingCmd,
|
|
|
|
) -> Result<(), crate::errors::KclError> {
|
|
|
|
self.ctx.engine.batch_modeling_cmd(id, self.source_range, &cmd).await
|
|
|
|
}
|
|
|
|
|
2024-06-22 14:31:37 -07:00
|
|
|
// Add a modeling command to the batch that gets executed at the end of the file.
|
|
|
|
// This is good for something like fillet or chamfer where the engine would
|
|
|
|
// eat the path id if we executed it right away.
|
|
|
|
pub async fn batch_end_cmd(
|
|
|
|
&self,
|
|
|
|
id: uuid::Uuid,
|
|
|
|
cmd: kittycad::types::ModelingCmd,
|
|
|
|
) -> Result<(), crate::errors::KclError> {
|
|
|
|
self.ctx.engine.batch_end_cmd(id, self.source_range, &cmd).await
|
|
|
|
}
|
|
|
|
|
2024-06-19 13:57:50 -07:00
|
|
|
/// Send the modeling cmd and wait for the response.
|
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> {
|
2024-04-09 00:18:35 -05:00
|
|
|
self.ctx.engine.send_modeling_cmd(id, self.source_range, cmd).await
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
|
2024-06-23 19:19:24 -07:00
|
|
|
/// Flush just the fillets and chamfers for this specific ExtrudeGroupSet.
|
2024-06-23 23:04:32 -07:00
|
|
|
pub async fn flush_batch_for_extrude_group_set(
|
|
|
|
&self,
|
|
|
|
extrude_groups: Vec<Box<ExtrudeGroup>>,
|
|
|
|
) -> Result<(), KclError> {
|
2024-06-23 19:19:24 -07:00
|
|
|
// Make sure we don't traverse sketch_groups more than once.
|
|
|
|
let mut traversed_sketch_groups = Vec::new();
|
|
|
|
|
|
|
|
// Collect all the fillet/chamfer ids for the extrude groups.
|
|
|
|
let mut ids = Vec::new();
|
|
|
|
for extrude_group in extrude_groups {
|
|
|
|
// We need to traverse the extrude groups that share the same sketch group.
|
|
|
|
let sketch_group_id = extrude_group.sketch_group.id;
|
|
|
|
if !traversed_sketch_groups.contains(&sketch_group_id) {
|
|
|
|
// Find all the extrude groups on the same shared sketch group.
|
|
|
|
ids.extend(
|
|
|
|
self.current_program_memory
|
|
|
|
.find_extrude_groups_on_sketch_group(extrude_group.sketch_group.id)
|
|
|
|
.iter()
|
|
|
|
.flat_map(|eg| eg.get_all_fillet_or_chamfer_ids()),
|
|
|
|
);
|
|
|
|
traversed_sketch_groups.push(sketch_group_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ids.extend(extrude_group.get_all_fillet_or_chamfer_ids());
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can return early if there are no fillets or chamfers.
|
|
|
|
if ids.is_empty() {
|
2024-06-22 14:31:37 -07:00
|
|
|
return Ok(());
|
|
|
|
}
|
2024-06-23 19:19:24 -07:00
|
|
|
|
|
|
|
// We want to move these fillets and chamfers from batch_end to batch so they get executed
|
|
|
|
// before what ever we call next.
|
|
|
|
for id in ids {
|
|
|
|
// Pop it off the batch_end and add it to the batch.
|
|
|
|
let Some(item) = self.ctx.engine.batch_end().lock().unwrap().remove(&id) else {
|
|
|
|
// It might be in the batch already.
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
// Add it to the batch.
|
|
|
|
self.ctx.engine.batch().lock().unwrap().push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run flush.
|
|
|
|
// Yes, we do need to actually flush the batch here, or references will fail later.
|
|
|
|
self.ctx.engine.flush_batch(false, SourceRange::default()).await?;
|
2024-06-22 14:31:37 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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]))
|
|
|
|
}
|
|
|
|
|
2024-03-13 17:16:57 -07:00
|
|
|
fn get_circle_args(
|
|
|
|
&self,
|
2024-06-24 14:45:07 -07:00
|
|
|
) -> Result<
|
|
|
|
(
|
|
|
|
[f64; 2],
|
|
|
|
f64,
|
|
|
|
crate::std::shapes::SketchSurfaceOrGroup,
|
|
|
|
Option<TagDeclarator>,
|
|
|
|
),
|
|
|
|
KclError,
|
|
|
|
> {
|
2024-03-13 17:16:57 -07:00
|
|
|
let first_value = self
|
|
|
|
.args
|
|
|
|
.first()
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!(
|
|
|
|
"Expected a [number, number] as the first argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let center: [f64; 2] = if let serde_json::Value::Array(arr) = first_value {
|
|
|
|
if arr.len() != 2 {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!(
|
|
|
|
"Expected a [number, number] as the first argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
let x = parse_json_number_as_f64(&arr[0], self.source_range)?;
|
|
|
|
let y = parse_json_number_as_f64(&arr[1], self.source_range)?;
|
|
|
|
[x, y]
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!(
|
|
|
|
"Expected a [number, number] as the first argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
let second_value = self
|
|
|
|
.args
|
|
|
|
.get(1)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected a number as the second argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?
|
|
|
|
.get_json_value()?;
|
|
|
|
|
|
|
|
let radius: f64 = serde_json::from_value(second_value).map_err(|e| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Failed to deserialize number from JSON: {}", e),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let third_value = self.args.get(2).ok_or_else(|| {
|
|
|
|
KclError::Type(KclErrorDetails {
|
|
|
|
message: format!(
|
|
|
|
"Expected a SketchGroup or SketchSurface as the third argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let sketch_group_or_surface = if let MemoryItem::SketchGroup(sg) = third_value {
|
|
|
|
crate::std::shapes::SketchSurfaceOrGroup::SketchGroup(sg.clone())
|
|
|
|
} else if let MemoryItem::Plane(sg) = third_value {
|
|
|
|
crate::std::shapes::SketchSurfaceOrGroup::SketchSurface(SketchSurface::Plane(sg.clone()))
|
|
|
|
} else if let MemoryItem::Face(sg) = third_value {
|
|
|
|
crate::std::shapes::SketchSurfaceOrGroup::SketchSurface(SketchSurface::Face(sg.clone()))
|
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!(
|
|
|
|
"Expected a SketchGroup or SketchSurface as the third argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2024-06-24 14:45:07 -07:00
|
|
|
let tag = if let Some(tag) = self.args.get(3) {
|
|
|
|
tag.get_tag_declarator_opt()?
|
2024-03-13 17:16:57 -07:00
|
|
|
} else {
|
2024-06-24 14:45:07 -07:00
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((center, radius, sketch_group_or_surface, tag))
|
2024-03-13 17:16:57 -07:00
|
|
|
}
|
|
|
|
|
2024-06-24 14:45:07 -07:00
|
|
|
fn get_segment_name_sketch_group(&self) -> Result<(TagIdentifier, 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.
|
2024-06-24 14:45:07 -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 string as the first argument, found `{:?}`", self.args),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
2024-06-24 14:45:07 -07:00
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let segment_name = first_value.get_tag_identifier()?;
|
2023-08-24 15:34:51 -07:00
|
|
|
|
|
|
|
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-06-21 16:44:31 -07:00
|
|
|
let sketch_set = match first_value.get_sketch_group_set() {
|
|
|
|
Ok(set) => set,
|
|
|
|
Err(err) => {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected an SketchGroupSet as the first argument: {}", err),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}))
|
|
|
|
}
|
2023-10-13 12:02:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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-06-24 14:45:07 -07:00
|
|
|
fn get_sketch_group_and_optional_tag(&self) -> Result<(Box<SketchGroup>, Option<TagDeclarator>), KclError> {
|
2024-03-07 12:35:56 -08: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],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
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],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2024-06-24 14:45:07 -07:00
|
|
|
let tag = if let Some(tag) = self.args.get(1) {
|
|
|
|
tag.get_tag_declarator_opt()?
|
2024-03-07 12:35:56 -08:00
|
|
|
} else {
|
2024-06-24 14:45:07 -07:00
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((sketch_group, tag))
|
2024-03-07 12:35:56 -08:00
|
|
|
}
|
|
|
|
|
2024-06-23 23:04:32 -07:00
|
|
|
fn get_data_and_optional_tag<T: serde::de::DeserializeOwned>(&self) -> Result<(T, Option<FaceTag>), 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-06-23 23:04:32 -07:00
|
|
|
let tag: FaceTag = serde_json::from_value(second_value.get_json_value()?).map_err(|e| {
|
2024-02-13 10:26:09 -08:00
|
|
|
KclError::Type(KclErrorDetails {
|
2024-06-23 23:04:32 -07:00
|
|
|
message: format!("Failed to deserialize FaceTag from JSON: {}", e),
|
2024-02-13 10:26:09 -08:00
|
|
|
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-04-23 10:31:20 -07:00
|
|
|
fn get_data_and_sketch_group_set<T: serde::de::DeserializeOwned>(&self) -> Result<(T, SketchGroupSet), 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 a SketchGroup as the second argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
2024-06-21 16:44:31 -07:00
|
|
|
let sketch_set = match second_value.get_sketch_group_set() {
|
|
|
|
Ok(set) => set,
|
|
|
|
Err(err) => {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected an SketchGroupSet as the second argument: {}", err),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}))
|
|
|
|
}
|
2024-04-23 10:31:20 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok((data, sketch_set))
|
|
|
|
}
|
|
|
|
|
2024-03-15 17:03:42 -04:00
|
|
|
fn get_data_and_sketch_group_and_tag<T: serde::de::DeserializeOwned>(
|
|
|
|
&self,
|
2024-06-24 14:45:07 -07:00
|
|
|
) -> Result<(T, Box<SketchGroup>, Option<TagDeclarator>), KclError> {
|
2024-03-15 17:03:42 -04: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 SketchGroup as the second argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let sketch_group = if let MemoryItem::SketchGroup(sg) = second_value {
|
|
|
|
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],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
let tag = if let Some(tag) = self.args.get(2) {
|
2024-06-24 14:45:07 -07:00
|
|
|
tag.get_tag_declarator_opt()?
|
2024-03-15 17:03:42 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((data, sketch_group, tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_data_and_sketch_surface<T: serde::de::DeserializeOwned>(
|
|
|
|
&self,
|
2024-06-24 14:45:07 -07:00
|
|
|
) -> Result<(T, SketchSurface, Option<TagDeclarator>), 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-06-24 14:45:07 -07:00
|
|
|
|
2024-03-15 17:03:42 -04:00
|
|
|
let tag = if let Some(tag) = self.args.get(2) {
|
2024-06-24 14:45:07 -07:00
|
|
|
tag.get_tag_declarator_opt()?
|
2024-03-15 17:03:42 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2023-10-05 14:27:48 -07:00
|
|
|
|
2024-03-15 17:03:42 -04:00
|
|
|
Ok((data, sketch_surface, tag))
|
2023-10-05 14:27:48 -07:00
|
|
|
}
|
|
|
|
|
2024-06-17 13:35:44 -07:00
|
|
|
fn get_data_and_extrude_group_set<T: serde::de::DeserializeOwned>(&self) -> Result<(T, ExtrudeGroupSet), 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],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
2024-06-21 16:44:31 -07:00
|
|
|
let extrude_set = match second_value.get_extrude_group_set() {
|
|
|
|
Ok(set) => set,
|
|
|
|
Err(err) => {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected an ExtrudeGroupSet as the second argument: {}", err),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}))
|
|
|
|
}
|
2024-06-17 13:35:44 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok((data, extrude_set))
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2024-06-23 23:04:32 -07:00
|
|
|
fn get_data_and_extrude_group_and_tag<T: serde::de::DeserializeOwned>(
|
|
|
|
&self,
|
2024-06-24 14:45:07 -07:00
|
|
|
) -> Result<(T, Box<ExtrudeGroup>, Option<TagDeclarator>), KclError> {
|
2024-06-23 23:04:32 -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 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],
|
|
|
|
}));
|
|
|
|
};
|
2024-06-24 14:45:07 -07:00
|
|
|
|
2024-06-23 23:04:32 -07:00
|
|
|
let tag = if let Some(tag) = self.args.get(2) {
|
2024-06-24 14:45:07 -07:00
|
|
|
tag.get_tag_declarator_opt()?
|
2024-06-23 23:04:32 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((data, extrude_group, tag))
|
|
|
|
}
|
|
|
|
|
2024-06-24 14:45:07 -07:00
|
|
|
fn get_tag_and_extrude_group(&self) -> Result<(TagIdentifier, 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],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let tag = first_value.get_tag_identifier()?;
|
|
|
|
|
|
|
|
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],
|
|
|
|
})
|
|
|
|
})?;
|
2023-08-24 15:34:51 -07:00
|
|
|
|
2024-06-24 14:45:07 -07:00
|
|
|
let extrude_group = if let MemoryItem::ExtrudeGroup(eg) = second_value {
|
|
|
|
eg.clone()
|
2023-08-24 15:34:51 -07:00
|
|
|
} else {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
2024-06-24 14:45:07 -07:00
|
|
|
message: format!(
|
|
|
|
"Expected an ExtrudeGroup as the second argument, found `{:?}`",
|
|
|
|
self.args
|
|
|
|
),
|
2023-08-24 15:34:51 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2024-06-24 14:45:07 -07:00
|
|
|
Ok((tag, extrude_group))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_segment_name_to_number_sketch_group(&self) -> Result<(TagIdentifier, f64, Box<SketchGroup>), KclError> {
|
|
|
|
// 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 {
|
|
|
|
message: format!("Expected a string as the first argument, found `{:?}`", self.args),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let segment_name = first_value.get_tag_identifier()?;
|
|
|
|
|
2023-08-24 15:34:51 -07:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2024-04-23 10:31:20 -07:00
|
|
|
fn get_number_sketch_group_set(&self) -> Result<(f64, SketchGroupSet), 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],
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
2024-06-21 16:44:31 -07:00
|
|
|
let sketch_set = match second_value.get_sketch_group_set() {
|
|
|
|
Ok(set) => set,
|
|
|
|
Err(err) => {
|
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: format!("Expected an SketchGroupSet as the second argument: {}", err),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}))
|
|
|
|
}
|
2023-08-24 15:34:51 -07:00
|
|
|
};
|
|
|
|
|
2024-04-23 10:31:20 -07:00
|
|
|
Ok((number, sketch_set))
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
2024-06-23 23:04:32 -07:00
|
|
|
|
|
|
|
pub async fn get_adjacent_face_to_tag(
|
|
|
|
&self,
|
|
|
|
extrude_group: &ExtrudeGroup,
|
2024-06-24 14:45:07 -07:00
|
|
|
tag: &TagIdentifier,
|
2024-06-23 23:04:32 -07:00
|
|
|
must_be_planar: bool,
|
|
|
|
) -> Result<uuid::Uuid, KclError> {
|
2024-06-24 14:45:07 -07:00
|
|
|
if tag.value.is_empty() {
|
2024-06-23 23:04:32 -07:00
|
|
|
return Err(KclError::Type(KclErrorDetails {
|
|
|
|
message: "Expected a non-empty tag for the face".to_string(),
|
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(face_from_surface) = extrude_group
|
|
|
|
.value
|
|
|
|
.iter()
|
|
|
|
.find_map(|extrude_surface| match extrude_surface {
|
2024-06-24 14:45:07 -07:00
|
|
|
ExtrudeSurface::ExtrudePlane(extrude_plane) => {
|
|
|
|
if let Some(plane_tag) = &extrude_plane.tag {
|
|
|
|
if plane_tag.name == tag.value {
|
|
|
|
Some(Ok(extrude_plane.face_id))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2024-06-23 23:04:32 -07:00
|
|
|
}
|
|
|
|
// The must be planar check must be called before the arc check.
|
|
|
|
ExtrudeSurface::ExtrudeArc(_) if must_be_planar => Some(Err(KclError::Type(KclErrorDetails {
|
2024-06-24 14:45:07 -07:00
|
|
|
message: format!("Tag `{}` is a non-planar surface", tag.value),
|
2024-06-23 23:04:32 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}))),
|
2024-06-24 14:45:07 -07:00
|
|
|
ExtrudeSurface::ExtrudeArc(extrude_arc) => {
|
|
|
|
if let Some(arc_tag) = &extrude_arc.tag {
|
|
|
|
if arc_tag.name == tag.value {
|
|
|
|
Some(Ok(extrude_arc.face_id))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2024-06-23 23:04:32 -07:00
|
|
|
})
|
|
|
|
{
|
|
|
|
return face_from_surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A face could also be the result of a chamfer or fillet.
|
|
|
|
if let Some(face_from_chamfer_fillet) = extrude_group.fillet_or_chamfers.iter().find_map(|fc| {
|
2024-06-24 14:45:07 -07:00
|
|
|
if let Some(ntag) = &fc.tag() {
|
|
|
|
if ntag.name == tag.value {
|
|
|
|
Some(Ok(fc.id()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2024-06-23 23:04:32 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}) {
|
|
|
|
// We want to make sure we execute the fillet before this operation.
|
|
|
|
self.flush_batch_for_extrude_group_set(extrude_group.into()).await?;
|
|
|
|
|
|
|
|
return face_from_chamfer_fillet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still haven't found the face, return an error.
|
|
|
|
Err(KclError::Type(KclErrorDetails {
|
2024-06-24 14:45:07 -07:00
|
|
|
message: format!("Expected a face with the tag `{}`", tag.value),
|
2024-06-23 23:04:32 -07:00
|
|
|
source_ranges: vec![self.source_range],
|
|
|
|
}))
|
|
|
|
}
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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",
|
2024-03-26 21:28:50 -07:00
|
|
|
tags = ["utilities"],
|
2023-08-25 13:41:04 -07:00
|
|
|
}]
|
|
|
|
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",
|
2024-03-26 21:28:50 -07:00
|
|
|
tags = ["utilities"],
|
2023-08-25 13:41:04 -07:00
|
|
|
}]
|
|
|
|
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",
|
2024-03-26 21:28:50 -07:00
|
|
|
tags = ["utilities"],
|
2023-08-25 13:41:04 -07:00
|
|
|
}]
|
|
|
|
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 {
|
2024-03-26 21:28:50 -07:00
|
|
|
use base64::Engine;
|
|
|
|
use convert_case::Casing;
|
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-26 21:28:50 -07:00
|
|
|
// If the function has tags, we should add them to the docs.
|
2024-05-19 17:02:25 -07:00
|
|
|
let tags = internal_fn.tags().clone();
|
2024-03-26 21:28:50 -07:00
|
|
|
if !tags.is_empty() {
|
|
|
|
fn_docs.push_str("### Tags\n\n");
|
|
|
|
for tag in tags {
|
|
|
|
fn_docs.push_str(&format!("* `{}`\n", tag));
|
|
|
|
}
|
|
|
|
fn_docs.push('\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
|
|
|
|
2024-03-26 21:28:50 -07:00
|
|
|
for (index, example) in internal_fn.examples().iter().enumerate() {
|
2024-03-13 15:43:42 -07:00
|
|
|
fn_docs.push_str("```js\n");
|
2024-03-26 21:28:50 -07:00
|
|
|
fn_docs.push_str(example);
|
2024-03-13 12:56:46 -07:00
|
|
|
fn_docs.push_str("\n```\n\n");
|
2024-03-26 21:28:50 -07:00
|
|
|
|
2024-05-15 13:09:38 -07:00
|
|
|
// If this is not a "utilities" function,
|
2024-03-26 21:28:50 -07:00
|
|
|
// we should add the image to the docs.
|
2024-05-19 17:02:25 -07:00
|
|
|
if !internal_fn.tags().contains(&"utilities".to_string()) {
|
2024-03-26 21:28:50 -07:00
|
|
|
// Get the path to this specific rust file.
|
|
|
|
let dir = env!("CARGO_MANIFEST_DIR");
|
|
|
|
|
|
|
|
// Convert from camel case to snake case.
|
|
|
|
let mut fn_name = internal_fn.name().to_case(convert_case::Case::Snake);
|
|
|
|
// Clean the fn name.
|
|
|
|
if fn_name.starts_with("last_seg_") {
|
|
|
|
fn_name = fn_name.replace("last_seg_", "last_segment_");
|
|
|
|
} else if fn_name.contains("_2_d") {
|
|
|
|
fn_name = fn_name.replace("_2_d", "_2d");
|
|
|
|
} else if fn_name.contains("_3_d") {
|
|
|
|
fn_name = fn_name.replace("_3_d", "_3d");
|
|
|
|
} else if fn_name == "seg_ang" {
|
|
|
|
fn_name = "segment_angle".to_string();
|
|
|
|
} else if fn_name == "seg_len" {
|
|
|
|
fn_name = "segment_length".to_string();
|
|
|
|
} else if fn_name.starts_with("seg_") {
|
|
|
|
fn_name = fn_name.replace("seg_", "segment_");
|
2024-05-15 13:09:38 -07:00
|
|
|
} else if fn_name.starts_with("log_") {
|
|
|
|
fn_name = fn_name.replace("log_", "log");
|
2024-03-26 21:28:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the image file and encode as base64.
|
|
|
|
let image_path = format!("{}/tests/outputs/serial_test_example_{}{}.png", dir, fn_name, index);
|
|
|
|
|
|
|
|
let image_data = std::fs::read(&image_path)
|
|
|
|
.unwrap_or_else(|_| panic!("Failed to read image file: {}", image_path));
|
|
|
|
let encoded = base64::engine::general_purpose::STANDARD.encode(&image_data);
|
|
|
|
|
|
|
|
fn_docs.push_str(&format!(
|
|
|
|
r#"
|
|
|
|
|
|
|
|
"#,
|
|
|
|
internal_fn.name(),
|
|
|
|
index,
|
|
|
|
encoded,
|
|
|
|
));
|
|
|
|
}
|
2024-03-13 12:56:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|