kcl_lib/std/
mod.rs

1//! Functions implemented for language execution.
2
3pub mod appearance;
4pub mod args;
5pub mod array;
6pub mod assert;
7pub mod axis_or_reference;
8pub mod chamfer;
9pub mod clone;
10pub mod csg;
11pub mod edge;
12pub mod extrude;
13pub mod fillet;
14pub mod helix;
15pub mod loft;
16pub mod math;
17pub mod mirror;
18pub mod patterns;
19pub mod planes;
20pub mod revolve;
21pub mod segment;
22pub mod shapes;
23pub mod shell;
24pub mod sketch;
25pub mod sweep;
26pub mod transform;
27pub mod utils;
28
29use anyhow::Result;
30pub use args::Args;
31use indexmap::IndexMap;
32use lazy_static::lazy_static;
33
34use crate::{
35    docs::StdLibFn,
36    errors::KclError,
37    execution::{types::PrimitiveType, ExecState, KclValue},
38    parsing::ast::types::Name,
39};
40
41pub type StdFn = fn(
42    &mut ExecState,
43    Args,
44) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<KclValue, KclError>> + Send + '_>>;
45
46lazy_static! {
47    static ref CORE_FNS: Vec<Box<dyn StdLibFn>> = vec![
48        Box::new(crate::std::appearance::Appearance),
49        Box::new(crate::std::extrude::Extrude),
50        Box::new(crate::std::segment::SegEnd),
51        Box::new(crate::std::segment::SegEndX),
52        Box::new(crate::std::segment::SegEndY),
53        Box::new(crate::std::segment::SegStart),
54        Box::new(crate::std::segment::SegStartX),
55        Box::new(crate::std::segment::SegStartY),
56        Box::new(crate::std::segment::LastSegX),
57        Box::new(crate::std::segment::LastSegY),
58        Box::new(crate::std::segment::SegLen),
59        Box::new(crate::std::segment::SegAng),
60        Box::new(crate::std::segment::TangentToEnd),
61        Box::new(crate::std::shapes::CircleThreePoint),
62        Box::new(crate::std::shapes::Polygon),
63        Box::new(crate::std::sketch::InvoluteCircular),
64        Box::new(crate::std::sketch::Line),
65        Box::new(crate::std::sketch::XLine),
66        Box::new(crate::std::sketch::YLine),
67        Box::new(crate::std::sketch::AngledLine),
68        Box::new(crate::std::sketch::AngledLineThatIntersects),
69        Box::new(crate::std::sketch::StartSketchOn),
70        Box::new(crate::std::sketch::StartProfile),
71        Box::new(crate::std::sketch::ProfileStartX),
72        Box::new(crate::std::sketch::ProfileStartY),
73        Box::new(crate::std::sketch::ProfileStart),
74        Box::new(crate::std::sketch::Close),
75        Box::new(crate::std::sketch::Arc),
76        Box::new(crate::std::sketch::TangentialArc),
77        Box::new(crate::std::sketch::BezierCurve),
78        Box::new(crate::std::sketch::Subtract2D),
79        Box::new(crate::std::patterns::PatternLinear2D),
80        Box::new(crate::std::patterns::PatternLinear3D),
81        Box::new(crate::std::patterns::PatternCircular2D),
82        Box::new(crate::std::patterns::PatternCircular3D),
83        Box::new(crate::std::patterns::PatternTransform),
84        Box::new(crate::std::patterns::PatternTransform2D),
85        Box::new(crate::std::edge::GetOppositeEdge),
86        Box::new(crate::std::edge::GetNextAdjacentEdge),
87        Box::new(crate::std::edge::GetPreviousAdjacentEdge),
88        Box::new(crate::std::edge::GetCommonEdge),
89        Box::new(crate::std::sweep::Sweep),
90        Box::new(crate::std::loft::Loft),
91        Box::new(crate::std::assert::Assert),
92        Box::new(crate::std::assert::AssertIs),
93        Box::new(crate::std::transform::Scale),
94        Box::new(crate::std::transform::Translate),
95        Box::new(crate::std::transform::Rotate),
96        Box::new(crate::std::csg::Intersect),
97        Box::new(crate::std::csg::Union),
98        Box::new(crate::std::csg::Subtract),
99    ];
100}
101
102pub fn name_in_stdlib(name: &str) -> bool {
103    CORE_FNS.iter().any(|f| f.name() == name)
104}
105
106pub fn get_stdlib_fn(name: &str) -> Option<Box<dyn StdLibFn>> {
107    CORE_FNS.iter().find(|f| f.name() == name).cloned()
108}
109
110#[derive(Clone, Debug, PartialEq, Eq)]
111pub struct StdFnProps {
112    pub name: String,
113    pub deprecated: bool,
114    pub include_in_feature_tree: bool,
115}
116
117impl StdFnProps {
118    fn default(name: &str) -> Self {
119        Self {
120            name: name.to_owned(),
121            deprecated: false,
122            include_in_feature_tree: false,
123        }
124    }
125
126    fn include_in_feature_tree(mut self) -> Self {
127        self.include_in_feature_tree = true;
128        self
129    }
130}
131
132pub(crate) fn std_fn(path: &str, fn_name: &str) -> (crate::std::StdFn, StdFnProps) {
133    match (path, fn_name) {
134        ("math", "cos") => (
135            |e, a| Box::pin(crate::std::math::cos(e, a)),
136            StdFnProps::default("std::math::cos"),
137        ),
138        ("math", "sin") => (
139            |e, a| Box::pin(crate::std::math::sin(e, a)),
140            StdFnProps::default("std::math::sin"),
141        ),
142        ("math", "tan") => (
143            |e, a| Box::pin(crate::std::math::tan(e, a)),
144            StdFnProps::default("std::math::tan"),
145        ),
146        ("math", "acos") => (
147            |e, a| Box::pin(crate::std::math::acos(e, a)),
148            StdFnProps::default("std::math::acos"),
149        ),
150        ("math", "asin") => (
151            |e, a| Box::pin(crate::std::math::asin(e, a)),
152            StdFnProps::default("std::math::asin"),
153        ),
154        ("math", "atan") => (
155            |e, a| Box::pin(crate::std::math::atan(e, a)),
156            StdFnProps::default("std::math::atan"),
157        ),
158        ("math", "atan2") => (
159            |e, a| Box::pin(crate::std::math::atan2(e, a)),
160            StdFnProps::default("std::math::atan2"),
161        ),
162        ("math", "sqrt") => (
163            |e, a| Box::pin(crate::std::math::sqrt(e, a)),
164            StdFnProps::default("std::math::sqrt"),
165        ),
166
167        ("math", "abs") => (
168            |e, a| Box::pin(crate::std::math::abs(e, a)),
169            StdFnProps::default("std::math::abs"),
170        ),
171        ("math", "rem") => (
172            |e, a| Box::pin(crate::std::math::rem(e, a)),
173            StdFnProps::default("std::math::rem"),
174        ),
175        ("math", "round") => (
176            |e, a| Box::pin(crate::std::math::round(e, a)),
177            StdFnProps::default("std::math::round"),
178        ),
179        ("math", "floor") => (
180            |e, a| Box::pin(crate::std::math::floor(e, a)),
181            StdFnProps::default("std::math::floor"),
182        ),
183        ("math", "ceil") => (
184            |e, a| Box::pin(crate::std::math::ceil(e, a)),
185            StdFnProps::default("std::math::ceil"),
186        ),
187        ("math", "min") => (
188            |e, a| Box::pin(crate::std::math::min(e, a)),
189            StdFnProps::default("std::math::min"),
190        ),
191        ("math", "max") => (
192            |e, a| Box::pin(crate::std::math::max(e, a)),
193            StdFnProps::default("std::math::max"),
194        ),
195        ("math", "pow") => (
196            |e, a| Box::pin(crate::std::math::pow(e, a)),
197            StdFnProps::default("std::math::pow"),
198        ),
199        ("math", "log") => (
200            |e, a| Box::pin(crate::std::math::log(e, a)),
201            StdFnProps::default("std::math::log"),
202        ),
203        ("math", "log2") => (
204            |e, a| Box::pin(crate::std::math::log2(e, a)),
205            StdFnProps::default("std::math::log2"),
206        ),
207        ("math", "log10") => (
208            |e, a| Box::pin(crate::std::math::log10(e, a)),
209            StdFnProps::default("std::math::log10"),
210        ),
211        ("math", "ln") => (
212            |e, a| Box::pin(crate::std::math::ln(e, a)),
213            StdFnProps::default("std::math::ln"),
214        ),
215        ("math", "legLen") => (
216            |e, a| Box::pin(crate::std::math::leg_length(e, a)),
217            StdFnProps::default("std::math::legLen"),
218        ),
219        ("math", "legAngX") => (
220            |e, a| Box::pin(crate::std::math::leg_angle_x(e, a)),
221            StdFnProps::default("std::math::legAngX"),
222        ),
223        ("math", "legAngY") => (
224            |e, a| Box::pin(crate::std::math::leg_angle_y(e, a)),
225            StdFnProps::default("std::math::legAngY"),
226        ),
227        ("sketch", "circle") => (
228            |e, a| Box::pin(crate::std::shapes::circle(e, a)),
229            StdFnProps::default("std::sketch::circle"),
230        ),
231        ("prelude", "helix") => (
232            |e, a| Box::pin(crate::std::helix::helix(e, a)),
233            StdFnProps::default("std::helix").include_in_feature_tree(),
234        ),
235        ("transform", "mirror2d") => (
236            |e, a| Box::pin(crate::std::mirror::mirror_2d(e, a)),
237            StdFnProps::default("std::transform::mirror2d"),
238        ),
239        ("sketch", "revolve") => (
240            |e, a| Box::pin(crate::std::revolve::revolve(e, a)),
241            StdFnProps::default("std::sketch::revolve").include_in_feature_tree(),
242        ),
243        ("prelude", "offsetPlane") => (
244            |e, a| Box::pin(crate::std::planes::offset_plane(e, a)),
245            StdFnProps::default("std::offsetPlane").include_in_feature_tree(),
246        ),
247        ("solid", "fillet") => (
248            |e, a| Box::pin(crate::std::fillet::fillet(e, a)),
249            StdFnProps::default("std::solid::fillet").include_in_feature_tree(),
250        ),
251        ("solid", "chamfer") => (
252            |e, a| Box::pin(crate::std::chamfer::chamfer(e, a)),
253            StdFnProps::default("std::solid::chamfer").include_in_feature_tree(),
254        ),
255        ("solid", "shell") => (
256            |e, a| Box::pin(crate::std::shell::shell(e, a)),
257            StdFnProps::default("std::solid::shell").include_in_feature_tree(),
258        ),
259        ("solid", "hollow") => (
260            |e, a| Box::pin(crate::std::shell::hollow(e, a)),
261            StdFnProps::default("std::solid::hollow").include_in_feature_tree(),
262        ),
263        ("array", "map") => (
264            |e, a| Box::pin(crate::std::array::map(e, a)),
265            StdFnProps::default("std::array::map"),
266        ),
267        ("array", "reduce") => (
268            |e, a| Box::pin(crate::std::array::reduce(e, a)),
269            StdFnProps::default("std::array::reduce"),
270        ),
271        ("array", "push") => (
272            |e, a| Box::pin(crate::std::array::push(e, a)),
273            StdFnProps::default("std::array::push"),
274        ),
275        ("array", "pop") => (
276            |e, a| Box::pin(crate::std::array::pop(e, a)),
277            StdFnProps::default("std::array::pop"),
278        ),
279        ("prelude", "clone") => (
280            |e, a| Box::pin(crate::std::clone::clone(e, a)),
281            StdFnProps::default("std::clone").include_in_feature_tree(),
282        ),
283        _ => unreachable!(),
284    }
285}
286
287pub(crate) fn std_ty(path: &str, fn_name: &str) -> (PrimitiveType, StdFnProps) {
288    match (path, fn_name) {
289        ("types", "Sketch") => (PrimitiveType::Sketch, StdFnProps::default("std::types::Sketch")),
290        ("types", "Solid") => (PrimitiveType::Solid, StdFnProps::default("std::types::Solid")),
291        ("types", "Plane") => (PrimitiveType::Plane, StdFnProps::default("std::types::Plane")),
292        ("types", "Face") => (PrimitiveType::Face, StdFnProps::default("std::types::Face")),
293        ("types", "Helix") => (PrimitiveType::Helix, StdFnProps::default("std::types::Helix")),
294        ("types", "Edge") => (PrimitiveType::Edge, StdFnProps::default("std::types::Edge")),
295        ("types", "Axis2d") => (PrimitiveType::Axis2d, StdFnProps::default("std::types::Axis2d")),
296        ("types", "Axis3d") => (PrimitiveType::Axis3d, StdFnProps::default("std::types::Axis3d")),
297        _ => unreachable!(),
298    }
299}
300
301pub struct StdLib {
302    pub fns: IndexMap<String, Box<dyn StdLibFn>>,
303}
304
305impl std::fmt::Debug for StdLib {
306    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
307        f.debug_struct("StdLib").field("fns.len()", &self.fns.len()).finish()
308    }
309}
310
311impl StdLib {
312    pub fn new() -> Self {
313        let fns = CORE_FNS
314            .clone()
315            .into_iter()
316            .map(|internal_fn| (internal_fn.name(), internal_fn))
317            .collect();
318
319        Self { fns }
320    }
321
322    // Get the combined hashmaps.
323    pub fn combined(&self) -> IndexMap<String, Box<dyn StdLibFn>> {
324        self.fns.clone()
325    }
326
327    pub fn get(&self, name: &str) -> Option<Box<dyn StdLibFn>> {
328        self.fns.get(name).cloned()
329    }
330
331    pub fn get_rust_function(&self, name: &Name) -> Option<Box<dyn StdLibFn>> {
332        if let Some(name) = name.local_ident() {
333            if let Some(f) = self.get(name.inner) {
334                return Some(f);
335            }
336        }
337
338        None
339    }
340
341    pub fn contains_key(&self, key: &str) -> bool {
342        self.fns.contains_key(key)
343    }
344}
345
346impl Default for StdLib {
347    fn default() -> Self {
348        Self::new()
349    }
350}
351
352/// The default tolerance for modeling commands in [`kittycad_modeling_cmds::length_unit::LengthUnit`].
353const DEFAULT_TOLERANCE: f64 = 0.0000001;