impl ToNapiValue for Result<T>
This commit is contained in:
parent
032861c5bc
commit
0d018a5470
9 changed files with 53 additions and 39 deletions
|
@ -64,7 +64,7 @@ pub fn ty_to_ts_type(ty: &Type) -> String {
|
||||||
|
|
||||||
pub fn str_to_ts_type(ty: &str) -> String {
|
pub fn str_to_ts_type(ty: &str) -> String {
|
||||||
match ty {
|
match ty {
|
||||||
"()" => "null".to_owned(),
|
"()" => "undefined".to_owned(),
|
||||||
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" => "number".to_owned(),
|
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" => "number".to_owned(),
|
||||||
"i128" | "isize" | "u64" | "u128" | "usize" => "BigInt".to_owned(),
|
"i128" | "isize" | "u64" | "u128" | "usize" => "BigInt".to_owned(),
|
||||||
"bool" => "boolean".to_owned(),
|
"bool" => "boolean".to_owned(),
|
||||||
|
@ -82,7 +82,7 @@ pub fn str_to_ts_type(ty: &str) -> String {
|
||||||
let captures = TYPE_REGEXES["Option"].captures(s).unwrap();
|
let captures = TYPE_REGEXES["Option"].captures(s).unwrap();
|
||||||
let inner = captures.get(1).unwrap().as_str();
|
let inner = captures.get(1).unwrap().as_str();
|
||||||
|
|
||||||
format!("{} | undefined", str_to_ts_type(inner))
|
format!("{} | null", str_to_ts_type(inner))
|
||||||
}
|
}
|
||||||
s if s.starts_with("Result") && TYPE_REGEXES["Result"].is_match(s) => {
|
s if s.starts_with("Result") && TYPE_REGEXES["Result"].is_match(s) => {
|
||||||
let captures = TYPE_REGEXES["Result"].captures(s).unwrap();
|
let captures = TYPE_REGEXES["Result"].captures(s).unwrap();
|
||||||
|
|
|
@ -95,10 +95,7 @@ pub trait ValidateNapiValue: FromNapiValue + TypeName {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> TypeName for Option<T>
|
impl<T> TypeName for Option<T> {
|
||||||
where
|
|
||||||
T: TypeName,
|
|
||||||
{
|
|
||||||
fn type_name() -> &'static str {
|
fn type_name() -> &'static str {
|
||||||
"Option"
|
"Option"
|
||||||
}
|
}
|
||||||
|
@ -106,19 +103,18 @@ where
|
||||||
|
|
||||||
impl<T> FromNapiValue for Option<T>
|
impl<T> FromNapiValue for Option<T>
|
||||||
where
|
where
|
||||||
T: FromNapiValue + TypeName,
|
T: FromNapiValue,
|
||||||
{
|
{
|
||||||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
|
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
|
||||||
let mut val_type = 0;
|
let mut val_type = 0;
|
||||||
|
|
||||||
check_status!(
|
check_status!(
|
||||||
sys::napi_typeof(env, napi_val, &mut val_type),
|
sys::napi_typeof(env, napi_val, &mut val_type),
|
||||||
"Failed to convert napi value into rust type `Option<{}>`",
|
"Failed to convert napi value into rust type `Option<T>`",
|
||||||
T::type_name()
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
match val_type {
|
match val_type {
|
||||||
sys::ValueType::napi_undefined => Ok(None),
|
sys::ValueType::napi_undefined | sys::ValueType::napi_null => Ok(None),
|
||||||
_ => Ok(Some(T::from_napi_value(env, napi_val)?)),
|
_ => Ok(Some(T::from_napi_value(env, napi_val)?)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,7 +122,7 @@ where
|
||||||
|
|
||||||
impl<T> ToNapiValue for Option<T>
|
impl<T> ToNapiValue for Option<T>
|
||||||
where
|
where
|
||||||
T: ToNapiValue + TypeName,
|
T: ToNapiValue,
|
||||||
{
|
{
|
||||||
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
||||||
match val {
|
match val {
|
||||||
|
@ -134,12 +130,33 @@ where
|
||||||
None => {
|
None => {
|
||||||
let mut ptr = ptr::null_mut();
|
let mut ptr = ptr::null_mut();
|
||||||
check_status!(
|
check_status!(
|
||||||
sys::napi_get_undefined(env, &mut ptr),
|
sys::napi_get_null(env, &mut ptr),
|
||||||
"Failed to convert rust type `Option<{}>` into napi value",
|
"Failed to convert rust type `Option<T>` into napi value",
|
||||||
T::type_name(),
|
|
||||||
)?;
|
)?;
|
||||||
Ok(ptr)
|
Ok(ptr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> ToNapiValue for Result<T>
|
||||||
|
where
|
||||||
|
T: ToNapiValue,
|
||||||
|
{
|
||||||
|
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
||||||
|
match val {
|
||||||
|
Ok(v) => T::to_napi_value(env, v),
|
||||||
|
Err(e) => {
|
||||||
|
let error_code = String::to_napi_value(env, format!("{:?}", e.status))?;
|
||||||
|
let reason = String::to_napi_value(env, e.reason)?;
|
||||||
|
let mut error = ptr::null_mut();
|
||||||
|
check_status!(
|
||||||
|
sys::napi_create_error(env, error_code, reason, &mut error),
|
||||||
|
"Failed to create napi error"
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -79,22 +79,3 @@ impl ToNapiValue for Undefined {
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToNapiValue for Result<()> {
|
|
||||||
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
|
|
||||||
match val {
|
|
||||||
Ok(_) => Ok(Null::to_napi_value(env, Null).unwrap_or_else(|_| ptr::null_mut())),
|
|
||||||
Err(e) => {
|
|
||||||
let error_code = String::to_napi_value(env, format!("{:?}", e.status))?;
|
|
||||||
let reason = String::to_napi_value(env, e.reason)?;
|
|
||||||
let mut error = ptr::null_mut();
|
|
||||||
check_status!(
|
|
||||||
sys::napi_create_error(env, error_code, reason, &mut error),
|
|
||||||
"Failed to create napi error"
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,11 +12,12 @@ Generated by [AVA](https://avajs.dev).
|
||||||
export function getNums(): Array<number>␊
|
export function getNums(): Array<number>␊
|
||||||
export function sumNums(nums: Array<number>): number␊
|
export function sumNums(nums: Array<number>): number␊
|
||||||
export function getCwd(callback: (arg0: string) => void): void␊
|
export function getCwd(callback: (arg0: string) => void): void␊
|
||||||
export function readFile(callback: (arg0: Error | null, arg1: string | undefined) => void): void␊
|
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void␊
|
||||||
export enum Kind { Dog = 0, Cat = 1, Duck = 2 }␊
|
export enum Kind { Dog = 0, Cat = 1, Duck = 2 }␊
|
||||||
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }␊
|
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }␊
|
||||||
export function enumToI32(e: CustomNumEnum): number␊
|
export function enumToI32(e: CustomNumEnum): number␊
|
||||||
export function mapOption(val: number | undefined): number | undefined␊
|
export function getError(): Error | undefined␊
|
||||||
|
export function mapOption(val: number | null): number | null␊
|
||||||
export function add(a: number, b: number): number␊
|
export function add(a: number, b: number): number␊
|
||||||
export function fibonacci(n: number): number␊
|
export function fibonacci(n: number): number␊
|
||||||
export function listObjKeys(obj: object): Array<string>␊
|
export function listObjKeys(obj: object): Array<string>␊
|
||||||
|
|
Binary file not shown.
|
@ -19,6 +19,7 @@ import {
|
||||||
createObj,
|
createObj,
|
||||||
mapOption,
|
mapOption,
|
||||||
readFile,
|
readFile,
|
||||||
|
getError,
|
||||||
} from '../'
|
} from '../'
|
||||||
|
|
||||||
test('number', (t) => {
|
test('number', (t) => {
|
||||||
|
@ -81,7 +82,7 @@ test('callback', (t) => {
|
||||||
)
|
)
|
||||||
|
|
||||||
readFile((err, content) => {
|
readFile((err, content) => {
|
||||||
t.is(err, null)
|
t.is(err, undefined)
|
||||||
t.is(content, 'hello world')
|
t.is(content, 'hello world')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -92,6 +93,12 @@ test('object', (t) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Option', (t) => {
|
test('Option', (t) => {
|
||||||
t.is(mapOption(undefined), undefined)
|
t.is(mapOption(null), null)
|
||||||
t.is(mapOption(3), 4)
|
t.is(mapOption(3), 4)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('Result', (t) => {
|
||||||
|
const error = getError()
|
||||||
|
t.not(error, undefined)
|
||||||
|
t.is(error!.message, 'Manual Error')
|
||||||
|
})
|
||||||
|
|
5
examples/napi/index.d.ts
vendored
5
examples/napi/index.d.ts
vendored
|
@ -2,11 +2,12 @@ export function getWords(): Array<string>
|
||||||
export function getNums(): Array<number>
|
export function getNums(): Array<number>
|
||||||
export function sumNums(nums: Array<number>): number
|
export function sumNums(nums: Array<number>): number
|
||||||
export function getCwd(callback: (arg0: string) => void): void
|
export function getCwd(callback: (arg0: string) => void): void
|
||||||
export function readFile(callback: (arg0: Error | null, arg1: string | undefined) => void): void
|
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void
|
||||||
export enum Kind { Dog = 0, Cat = 1, Duck = 2 }
|
export enum Kind { Dog = 0, Cat = 1, Duck = 2 }
|
||||||
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }
|
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }
|
||||||
export function enumToI32(e: CustomNumEnum): number
|
export function enumToI32(e: CustomNumEnum): number
|
||||||
export function mapOption(val: number | undefined): number | undefined
|
export function getError(): Error | undefined
|
||||||
|
export function mapOption(val: number | null): number | null
|
||||||
export function add(a: number, b: number): number
|
export function add(a: number, b: number): number
|
||||||
export function fibonacci(n: number): number
|
export function fibonacci(n: number): number
|
||||||
export function listObjKeys(obj: object): Array<string>
|
export function listObjKeys(obj: object): Array<string>
|
||||||
|
|
6
examples/napi/src/error.rs
Normal file
6
examples/napi/src/error.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
use napi::bindgen_prelude::*;
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
fn get_error() -> Result<()> {
|
||||||
|
Err(Error::new(Status::InvalidArg, "Manual Error".to_owned()))
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ mod array;
|
||||||
mod callback;
|
mod callback;
|
||||||
mod class;
|
mod class;
|
||||||
mod r#enum;
|
mod r#enum;
|
||||||
|
mod error;
|
||||||
mod nullable;
|
mod nullable;
|
||||||
mod number;
|
mod number;
|
||||||
mod object;
|
mod object;
|
||||||
|
|
Loading…
Reference in a new issue