Review folllow ups (#5444)

* Remove StandardPlane docs

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Add a struct for encapsualting an f64 and a type

Signed-off-by: Nick Cameron <nrc@ncameron.org>

---------

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-02-21 12:36:21 +13:00
committed by GitHub
parent 59b1c414f0
commit 96b93f8d51
11 changed files with 108 additions and 131 deletions

View File

@ -3,6 +3,8 @@ use std::{any::type_name, collections::HashMap, num::NonZeroU32};
use anyhow::Result;
use kcmc::{websocket::OkWebSocketResponseData, ModelingCmd};
use kittycad_modeling_cmds as kcmc;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::shapes::PolygonType;
use crate::{
@ -61,6 +63,32 @@ impl KwArgs {
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct TyF64 {
pub n: f64,
pub ty: NumericType,
}
impl TyF64 {
pub fn new(n: f64, ty: NumericType) -> Self {
Self { n, ty }
}
pub fn count(n: f64) -> Self {
Self {
n,
ty: NumericType::count(),
}
}
pub fn map(mut self, n: f64) -> Self {
self.n = n;
self
}
}
#[derive(Debug, Clone)]
pub struct Args {
/// Positional args.
@ -321,17 +349,17 @@ impl Args {
)
}
pub(crate) fn make_user_val_from_f64_with_type(&self, f: f64, ty: NumericType) -> KclValue {
pub(crate) fn make_user_val_from_f64_with_type(&self, f: TyF64) -> KclValue {
KclValue::from_number_with_type(
f,
ty,
f.n,
f.ty,
vec![Metadata {
source_range: self.source_range,
}],
)
}
pub(crate) fn make_user_val_from_f64_array(&self, f: Vec<f64>, ty: NumericType) -> Result<KclValue, KclError> {
pub(crate) fn make_user_val_from_f64_array(&self, f: Vec<f64>, ty: &NumericType) -> Result<KclValue, KclError> {
let array = f
.into_iter()
.map(|n| KclValue::Number {
@ -354,7 +382,7 @@ impl Args {
FromArgs::from_args(self, 0)
}
pub(crate) fn get_number_with_type(&self) -> Result<(f64, NumericType), KclError> {
pub(crate) fn get_number_with_type(&self) -> Result<TyF64, KclError> {
FromArgs::from_args(self, 0)
}
@ -375,12 +403,12 @@ impl Args {
Ok(numbers)
}
pub(crate) fn get_number_array_with_types(&self) -> Result<Vec<(f64, NumericType)>, KclError> {
pub(crate) fn get_number_array_with_types(&self) -> Result<Vec<TyF64>, KclError> {
let numbers = self
.args
.iter()
.map(|arg| {
let Some(num) = <(f64, NumericType)>::from_kcl_val(&arg.value) else {
let Some(num) = <TyF64>::from_kcl_val(&arg.value) else {
return Err(KclError::Semantic(KclErrorDetails {
source_ranges: arg.source_ranges(),
message: format!("Expected a number but found {}", arg.value.human_friendly_type()),
@ -403,10 +431,10 @@ impl Args {
}
let mut numbers = numbers.into_iter();
let (a, ta) = numbers.next().unwrap();
let (b, tb) = numbers.next().unwrap();
let ty = ta.combine_eq(&tb);
Ok((a, b, ty))
let a = numbers.next().unwrap();
let b = numbers.next().unwrap();
let ty = a.ty.combine_eq(&b.ty);
Ok((a.n, b.n, ty))
}
pub(crate) fn get_circle_args(
@ -1470,10 +1498,10 @@ impl<'a> FromKclValue<'a> for f64 {
}
}
}
impl<'a> FromKclValue<'a> for (f64, NumericType) {
impl<'a> FromKclValue<'a> for TyF64 {
fn from_kcl_val(arg: &'a KclValue) -> Option<Self> {
match arg {
KclValue::Number { value, ty, .. } => Some((*value, ty.clone())),
KclValue::Number { value, ty, .. } => Some(TyF64::new(*value, ty.clone())),
_ => None,
}
}

View File

@ -5,7 +5,7 @@ use derive_docs::stdlib;
use crate::{
errors::{KclError, KclErrorDetails},
execution::{kcl_value::NumericType, ExecState, KclValue},
execution::{ExecState, KclValue},
std::Args,
};
@ -24,7 +24,7 @@ async fn _assert(value: bool, message: &str, args: &Args) -> Result<(), KclError
pub async fn assert(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (data, description): (bool, String) = args.get_data()?;
inner_assert(data, &description, &args).await?;
Ok(args.make_user_val_from_f64_with_type(0.0, NumericType::count())) // TODO: Add a new Void enum for fns that don't return anything.
Ok(KclValue::none())
}
/// Check a value at runtime, and raise an error if the argument provided
@ -44,7 +44,7 @@ async fn inner_assert(data: bool, message: &str, args: &Args) -> Result<(), KclE
pub async fn assert_lt(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (left, right, description): (f64, f64, String) = args.get_data()?;
inner_assert_lt(left, right, &description, &args).await?;
Ok(args.make_user_val_from_f64_with_type(0.0, NumericType::count())) // TODO: Add a new Void enum for fns that don't return anything.
Ok(KclValue::none())
}
/// Check that a numerical value is less than to another at runtime,
@ -63,7 +63,7 @@ async fn inner_assert_lt(left: f64, right: f64, message: &str, args: &Args) -> R
pub async fn assert_gt(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (left, right, description): (f64, f64, String) = args.get_data()?;
inner_assert_gt(left, right, &description, &args).await?;
Ok(args.make_user_val_from_f64_with_type(0.0, NumericType::count())) // TODO: Add a new Void enum for fns that don't return anything.
Ok(KclValue::none())
}
/// Check that a numerical value equals another at runtime,
@ -96,7 +96,7 @@ async fn inner_assert_equal(left: f64, right: f64, epsilon: f64, message: &str,
pub async fn assert_equal(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (left, right, epsilon, description): (f64, f64, f64, String) = args.get_data()?;
inner_assert_equal(left, right, epsilon, &description, &args).await?;
Ok(args.make_user_val_from_f64_with_type(0.0, NumericType::count())) // TODO: Add a new Void enum for fns that don't return anything.
Ok(KclValue::none())
}
/// Check that a numerical value is greater than another at runtime,
@ -115,7 +115,7 @@ async fn inner_assert_gt(left: f64, right: f64, message: &str, args: &Args) -> R
pub async fn assert_lte(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (left, right, description): (f64, f64, String) = args.get_data()?;
inner_assert_lte(left, right, &description, &args).await?;
Ok(args.make_user_val_from_f64_with_type(0.0, NumericType::count())) // TODO: Add a new Void enum for fns that don't return anything.
Ok(KclValue::none())
}
/// Check that a numerical value is less than or equal to another at runtime,
@ -135,7 +135,7 @@ async fn inner_assert_lte(left: f64, right: f64, message: &str, args: &Args) ->
pub async fn assert_gte(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (left, right, description): (f64, f64, String) = args.get_data()?;
inner_assert_gte(left, right, &description, &args).await?;
Ok(args.make_user_val_from_f64_with_type(0.0, NumericType::count())) // TODO: Add a new Void enum for fns that don't return anything.
Ok(KclValue::none())
}
/// Check that a numerical value is greater than or equal to another at runtime,

View File

@ -10,10 +10,10 @@ use crate::{
/// Converts a number to integer.
pub async fn int(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let (num, ty) = args.get_number_with_type()?;
let converted = inner_int(num)?;
let num = args.get_number_with_type()?;
let converted = inner_int(num.n)?;
Ok(args.make_user_val_from_f64_with_type(converted, ty))
Ok(args.make_user_val_from_f64_with_type(num.map(converted)))
}
/// Convert a number to an integer.

View File

@ -6,8 +6,8 @@ use derive_docs::stdlib;
use super::args::FromArgs;
use crate::{
errors::{KclError, KclErrorDetails},
execution::{kcl_value::NumericType, ExecState, KclValue},
std::Args,
execution::{ExecState, KclValue},
std::args::{Args, TyF64},
};
/// Compute the remainder after dividing `num` by `div`.
@ -48,19 +48,19 @@ fn inner_rem(num: f64, divisor: f64) -> f64 {
/// Compute the cosine of a number (in radians).
pub async fn cos(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let num = args.get_number()?;
Ok(args.make_user_val_from_f64_with_type(num.cos(), NumericType::count()))
Ok(args.make_user_val_from_f64_with_type(TyF64::count(num.cos())))
}
/// Compute the sine of a number (in radians).
pub async fn sin(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let num = args.get_number()?;
Ok(args.make_user_val_from_f64_with_type(num.sin(), NumericType::count()))
Ok(args.make_user_val_from_f64_with_type(TyF64::count(num.sin())))
}
/// Compute the tangent of a number (in radians).
pub async fn tan(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let num = args.get_number()?;
Ok(args.make_user_val_from_f64_with_type(num.tan(), NumericType::count()))
Ok(args.make_user_val_from_f64_with_type(TyF64::count(num.tan())))
}
/// Return the value of `pi`. Archimedes constant (π).

View File

@ -14,7 +14,6 @@ use super::sketch::PlaneData;
/// Offset a plane by a distance along its normal.
pub async fn offset_plane(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
// let (std_plane, offset): (StandardPlane, f64) = args.get_data_and_float()?;
let std_plane = args.get_unlabeled_kw_arg("plane")?;
let offset = args.get_kw_arg("offset")?;
let plane = inner_offset_plane(std_plane, offset, exec_state).await?;

View File

@ -7,8 +7,8 @@ use serde::{Deserialize, Serialize};
use crate::{
errors::KclError,
execution::{kcl_value::NumericType, ExecState, KclValue},
std::Args,
execution::{ExecState, KclValue},
std::args::{Args, TyF64},
};
/// Data for polar coordinates.
@ -19,7 +19,7 @@ pub struct PolarCoordsData {
/// The angle of the line (in degrees).
pub angle: f64,
/// The length of the line.
pub length: (f64, NumericType),
pub length: TyF64,
}
/// Convert from polar/sphere coordinates to cartesian coordinates.
@ -27,7 +27,7 @@ pub async fn polar(_exec_state: &mut ExecState, args: Args) -> Result<KclValue,
let data: PolarCoordsData = args.get_data()?;
let result = inner_polar(&data)?;
args.make_user_val_from_f64_array(result.to_vec(), data.length.1)
args.make_user_val_from_f64_array(result.to_vec(), &data.length.ty)
}
/// Convert polar/sphere (azimuth, elevation, distance) coordinates to
@ -49,7 +49,7 @@ pub async fn polar(_exec_state: &mut ExecState, args: Args) -> Result<KclValue,
}]
fn inner_polar(data: &PolarCoordsData) -> Result<[f64; 2], KclError> {
let angle = data.angle.to_radians();
let x = data.length.0 * angle.cos();
let y = data.length.0 * angle.sin();
let x = data.length.n * angle.cos();
let y = data.length.n * angle.sin();
Ok([x, y])
}

View File

@ -19,11 +19,11 @@ use crate::{
},
parsing::ast::types::TagNode,
std::{
args::{Args, TyF64},
utils::{
arc_angles, arc_center_and_end, calculate_circle_center, get_tangential_arc_to_info, get_x_component,
get_y_component, intersection_with_parallel_line, TangentialArcInfoInput,
},
Args,
},
};
@ -1406,7 +1406,7 @@ pub async fn profile_start_x(_exec_state: &mut ExecState, args: Args) -> Result<
let sketch: Sketch = args.get_sketch()?;
let ty = sketch.units.into();
let x = inner_profile_start_x(sketch)?;
Ok(args.make_user_val_from_f64_with_type(x, ty))
Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
}
/// Extract the provided 2-dimensional sketch's profile's origin's 'x'
@ -1431,7 +1431,7 @@ pub async fn profile_start_y(_exec_state: &mut ExecState, args: Args) -> Result<
let sketch: Sketch = args.get_sketch()?;
let ty = sketch.units.into();
let x = inner_profile_start_y(sketch)?;
Ok(args.make_user_val_from_f64_with_type(x, ty))
Ok(args.make_user_val_from_f64_with_type(TyF64::new(x, ty)))
}
/// Extract the provided 2-dimensional sketch's profile's origin's 'y'