use std::collections::HashMap; use chrono::{DateTime, Utc}; use napi::{bindgen_prelude::*, JsSymbol, JsUnknown}; #[napi(strict)] fn validate_array(arr: Vec) -> u32 { arr.len() as u32 } #[napi(strict)] fn validate_buffer(b: Buffer) -> u32 { b.len() as u32 } #[napi(strict)] fn validate_typed_array(input: Uint8Array) -> u32 { input.len() as u32 } #[napi(strict)] fn validate_bigint(input: BigInt) -> i128 { input.get_i128().0 } #[napi(strict)] fn validate_boolean(i: bool) -> bool { !i } #[napi(strict)] fn validate_date(d: Date) -> Result { d.value_of() } #[napi(strict)] fn validate_date_time(_d: DateTime) -> i64 { 1 } #[napi(strict)] fn validate_external(e: External) -> u32 { *e } #[napi(strict, ts_args_type = "cb: () => number")] fn validate_function(cb: JsFunction) -> Result { Ok( cb.call::(None, &[])? .coerce_to_number()? .get_uint32()? + 3, ) } #[napi(strict)] fn validate_hash_map(input: HashMap) -> u32 { input.len() as u32 } #[napi(strict)] fn validate_null(_i: Null) -> bool { true } #[napi(strict)] fn validate_undefined(_i: Undefined) -> bool { true } #[napi(strict)] fn validate_number(i: f64) -> f64 { i + 1.0 } #[napi(strict)] async fn validate_promise(p: Promise) -> Result { Ok(p.await? + 1) } #[napi(strict)] fn validate_string(s: String) -> String { s + "!" } #[napi(strict)] fn validate_symbol(_s: JsSymbol) -> bool { true } #[napi(strict)] fn validate_optional(input1: Option, input2: Option) -> bool { input1.is_some() || input2.unwrap_or(false) } #[napi(return_if_invalid)] fn return_undefined_if_invalid(input: bool) -> bool { !input } #[napi(return_if_invalid)] async fn return_undefined_if_invalid_promise(input: Promise) -> Result { let input_value = input.await?; Ok(!input_value) }