Move the wasm lib, and cleanup rust directory and all references (#5585)
* git mv src/wasm-lib rust Signed-off-by: Jess Frazelle <github@jessfraz.com> * mv wasm-lib to workspace Signed-off-by: Jess Frazelle <github@jessfraz.com> * mv kcl-lib Signed-off-by: Jess Frazelle <github@jessfraz.com> * mv derive docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * resolve file paths Signed-off-by: Jess Frazelle <github@jessfraz.com> * clippy Signed-off-by: Jess Frazelle <github@jessfraz.com> * move more shit Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix more paths Signed-off-by: Jess Frazelle <github@jessfraz.com> * make yarn build:wasm work Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix scripts Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixups Signed-off-by: Jess Frazelle <github@jessfraz.com> * better references Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix cargo ci Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix reference Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix more ci Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix tests Signed-off-by: Jess Frazelle <github@jessfraz.com> * cargo sort Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix script Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * fmt Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix a dep Signed-off-by: Jess Frazelle <github@jessfraz.com> * sort Signed-off-by: Jess Frazelle <github@jessfraz.com> * remove unused deps Signed-off-by: Jess Frazelle <github@jessfraz.com> * Revert "remove unused deps" This reverts commit fbabdb062e275fd5cbc1476f8480a1afee15d972. * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * deps; Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
153
rust/kcl-lib/src/std/assert.rs
Normal file
153
rust/kcl-lib/src/std/assert.rs
Normal file
@ -0,0 +1,153 @@
|
||||
//! Standard library assert functions.
|
||||
|
||||
use anyhow::Result;
|
||||
use kcl_derive_docs::stdlib;
|
||||
|
||||
use crate::{
|
||||
errors::{KclError, KclErrorDetails},
|
||||
execution::{ExecState, KclValue},
|
||||
std::Args,
|
||||
};
|
||||
|
||||
async fn _assert(value: bool, message: &str, args: &Args) -> Result<(), KclError> {
|
||||
if !value {
|
||||
return Err(KclError::Type(KclErrorDetails {
|
||||
message: format!("assert failed: {}", message),
|
||||
source_ranges: vec![args.source_range],
|
||||
}));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check that the provided value is true, or raise a [KclError]
|
||||
/// with the provided description.
|
||||
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(KclValue::none())
|
||||
}
|
||||
|
||||
/// Check a value at runtime, and raise an error if the argument provided
|
||||
/// is false.
|
||||
///
|
||||
/// ```no_run
|
||||
/// myVar = true
|
||||
/// assert(myVar, "should always be true")
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "assert",
|
||||
}]
|
||||
async fn inner_assert(data: bool, message: &str, args: &Args) -> Result<(), KclError> {
|
||||
_assert(data, message, args).await
|
||||
}
|
||||
|
||||
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(KclValue::none())
|
||||
}
|
||||
|
||||
/// Check that a numerical value is less than to another at runtime,
|
||||
/// otherwise raise an error.
|
||||
///
|
||||
/// ```no_run
|
||||
/// assertLessThan(1, 2, "1 is less than 2")
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "assertLessThan",
|
||||
}]
|
||||
async fn inner_assert_lt(left: f64, right: f64, message: &str, args: &Args) -> Result<(), KclError> {
|
||||
_assert(left < right, message, args).await
|
||||
}
|
||||
|
||||
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(KclValue::none())
|
||||
}
|
||||
|
||||
/// Check that a numerical value equals another at runtime,
|
||||
/// otherwise raise an error.
|
||||
///
|
||||
/// ```no_run
|
||||
/// n = 1.0285
|
||||
/// o = 1.0286
|
||||
/// assertEqual(n, o, 0.01, "n is within the given tolerance for o")
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "assertEqual",
|
||||
}]
|
||||
async fn inner_assert_equal(left: f64, right: f64, epsilon: f64, message: &str, args: &Args) -> Result<(), KclError> {
|
||||
if epsilon <= 0.0 {
|
||||
Err(KclError::Type(KclErrorDetails {
|
||||
message: "assertEqual epsilon must be greater than zero".to_owned(),
|
||||
source_ranges: vec![args.source_range],
|
||||
}))
|
||||
} else if (right - left).abs() < epsilon {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(KclError::Type(KclErrorDetails {
|
||||
message: format!("assert failed because {left} != {right}: {message}"),
|
||||
source_ranges: vec![args.source_range],
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
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(KclValue::none())
|
||||
}
|
||||
|
||||
/// Check that a numerical value is greater than another at runtime,
|
||||
/// otherwise raise an error.
|
||||
///
|
||||
/// ```no_run
|
||||
/// assertGreaterThan(2, 1, "2 is greater than 1")
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "assertGreaterThan",
|
||||
}]
|
||||
async fn inner_assert_gt(left: f64, right: f64, message: &str, args: &Args) -> Result<(), KclError> {
|
||||
_assert(left > right, message, args).await
|
||||
}
|
||||
|
||||
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(KclValue::none())
|
||||
}
|
||||
|
||||
/// Check that a numerical value is less than or equal to another at runtime,
|
||||
/// otherwise raise an error.
|
||||
///
|
||||
/// ```no_run
|
||||
/// assertLessThanOrEq(1, 2, "1 is less than 2")
|
||||
/// assertLessThanOrEq(1, 1, "1 is equal to 1")
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "assertLessThanOrEq",
|
||||
}]
|
||||
async fn inner_assert_lte(left: f64, right: f64, message: &str, args: &Args) -> Result<(), KclError> {
|
||||
_assert(left <= right, message, args).await
|
||||
}
|
||||
|
||||
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(KclValue::none())
|
||||
}
|
||||
|
||||
/// Check that a numerical value is greater than or equal to another at runtime,
|
||||
/// otherwise raise an error.
|
||||
///
|
||||
/// ```no_run
|
||||
/// assertGreaterThanOrEq(2, 1, "2 is greater than 1")
|
||||
/// assertGreaterThanOrEq(1, 1, "1 is equal to 1")
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "assertGreaterThanOrEq",
|
||||
}]
|
||||
async fn inner_assert_gte(left: f64, right: f64, message: &str, args: &Args) -> Result<(), KclError> {
|
||||
_assert(left >= right, message, args).await
|
||||
}
|
Reference in New Issue
Block a user