1use std::fmt::{Display, Formatter, Result};
2
3use crate::sys;
4
5#[repr(i32)]
6#[derive(Eq, PartialEq, Debug, Clone, Copy)]
7pub enum Status {
8 Ok = 0,
9 InvalidArg,
10 ObjectExpected,
11 StringExpected,
12 NameExpected,
13 FunctionExpected,
14 NumberExpected,
15 BooleanExpected,
16 ArrayExpected,
17 GenericFailure,
18 PendingException,
19 Cancelled,
20 EscapeCalledTwice,
21 HandleScopeMismatch,
22 CallbackScopeMismatch,
23 /// ThreadSafeFunction queue is full
24 QueueFull,
25 /// ThreadSafeFunction closed
26 Closing,
27 BigintExpected,
28 DateExpected,
29 ArrayBufferExpected,
30 DetachableArraybufferExpected,
31 WouldDeadlock,
32 NoExternalBuffersAllowed,
33 Unknown = 1024, // unknown status. for example, using napi3 module in napi7 Node.js, and generate an invalid napi3 status
34}
35
36impl Display for Status {
37 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
38 let status_string: String = format!("{:?}", self);
39 write!(f, "{}", status_string)
40 }
41}
42
43impl AsRef<str> for Status {
44 fn as_ref(&self) -> &str {
45 match self {
46 Status::Ok => "Ok",
47 Status::InvalidArg => "InvalidArg",
48 Status::ObjectExpected => "ObjectExpected",
49 Status::StringExpected => "StringExpected",
50 Status::NameExpected => "NameExpected",
51 Status::FunctionExpected => "FunctionExpected",
52 Status::NumberExpected => "NumberExpected",
53 Status::BooleanExpected => "BooleanExpected",
54 Status::ArrayExpected => "ArrayExpected",
55 Status::GenericFailure => "GenericFailure",
56 Status::PendingException => "PendingException",
57 Status::Cancelled => "Cancelled",
58 Status::EscapeCalledTwice => "EscapeCalledTwice",
59 Status::HandleScopeMismatch => "HandleScopeMismatch",
60 Status::CallbackScopeMismatch => "CallbackScopeMismatch",
61 Status::QueueFull => "QueueFull",
62 Status::Closing => "Closing",
63 Status::BigintExpected => "BigintExpected",
64 Status::DateExpected => "DateExpected",
65 Status::ArrayBufferExpected => "ArrayBufferExpected",
66 Status::DetachableArraybufferExpected => "DetachableArraybufferExpected",
67 Status::WouldDeadlock => "WouldDeadlock",
68 Status::NoExternalBuffersAllowed => "NoExternalBuffersAllowed",
69 _ => "Unknown",
70 }
71 }
72}
73
74impl From<i32> for Status {
75 fn from(code: i32) -> Self {
76 match code {
77 sys::Status::napi_ok => Status::Ok,
78 sys::Status::napi_invalid_arg => Status::InvalidArg,
79 sys::Status::napi_object_expected => Status::ObjectExpected,
80 sys::Status::napi_string_expected => Status::StringExpected,
81 sys::Status::napi_name_expected => Status::NameExpected,
82 sys::Status::napi_function_expected => Status::FunctionExpected,
83 sys::Status::napi_number_expected => Status::NumberExpected,
84 sys::Status::napi_boolean_expected => Status::BooleanExpected,
85 sys::Status::napi_array_expected => Status::ArrayExpected,
86 sys::Status::napi_generic_failure => Status::GenericFailure,
87 sys::Status::napi_pending_exception => Status::PendingException,
88 sys::Status::napi_cancelled => Status::Cancelled,
89 sys::Status::napi_escape_called_twice => Status::EscapeCalledTwice,
90 sys::Status::napi_handle_scope_mismatch => Status::HandleScopeMismatch,
91 sys::Status::napi_callback_scope_mismatch => Status::CallbackScopeMismatch,
92 sys::Status::napi_queue_full => Status::QueueFull,
93 sys::Status::napi_closing => Status::Closing,
94 sys::Status::napi_bigint_expected => Status::BigintExpected,
95 sys::Status::napi_date_expected => Status::DateExpected,
96 sys::Status::napi_arraybuffer_expected => Status::ArrayBufferExpected,
97 sys::Status::napi_detachable_arraybuffer_expected => Status::DetachableArraybufferExpected,
98 sys::Status::napi_would_deadlock => Status::WouldDeadlock,
99 sys::Status::napi_no_external_buffers_allowed => Status::NoExternalBuffersAllowed,
100 _ => Status::Unknown,
101 }
102 }
103}
104
105impl From<Status> for i32 {
106 fn from(code: Status) -> Self {
107 match code {
108 Status::Ok => sys::Status::napi_ok,
109 Status::InvalidArg => sys::Status::napi_invalid_arg,
110 Status::ObjectExpected => sys::Status::napi_object_expected,
111 Status::StringExpected => sys::Status::napi_string_expected,
112 Status::NameExpected => sys::Status::napi_name_expected,
113 Status::FunctionExpected => sys::Status::napi_function_expected,
114 Status::NumberExpected => sys::Status::napi_number_expected,
115 Status::BooleanExpected => sys::Status::napi_boolean_expected,
116 Status::ArrayExpected => sys::Status::napi_array_expected,
117 Status::GenericFailure => sys::Status::napi_generic_failure,
118 Status::PendingException => sys::Status::napi_pending_exception,
119 Status::Cancelled => sys::Status::napi_cancelled,
120 Status::EscapeCalledTwice => sys::Status::napi_escape_called_twice,
121 Status::HandleScopeMismatch => sys::Status::napi_handle_scope_mismatch,
122 Status::CallbackScopeMismatch => sys::Status::napi_callback_scope_mismatch,
123 Status::QueueFull => sys::Status::napi_queue_full,
124 Status::Closing => sys::Status::napi_closing,
125 Status::BigintExpected => sys::Status::napi_bigint_expected,
126 Status::DateExpected => sys::Status::napi_date_expected,
127 Status::ArrayBufferExpected => sys::Status::napi_arraybuffer_expected,
128 Status::DetachableArraybufferExpected => sys::Status::napi_detachable_arraybuffer_expected,
129 Status::WouldDeadlock => sys::Status::napi_would_deadlock,
130 Status::NoExternalBuffersAllowed => sys::Status::napi_no_external_buffers_allowed,
131 Status::Unknown => sys::Status::napi_generic_failure,
132 }
133 }
134}
135