kcl_lib/std/
convert.rs

1//! Conversions between types.
2
3use kcl_derive_docs::stdlib;
4
5use crate::{
6    errors::KclError,
7    execution::{ExecState, KclValue},
8    std::Args,
9};
10
11/// Converts a number to integer.
12pub async fn int(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
13    let num = args.get_number_with_type()?;
14    let converted = inner_int(num.n)?;
15
16    Ok(args.make_user_val_from_f64_with_type(num.map(converted)))
17}
18
19/// Convert a number to an integer.
20///
21/// DEPRECATED use floor(), ceil(), or round().
22///
23/// ```no_run
24/// n = int(ceil(5/2))
25/// assertEqual(n, 3, 0.0001, "5/2 = 2.5, rounded up makes 3")
26/// // Draw n cylinders.
27/// startSketchOn('XZ')
28///   |> circle(center = [0, 0], radius = 2 )
29///   |> extrude(length = 5)
30///   |> patternTransform(instances = n, transform = fn(id) {
31///   return { translate = [4 * id, 0, 0] }
32/// })
33/// ```
34#[stdlib {
35    name = "int",
36    tags = ["convert"],
37    deprecated = true,
38}]
39fn inner_int(num: f64) -> Result<f64, KclError> {
40    Ok(num)
41}