2023-08-24 15:34:51 -07:00
|
|
|
//! Functions implemented for language execution.
|
|
|
|
|
2024-07-16 07:45:43 -05:00
|
|
|
pub mod args;
|
2024-07-26 15:14:51 -04:00
|
|
|
pub mod assert;
|
2024-06-17 12:13:19 -07:00
|
|
|
pub mod chamfer;
|
2024-07-25 21:18:52 -04:00
|
|
|
pub mod convert;
|
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-07-28 22:45:40 -07:00
|
|
|
pub mod polar;
|
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-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;
|
2024-07-29 13:18:55 -07:00
|
|
|
pub use args::Args;
|
2023-08-25 13:41:04 -07:00
|
|
|
use derive_docs::stdlib;
|
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-07-16 07:45:43 -05:00
|
|
|
ast::types::FunctionExpression,
|
2023-11-08 20:23:59 -06:00
|
|
|
docs::StdLibFn,
|
2024-07-16 07:45:43 -05:00
|
|
|
errors::KclError,
|
2024-07-22 19:43:40 -04:00
|
|
|
executor::{MemoryItem, ProgramMemory, SketchGroup, SketchSurface},
|
2024-07-16 07:45:43 -05:00
|
|
|
std::kcl_stdlib::KclStdLibFn,
|
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),
|
2024-07-25 21:18:52 -04:00
|
|
|
Box::new(crate::std::convert::Int),
|
2023-11-08 20:23:59 -06:00
|
|
|
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-27 22:20:51 -05:00
|
|
|
Box::new(crate::std::patterns::PatternTransform),
|
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),
|
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),
|
2024-07-28 22:45:40 -07:00
|
|
|
Box::new(crate::std::polar::Polar),
|
2024-07-26 15:14:51 -04:00
|
|
|
Box::new(crate::std::assert::Assert),
|
2024-08-05 11:31:58 -05:00
|
|
|
Box::new(crate::std::assert::AssertEqual),
|
2024-07-26 15:14:51 -04:00
|
|
|
Box::new(crate::std::assert::AssertLessThan),
|
|
|
|
Box::new(crate::std::assert::AssertGreaterThan),
|
|
|
|
Box::new(crate::std::assert::AssertLessThanOrEq),
|
|
|
|
Box::new(crate::std::assert::AssertGreaterThanOrEq),
|
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-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,
|
|
|
|
}
|
|
|
|
|
2024-06-27 22:20:51 -05:00
|
|
|
pub struct FnAsArg<'a> {
|
|
|
|
pub func: &'a crate::executor::MemoryFunction,
|
|
|
|
pub expr: Box<FunctionExpression>,
|
2024-07-22 19:43:40 -04:00
|
|
|
pub memory: Box<ProgramMemory>,
|
2024-06-27 22:20:51 -05:00
|
|
|
}
|
|
|
|
|
2023-08-25 13:41:04 -07:00
|
|
|
#[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");
|
2024-07-26 21:15:54 -07:00
|
|
|
} else if fn_name.contains("_greater_than_or_eq") {
|
|
|
|
fn_name = fn_name.replace("_greater_than_or_eq", "_gte");
|
|
|
|
} else if fn_name.contains("_less_than_or_eq") {
|
|
|
|
fn_name = fn_name.replace("_less_than_or_eq", "_lte");
|
|
|
|
} else if fn_name.contains("_greater_than") {
|
|
|
|
fn_name = fn_name.replace("_greater_than", "_gt");
|
|
|
|
} else if fn_name.contains("_less_than") {
|
|
|
|
fn_name = fn_name.replace("_less_than", "_lt");
|
2024-03-26 21:28:50 -07:00
|
|
|
} 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(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|