-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from ohos-rs/feat-build
feat: add error and result
- Loading branch information
Showing
17 changed files
with
251 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,100 @@ | ||
use thiserror::Error; | ||
use enum_macro::EnumFrom; | ||
#[cfg(feature = "napi")] | ||
use napi_ohos::{Error, Result}; | ||
use ohos_arkui_sys::*; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ArkUIError { | ||
#[error("ArkUI call `{0}` failed with params invalid")] | ||
ParamsInvalid(String), | ||
#[derive(Debug, EnumFrom)] | ||
#[enum_from_config(ArkUI_ErrorCode, "ArkUI_ErrorCode_ARKUI_ERROR_CODE_")] | ||
pub enum ArkUIErrorCode { | ||
ParamInvalid, | ||
AttributeOrEventNotSupported, | ||
ArkTSNodeNotSupported, | ||
AdapterNotBound, | ||
AdapterExist, | ||
ChildNodeExist, | ||
NodeEventParamIndexOutOfRange, | ||
NodeEventParamInvalid, | ||
NodeIndexInvalid, | ||
BufferSizeError, | ||
NonScrollableContainer, | ||
BufferSizeNotEnough, | ||
} | ||
|
||
#[cfg(not(feature = "napi"))] | ||
pub struct ArkUIError { | ||
pub code: ArkUIErrorCode, | ||
pub message: Option<String>, | ||
} | ||
|
||
#[cfg(not(feature = "napi"))] | ||
impl ArkUIError { | ||
pub fn new<T: AsRef<str>>(code: ArkUIErrorCode, message: T) -> Self { | ||
Self { | ||
code, | ||
message: Some(message.as_ref().to_string()), | ||
} | ||
} | ||
|
||
pub fn from_status(code: ArkUIErrorCode) -> Self { | ||
Self { | ||
code, | ||
message: None, | ||
} | ||
} | ||
|
||
pub fn from_reason<T: AsRef<str>>(message: T) -> Self { | ||
Self { | ||
code: ArkUIErrorCode::ParamInvalid, | ||
message: Some(message.as_ref().to_string()), | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<str> for ArkUIErrorCode { | ||
fn as_ref(&self) -> &str { | ||
match self { | ||
ArkUIErrorCode::AdapterExist => "AdapterExist", | ||
ArkUIErrorCode::AdapterNotBound => "AdapterNotBound", | ||
ArkUIErrorCode::ArkTSNodeNotSupported => "ArkTSNodeNotSupported", | ||
ArkUIErrorCode::AttributeOrEventNotSupported => "AttributeOrEventNotSupported", | ||
ArkUIErrorCode::BufferSizeError => "BufferSizeError", | ||
ArkUIErrorCode::BufferSizeNotEnough => "BufferSizeNotEnough", | ||
ArkUIErrorCode::ChildNodeExist => "ChildNodeExist", | ||
ArkUIErrorCode::NonScrollableContainer => "NonScrollableContainer", | ||
ArkUIErrorCode::NodeEventParamIndexOutOfRange => "NodeEventParamIndexOutOfRange", | ||
ArkUIErrorCode::NodeEventParamInvalid => "NodeEventParamInvalid", | ||
ArkUIErrorCode::NodeIndexInvalid => "NodeIndexInvalid", | ||
ArkUIErrorCode::ParamInvalid => "ParamInvalid", | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "napi"))] | ||
/// This type is used for ArkUI result. | ||
pub type ArkUIResult = Result<(), ArkUIError>; | ||
pub type ArkUIResult<T> = Result<T, ArkUIError>; | ||
|
||
#[cfg(feature = "napi")] | ||
pub type ArkUIResult<T> = Result<T, ArkUIErrorCode>; | ||
|
||
#[cfg(feature = "napi")] | ||
pub type ArkUIError = Error<ArkUIErrorCode>; | ||
|
||
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! check_arkui_status { | ||
($code:expr) => {{ | ||
let c = $code; | ||
let c = $code as u32; | ||
match c { | ||
$crate::sys::Status::napi_ok => Ok(()), | ||
_ => Err($crate::Error::new($crate::Status::from(c), "".to_owned())), | ||
ohos_arkui_sys::ArkUI_ErrorCode_ARKUI_ERROR_CODE_NO_ERROR => Ok(()), | ||
_ => Err($crate::ArkUIError::new(c.into(), "".to_owned())), | ||
} | ||
}}; | ||
|
||
($code:expr, $($msg:tt)*) => {{ | ||
let c = $code; | ||
let c = $code as u32; | ||
match c { | ||
$crate::sys::Status::napi_ok => Ok(()), | ||
_ => Err($crate::Error::new($crate::Status::from(c), format!($($msg)*))), | ||
ohos_arkui_sys::ArkUI_ErrorCode_ARKUI_ERROR_CODE_NO_ERROR => Ok(()), | ||
_ => Err($crate::ArkUIError::new(c.into(), format!($($msg)*))), | ||
} | ||
}}; | ||
|
||
($code:expr, $msg:expr, $env:expr, $val:expr) => {{ | ||
let c = $code; | ||
match c { | ||
$crate::sys::Status::napi_ok => Ok(()), | ||
_ => Err($crate::Error::new($crate::Status::from(c), format!($msg, $crate::type_of!($env, $val)?))), | ||
} | ||
}}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
mod attribute; | ||
mod error; | ||
#[cfg(feature = "napi")] | ||
mod handle; | ||
mod native_node_api; | ||
mod node; | ||
|
||
pub use attribute::*; | ||
pub use error::*; | ||
#[cfg(feature = "napi")] | ||
pub use handle::*; | ||
pub use native_node_api::*; | ||
pub use node::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.