napi-rs/napi/src/status.rs

101 lines
4 KiB
Rust
Raw Normal View History

use std::fmt::{Display, Formatter, Result};
2018-04-28 17:26:38 +09:00
use crate::sys;
#[repr(i32)]
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
2018-04-28 17:26:38 +09:00
pub enum Status {
Ok = 0,
2018-04-28 17:26:38 +09:00
InvalidArg,
ObjectExpected,
StringExpected,
NameExpected,
FunctionExpected,
NumberExpected,
BooleanExpected,
ArrayExpected,
GenericFailure,
PendingException,
Cancelled,
EscapeCalledTwice,
HandleScopeMismatch,
CallbackScopeMismatch,
/// ThreadSafeFunction queue is full
2019-02-22 12:04:13 +09:00
QueueFull,
/// ThreadSafeFunction closed
2019-02-22 12:04:13 +09:00
Closing,
BigintExpected,
DateExpected,
ArrayBufferExpected,
DetachableArraybufferExpected,
WouldDeadlock,
2021-04-21 18:19:45 +09:00
Unknown = 1024, // unknown status. for example, using napi3 module in napi7 Node.js, and generate an invalid napi3 status
2018-04-28 17:26:38 +09:00
}
impl Display for Status {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let status_string = format!("{:?}", self);
write!(f, "{}", status_string)
2018-04-28 17:26:38 +09:00
}
}
impl From<i32> for Status {
fn from(code: i32) -> Self {
match code {
sys::Status::napi_ok => Status::Ok,
sys::Status::napi_invalid_arg => Status::InvalidArg,
sys::Status::napi_object_expected => Status::ObjectExpected,
sys::Status::napi_string_expected => Status::StringExpected,
sys::Status::napi_name_expected => Status::NameExpected,
sys::Status::napi_function_expected => Status::FunctionExpected,
sys::Status::napi_number_expected => Status::NumberExpected,
sys::Status::napi_boolean_expected => Status::BooleanExpected,
sys::Status::napi_array_expected => Status::ArrayExpected,
sys::Status::napi_generic_failure => Status::GenericFailure,
sys::Status::napi_pending_exception => Status::PendingException,
sys::Status::napi_cancelled => Status::Cancelled,
sys::Status::napi_escape_called_twice => Status::EscapeCalledTwice,
sys::Status::napi_handle_scope_mismatch => Status::HandleScopeMismatch,
sys::Status::napi_callback_scope_mismatch => Status::CallbackScopeMismatch,
sys::Status::napi_queue_full => Status::QueueFull,
sys::Status::napi_closing => Status::Closing,
sys::Status::napi_bigint_expected => Status::BigintExpected,
sys::Status::napi_date_expected => Status::DateExpected,
sys::Status::napi_arraybuffer_expected => Status::ArrayBufferExpected,
sys::Status::napi_detachable_arraybuffer_expected => Status::DetachableArraybufferExpected,
sys::Status::napi_would_deadlock => Status::WouldDeadlock,
_ => Status::Unknown,
}
}
}
2020-12-02 15:10:48 +09:00
impl From<Status> for i32 {
fn from(code: Status) -> Self {
match code {
Status::Ok => sys::Status::napi_ok,
Status::InvalidArg => sys::Status::napi_invalid_arg,
Status::ObjectExpected => sys::Status::napi_object_expected,
Status::StringExpected => sys::Status::napi_string_expected,
Status::NameExpected => sys::Status::napi_name_expected,
Status::FunctionExpected => sys::Status::napi_function_expected,
Status::NumberExpected => sys::Status::napi_number_expected,
Status::BooleanExpected => sys::Status::napi_boolean_expected,
Status::ArrayExpected => sys::Status::napi_array_expected,
Status::GenericFailure => sys::Status::napi_generic_failure,
Status::PendingException => sys::Status::napi_pending_exception,
Status::Cancelled => sys::Status::napi_cancelled,
Status::EscapeCalledTwice => sys::Status::napi_escape_called_twice,
Status::HandleScopeMismatch => sys::Status::napi_handle_scope_mismatch,
Status::CallbackScopeMismatch => sys::Status::napi_callback_scope_mismatch,
Status::QueueFull => sys::Status::napi_queue_full,
Status::Closing => sys::Status::napi_closing,
Status::BigintExpected => sys::Status::napi_bigint_expected,
Status::DateExpected => sys::Status::napi_date_expected,
Status::ArrayBufferExpected => sys::Status::napi_arraybuffer_expected,
Status::DetachableArraybufferExpected => sys::Status::napi_detachable_arraybuffer_expected,
Status::WouldDeadlock => sys::Status::napi_would_deadlock,
Status::Unknown => sys::Status::napi_generic_failure,
}
}
}