diff --git a/crates/napi/src/bindgen_runtime/js_values/array.rs b/crates/napi/src/bindgen_runtime/js_values/array.rs index 3fc7baba..d47ae411 100644 --- a/crates/napi/src/bindgen_runtime/js_values/array.rs +++ b/crates/napi/src/bindgen_runtime/js_values/array.rs @@ -335,6 +335,20 @@ macro_rules! tuple_from_napi_value { } } +macro_rules! impl_tuple_validate_napi_value { + ($($ident:ident),+) => { + impl<$($ident: FromNapiValue),*> ValidateNapiValue for ($($ident,)*) {} + impl<$($ident: FromNapiValue),*> TypeName for ($($ident,)*) { + fn type_name() -> &'static str { + concat!("Tuple", "(", $(stringify!($ident), ","),*, ")") + } + fn value_type() -> ValueType { + ValueType::Object + } + } + }; +} + impl FromNapiValue for (T0, T1) where T0: FromNapiValue, @@ -599,3 +613,21 @@ where { tuple_from_napi_value!(16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); } + +impl_tuple_validate_napi_value!(T0, T1); +impl_tuple_validate_napi_value!(T0, T1, T2); +impl_tuple_validate_napi_value!(T0, T1, T2, T3); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5, T6); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5, T6, T7); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5, T6, T7, T8); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13); +impl_tuple_validate_napi_value!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14); +impl_tuple_validate_napi_value!( + T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 +); diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index ab7a246a..d6b71e9a 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -415,6 +415,8 @@ Generated by [AVA](https://avajs.dev). ␊ export function eitherBoolOrFunction(input: boolean | ((...args: any[]) => any)): void␊ ␊ + export function eitherBoolOrTuple(input: boolean | [boolean, string]): void␊ + ␊ export function eitherFromObjects(input: A | B | C): string␊ ␊ export function eitherFromOption(): JsClassForEither | undefined␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 37b58329..ab45356b 100644 Binary files a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap and b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap differ diff --git a/examples/napi/__tests__/__snapshots__/values.spec.ts.snap b/examples/napi/__tests__/__snapshots__/values.spec.ts.snap index 22206d79..d4241d4d 100644 Binary files a/examples/napi/__tests__/__snapshots__/values.spec.ts.snap and b/examples/napi/__tests__/__snapshots__/values.spec.ts.snap differ diff --git a/examples/napi/index.cjs b/examples/napi/index.cjs index c7c611ee..eb2b6fb3 100644 --- a/examples/napi/index.cjs +++ b/examples/napi/index.cjs @@ -437,6 +437,7 @@ module.exports.derefUint8Array = nativeBinding.derefUint8Array module.exports.either3 = nativeBinding.either3 module.exports.either4 = nativeBinding.either4 module.exports.eitherBoolOrFunction = nativeBinding.eitherBoolOrFunction +module.exports.eitherBoolOrTuple = nativeBinding.eitherBoolOrTuple module.exports.eitherFromObjects = nativeBinding.eitherFromObjects module.exports.eitherFromOption = nativeBinding.eitherFromOption module.exports.eitherStringOrNumber = nativeBinding.eitherStringOrNumber diff --git a/examples/napi/index.d.cts b/examples/napi/index.d.cts index 1ebc2ce4..19f89483 100644 --- a/examples/napi/index.d.cts +++ b/examples/napi/index.d.cts @@ -405,6 +405,8 @@ export function either4(input: string | number | boolean | Obj): number export function eitherBoolOrFunction(input: boolean | ((...args: any[]) => any)): void +export function eitherBoolOrTuple(input: boolean | [boolean, string]): void + export function eitherFromObjects(input: A | B | C): string export function eitherFromOption(): JsClassForEither | undefined diff --git a/examples/napi/src/either.rs b/examples/napi/src/either.rs index 71bbaa9e..4f8902c1 100644 --- a/examples/napi/src/either.rs +++ b/examples/napi/src/either.rs @@ -143,3 +143,6 @@ pub async fn promise_in_either(input: Either>) -> Result } } } + +#[napi] +pub fn either_bool_or_tuple(_input: Either) {}