feat(napi): support to use tuple with either (#1993)

`Either` uses `ValidateNapiValue` + `TypeName` to validate and report error on value not being matched. So there's no way to remove these super traits from it. So I implemented these types to `Tuple` types.
This commit is contained in:
Hana 2024-03-13 13:29:06 +08:00 committed by GitHub
parent 6b1058a268
commit 97746b79a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 40 additions and 0 deletions

View file

@ -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<T0, T1> FromNapiValue for (T0, T1) impl<T0, T1> FromNapiValue for (T0, T1)
where where
T0: FromNapiValue, 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); 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
);

View file

@ -415,6 +415,8 @@ Generated by [AVA](https://avajs.dev).
export function eitherBoolOrFunction(input: boolean | ((...args: any[]) => any)): void␊ 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 eitherFromObjects(input: A | B | C): string␊
export function eitherFromOption(): JsClassForEither | undefined␊ export function eitherFromOption(): JsClassForEither | undefined␊

View file

@ -437,6 +437,7 @@ module.exports.derefUint8Array = nativeBinding.derefUint8Array
module.exports.either3 = nativeBinding.either3 module.exports.either3 = nativeBinding.either3
module.exports.either4 = nativeBinding.either4 module.exports.either4 = nativeBinding.either4
module.exports.eitherBoolOrFunction = nativeBinding.eitherBoolOrFunction module.exports.eitherBoolOrFunction = nativeBinding.eitherBoolOrFunction
module.exports.eitherBoolOrTuple = nativeBinding.eitherBoolOrTuple
module.exports.eitherFromObjects = nativeBinding.eitherFromObjects module.exports.eitherFromObjects = nativeBinding.eitherFromObjects
module.exports.eitherFromOption = nativeBinding.eitherFromOption module.exports.eitherFromOption = nativeBinding.eitherFromOption
module.exports.eitherStringOrNumber = nativeBinding.eitherStringOrNumber module.exports.eitherStringOrNumber = nativeBinding.eitherStringOrNumber

View file

@ -405,6 +405,8 @@ export function either4(input: string | number | boolean | Obj): number
export function eitherBoolOrFunction(input: boolean | ((...args: any[]) => any)): void 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 eitherFromObjects(input: A | B | C): string
export function eitherFromOption(): JsClassForEither | undefined export function eitherFromOption(): JsClassForEither | undefined

View file

@ -143,3 +143,6 @@ pub async fn promise_in_either(input: Either<u32, Promise<u32>>) -> Result<bool>
} }
} }
} }
#[napi]
pub fn either_bool_or_tuple(_input: Either<bool, (bool, String)>) {}