use anyhow::Result;
use derive_docs::stdlib;
use schemars::JsonSchema;
use crate::{
errors::{KclError, KclErrorDetails},
executor::MemoryItem,
std::Args,
};
pub async fn cos(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_cos(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "cos",
}]
fn inner_cos(num: f64) -> Result<f64, KclError> {
Ok(num.cos())
}
pub async fn sin(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_sin(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "sin",
}]
fn inner_sin(num: f64) -> Result<f64, KclError> {
Ok(num.sin())
}
pub async fn tan(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_tan(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "tan",
}]
fn inner_tan(num: f64) -> Result<f64, KclError> {
Ok(num.tan())
}
pub async fn pi(args: Args) -> Result<MemoryItem, KclError> {
let result = inner_pi()?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "pi",
}]
fn inner_pi() -> Result<f64, KclError> {
Ok(std::f64::consts::PI)
}
pub async fn sqrt(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_sqrt(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "sqrt",
}]
fn inner_sqrt(num: f64) -> Result<f64, KclError> {
Ok(num.sqrt())
}
pub async fn abs(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_abs(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "abs",
}]
fn inner_abs(num: f64) -> Result<f64, KclError> {
Ok(num.abs())
}
pub async fn floor(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_floor(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "floor",
}]
fn inner_floor(num: f64) -> Result<f64, KclError> {
Ok(num.floor())
}
pub async fn ceil(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_ceil(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "ceil",
}]
fn inner_ceil(num: f64) -> Result<f64, KclError> {
Ok(num.ceil())
}
pub async fn min(args: Args) -> Result<MemoryItem, KclError> {
let nums = args.get_number_array()?;
let result = inner_min(nums);
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "min",
}]
fn inner_min(args: Vec<f64>) -> f64 {
let mut min = std::f64::MAX;
for arg in args.iter() {
if *arg < min {
min = *arg;
}
}
min
}
pub async fn max(args: Args) -> Result<MemoryItem, KclError> {
let nums = args.get_number_array()?;
let result = inner_max(nums);
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "max",
}]
fn inner_max(args: Vec<f64>) -> f64 {
let mut max = std::f64::MAX;
for arg in args.iter() {
if *arg > max {
max = *arg;
}
}
max
}
pub async fn pow(args: Args) -> Result<MemoryItem, KclError> {
let nums = args.get_number_array()?;
if nums.len() > 2 {
return Err(KclError::Type(KclErrorDetails {
message: format!("expected 2 arguments, got {}", nums.len()),
source_ranges: vec![args.source_range],
}));
}
if nums.len() <= 1 {
return Err(KclError::Type(KclErrorDetails {
message: format!("expected 2 arguments, got {}", nums.len()),
source_ranges: vec![args.source_range],
}));
}
let result = inner_pow(nums[0], nums[1])?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "pow",
}]
fn inner_pow(num: f64, pow: f64) -> Result<f64, KclError> {
Ok(num.powf(pow))
}
pub async fn acos(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_acos(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "acos",
}]
fn inner_acos(num: f64) -> Result<f64, KclError> {
Ok(num.acos())
}
pub async fn asin(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_asin(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "asin",
}]
fn inner_asin(num: f64) -> Result<f64, KclError> {
Ok(num.asin())
}
pub async fn atan(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_atan(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "atan",
}]
fn inner_atan(num: f64) -> Result<f64, KclError> {
Ok(num.atan())
}
pub async fn log(args: Args) -> Result<MemoryItem, KclError> {
let nums = args.get_number_array()?;
if nums.len() > 2 {
return Err(KclError::Type(KclErrorDetails {
message: format!("expected 2 arguments, got {}", nums.len()),
source_ranges: vec![args.source_range],
}));
}
if nums.len() <= 1 {
return Err(KclError::Type(KclErrorDetails {
message: format!("expected 2 arguments, got {}", nums.len()),
source_ranges: vec![args.source_range],
}));
}
let result = inner_log(nums[0], nums[1])?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "log",
}]
fn inner_log(num: f64, base: f64) -> Result<f64, KclError> {
Ok(num.log(base))
}
pub async fn log2(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_log2(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "log2",
}]
fn inner_log2(num: f64) -> Result<f64, KclError> {
Ok(num.log2())
}
pub async fn log10(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_log10(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "log10",
}]
fn inner_log10(num: f64) -> Result<f64, KclError> {
Ok(num.log10())
}
pub async fn ln(args: Args) -> Result<MemoryItem, KclError> {
let num = args.get_number()?;
let result = inner_ln(num)?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "ln",
}]
fn inner_ln(num: f64) -> Result<f64, KclError> {
Ok(num.ln())
}
pub async fn e(args: Args) -> Result<MemoryItem, KclError> {
let result = inner_e()?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "e",
}]
fn inner_e() -> Result<f64, KclError> {
Ok(std::f64::consts::E)
}
pub async fn tau(args: Args) -> Result<MemoryItem, KclError> {
let result = inner_tau()?;
args.make_user_val_from_f64(result)
}
#[stdlib {
name = "tau",
}]
fn inner_tau() -> Result<f64, KclError> {
Ok(std::f64::consts::TAU)
}