diff --git a/Cargo.toml b/Cargo.toml index b68b446..713a13d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ ohos-asset-binding = { version = "0.0.1", path = "crates/asset" } ohos-bundle-sys = { version = "0.0.2", path = "sys/ohos-bundle-sys" } ohos-init-sys = { version = "0.0.2", path = "sys/ohos-init-sys" } ohos-hilogs-sys = { version = "0.0.2", path = "sys/ohos-hilogs-sys" } -ohos-asset-sys = { version = "0.0.1", path = "sys/ohos-asset-sys" } +ohos-asset-sys = { version = "0.0.1", path = "sys/ohos-asset-sys" } napi-ohos = { version = "1.0.0-beta.2" } napi-derive-ohos = { version = "1.0.0-beta.2" } diff --git a/crates/asset/Cargo.toml b/crates/asset/Cargo.toml index bc217cb..89d1255 100644 --- a/crates/asset/Cargo.toml +++ b/crates/asset/Cargo.toml @@ -1,9 +1,9 @@ [package] -name = "ohos-asset-binding" -version = "0.0.1" -edition = "2021" +name = "ohos-asset-binding" +version = "0.0.1" +edition = "2021" license = "MIT OR Apache-2.0" description = "OpenHarmony's asset binding for rust" [dependencies] -ohos-asset-sys = {workspace = true} \ No newline at end of file +ohos-asset-sys = { workspace = true } diff --git a/crates/asset/src/data.rs b/crates/asset/src/data.rs new file mode 100644 index 0000000..45244b8 --- /dev/null +++ b/crates/asset/src/data.rs @@ -0,0 +1,16 @@ +use std::mem::ManuallyDrop; + +use crate::AssetTag; + +pub type AssetBlob = Vec; + +pub enum AssetValue { + Boolean(bool), + U32IntT(u32), + Blob(ManuallyDrop), +} + +pub struct AssetAttr { + pub tag: AssetTag, + pub value: AssetValue, +} diff --git a/crates/asset/src/lib.rs b/crates/asset/src/lib.rs index d02d375..fdfae31 100644 --- a/crates/asset/src/lib.rs +++ b/crates/asset/src/lib.rs @@ -1 +1,38 @@ -mod r#type; \ No newline at end of file +mod data; +mod r#type; + +pub use data::*; +pub use r#type::*; + +/// add +pub fn asset_add(attrs: Vec) { + let count = attrs.len() as u32; + let real_attrs = attrs + .iter() + .map(|a| match &a.value { + AssetValue::Blob(blob) => { + let b = ohos_asset_sys::Asset_Blob { + size: blob.len() as u32, + data: blob.as_ptr() as *mut u8, + }; + ohos_asset_sys::Asset_Attr { + tag: a.tag.into(), + value: ohos_asset_sys::Asset_Value { blob: b }, + } + } + AssetValue::Boolean(boolean) => ohos_asset_sys::Asset_Attr { + tag: a.tag.into(), + value: ohos_asset_sys::Asset_Value { + boolean: boolean.to_owned(), + }, + }, + AssetValue::U32IntT(uint32_t) => ohos_asset_sys::Asset_Attr { + tag: a.tag.into(), + value: ohos_asset_sys::Asset_Value { + u32_: uint32_t.to_owned(), + }, + }, + }) + .collect::>(); + unsafe { ohos_asset_sys::OH_Asset_Add(real_attrs.as_ptr(), count) }; +} diff --git a/crates/asset/src/type.rs b/crates/asset/src/type.rs index 5cb8410..6158bf9 100644 --- a/crates/asset/src/type.rs +++ b/crates/asset/src/type.rs @@ -15,7 +15,8 @@ use ohos_asset_sys::{ Asset_ResultCode_ASSET_STATUS_MISMATCH, Asset_ResultCode_ASSET_SUCCESS, Asset_ResultCode_ASSET_UNSUPPORTED, Asset_ReturnType_ASSET_RETURN_ALL, Asset_ReturnType_ASSET_RETURN_ATTRIBUTES, Asset_SyncType_ASSET_SYNC_TYPE_NEVER, - Asset_SyncType_ASSET_SYNC_TYPE_THIS_DEVICE, Asset_SyncType_ASSET_SYNC_TYPE_TRUSTED_DEVICE, + Asset_SyncType_ASSET_SYNC_TYPE_THIS_DEVICE, Asset_SyncType_ASSET_SYNC_TYPE_TRUSTED_ACCOUNT, + Asset_SyncType_ASSET_SYNC_TYPE_TRUSTED_DEVICE, }; #[derive(Debug)] @@ -157,7 +158,7 @@ impl From for ohos_asset_sys::Asset_SyncType { AssetSyncType::AssetSyncTypeNever => Asset_SyncType_ASSET_SYNC_TYPE_NEVER, AssetSyncType::AssetSyncTypeThisDevice => Asset_SyncType_ASSET_SYNC_TYPE_THIS_DEVICE, AssetSyncType::AssetSyncTypeTrustedAccount => { - Asset_SyncType_ASSET_SYNC_TYPE_TRUSTED_DEVICE + Asset_SyncType_ASSET_SYNC_TYPE_TRUSTED_ACCOUNT } AssetSyncType::AssetSyncTypeTrustedDevice => { Asset_SyncType_ASSET_SYNC_TYPE_TRUSTED_DEVICE @@ -165,3 +166,140 @@ impl From for ohos_asset_sys::Asset_SyncType { } } } + +#[derive(Debug, Clone, Copy)] +pub enum AssetTag { + AssetTagSecret, + AssetTagAlias, + AssetTagAccessibility, + AssetTagRequirePasswordSet, + AssetTagAuthType, + AssetTagAuthValidityPeriod, + AssetTagAuthChallenge, + AssetTagAuthToken, + AssetTagSyncType, + AssetTagIsPersistent, + AssetTagDataLabelCritical1, + AssetTagDataLabelCritical2, + AssetTagDataLabelCritical3, + AssetTagDataLabelCritical4, + AssetTagDataLabelNormal1, + AssetTagDataLabelNormal2, + AssetTagDataLabelNormal3, + AssetTagDataLabelNormal4, + AssetTagDataLabelNormalLocal1, + AssetTagDataLabelNormalLocal2, + AssetTagDataLabelNormalLocal3, + AssetTagDataLabelNormalLocal4, + AssetTagReturnType, + AssetTagReturnLimit, + AssetTagReturnOffset, + AssetTagReturnOrderedBy, + AssetTagConflictResolution, + AssetTagUpdateTime, + AssetTagOperationType, +} + +impl From for ohos_asset_sys::Asset_Tag { + fn from(value: AssetTag) -> Self { + match value { + AssetTag::AssetTagSecret => ohos_asset_sys::Asset_Tag_ASSET_TAG_SECRET, + AssetTag::AssetTagAlias => ohos_asset_sys::Asset_Tag_ASSET_TAG_ALIAS, + AssetTag::AssetTagAccessibility => ohos_asset_sys::Asset_Tag_ASSET_TAG_ACCESSIBILITY, + AssetTag::AssetTagRequirePasswordSet => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_REQUIRE_PASSWORD_SET + } + AssetTag::AssetTagAuthType => ohos_asset_sys::Asset_Tag_ASSET_TAG_AUTH_TYPE, + AssetTag::AssetTagAuthValidityPeriod => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_AUTH_VALIDITY_PERIOD + } + AssetTag::AssetTagAuthChallenge => ohos_asset_sys::Asset_Tag_ASSET_TAG_AUTH_CHALLENGE, + AssetTag::AssetTagAuthToken => ohos_asset_sys::Asset_Tag_ASSET_TAG_AUTH_TOKEN, + AssetTag::AssetTagSyncType => ohos_asset_sys::Asset_Tag_ASSET_TAG_AUTH_TYPE, + AssetTag::AssetTagIsPersistent => ohos_asset_sys::Asset_Tag_ASSET_TAG_IS_PERSISTENT, + AssetTag::AssetTagDataLabelCritical1 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_CRITICAL_1 + } + AssetTag::AssetTagDataLabelCritical2 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_CRITICAL_2 + } + AssetTag::AssetTagDataLabelCritical3 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_CRITICAL_3 + } + AssetTag::AssetTagDataLabelCritical4 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_CRITICAL_4 + } + AssetTag::AssetTagDataLabelNormal1 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_1 + } + AssetTag::AssetTagDataLabelNormal2 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_2 + } + AssetTag::AssetTagDataLabelNormal3 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_3 + } + AssetTag::AssetTagDataLabelNormal4 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_4 + } + AssetTag::AssetTagDataLabelNormalLocal1 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_1 + } + AssetTag::AssetTagDataLabelNormalLocal2 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_2 + } + AssetTag::AssetTagDataLabelNormalLocal3 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_3 + } + AssetTag::AssetTagDataLabelNormalLocal4 => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_4 + } + AssetTag::AssetTagReturnType => ohos_asset_sys::Asset_Tag_ASSET_TAG_RETURN_TYPE, + AssetTag::AssetTagReturnLimit => ohos_asset_sys::Asset_Tag_ASSET_TAG_RETURN_LIMIT, + AssetTag::AssetTagReturnOffset => ohos_asset_sys::Asset_Tag_ASSET_TAG_RETURN_OFFSET, + AssetTag::AssetTagReturnOrderedBy => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_RETURN_ORDERED_BY + } + AssetTag::AssetTagConflictResolution => { + ohos_asset_sys::Asset_Tag_ASSET_TAG_CONFLICT_RESOLUTION + } + AssetTag::AssetTagUpdateTime => ohos_asset_sys::Asset_Tag_ASSET_TAG_UPDATE_TIME, + AssetTag::AssetTagOperationType => ohos_asset_sys::Asset_Tag_ASSET_TAG_OPERATION_TYPE, + } + } +} + +#[derive(Debug)] +pub enum AssetTagType { + AssetTypeBool, + AssetTypeNumber, + AssetTypeBytes, +} + +impl From for ohos_asset_sys::Asset_TagType { + fn from(value: AssetTagType) -> Self { + match value { + AssetTagType::AssetTypeBool => ohos_asset_sys::Asset_TagType_ASSET_TYPE_BOOL, + AssetTagType::AssetTypeNumber => ohos_asset_sys::Asset_TagType_ASSET_TYPE_NUMBER, + AssetTagType::AssetTypeBytes => ohos_asset_sys::Asset_TagType_ASSET_TYPE_BYTES, + } + } +} + +#[derive(Debug)] +pub enum AssetOperationType { + AssetNeedSync, + AssetNeedLogout, +} + +impl From for ohos_asset_sys::Asset_OperationType { + fn from(value: AssetOperationType) -> Self { + match value { + AssetOperationType::AssetNeedLogout => { + ohos_asset_sys::Asset_OperationType_ASSET_NEED_LOGOUT + } + AssetOperationType::AssetNeedSync => { + ohos_asset_sys::Asset_OperationType_ASSET_NEED_SYNC + } + } + } +} diff --git a/sys/ohos-asset-sys/Cargo.toml b/sys/ohos-asset-sys/Cargo.toml index 7de540a..21d8c96 100644 --- a/sys/ohos-asset-sys/Cargo.toml +++ b/sys/ohos-asset-sys/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "ohos-asset-sys" -version = "0.0.1" -edition = "2021" +name = "ohos-asset-sys" +version = "0.0.1" +edition = "2021" license = "MIT OR Apache-2.0" description = "OpenHarmony's asset binding for rust" diff --git a/sys/ohos-asset-sys/src/lib.rs b/sys/ohos-asset-sys/src/lib.rs index c211136..46384f4 100644 --- a/sys/ohos-asset-sys/src/lib.rs +++ b/sys/ohos-asset-sys/src/lib.rs @@ -474,11 +474,17 @@ pub const Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_1: Asset_Tag = 805306416; pub const Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_2: Asset_Tag = 805306417; pub const Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_3: Asset_Tag = 805306418; pub const Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_4: Asset_Tag = 805306419; +pub const Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_1: Asset_Tag = 805306420; +pub const Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_2: Asset_Tag = 805306421; +pub const Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_3: Asset_Tag = 805306422; +pub const Asset_Tag_ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_4: Asset_Tag = 805306423; pub const Asset_Tag_ASSET_TAG_RETURN_TYPE: Asset_Tag = 536870976; pub const Asset_Tag_ASSET_TAG_RETURN_LIMIT: Asset_Tag = 536870977; pub const Asset_Tag_ASSET_TAG_RETURN_OFFSET: Asset_Tag = 536870978; pub const Asset_Tag_ASSET_TAG_RETURN_ORDERED_BY: Asset_Tag = 536870979; pub const Asset_Tag_ASSET_TAG_CONFLICT_RESOLUTION: Asset_Tag = 536870980; +pub const Asset_Tag_ASSET_TAG_UPDATE_TIME: Asset_Tag = 805306437; +pub const Asset_Tag_ASSET_TAG_OPERATION_TYPE: Asset_Tag = 536870982; pub type Asset_Tag = ::std::os::raw::c_uint; pub const Asset_ResultCode_ASSET_SUCCESS: Asset_ResultCode = 0; pub const Asset_ResultCode_ASSET_PERMISSION_DENIED: Asset_ResultCode = 201; @@ -511,6 +517,7 @@ pub type Asset_AuthType = ::std::os::raw::c_uint; pub const Asset_SyncType_ASSET_SYNC_TYPE_NEVER: Asset_SyncType = 0; pub const Asset_SyncType_ASSET_SYNC_TYPE_THIS_DEVICE: Asset_SyncType = 1; pub const Asset_SyncType_ASSET_SYNC_TYPE_TRUSTED_DEVICE: Asset_SyncType = 2; +pub const Asset_SyncType_ASSET_SYNC_TYPE_TRUSTED_ACCOUNT: Asset_SyncType = 4; pub type Asset_SyncType = ::std::os::raw::c_uint; pub const Asset_ConflictResolution_ASSET_CONFLICT_OVERWRITE: Asset_ConflictResolution = 0; pub const Asset_ConflictResolution_ASSET_CONFLICT_THROW_ERROR: Asset_ConflictResolution = 1; @@ -518,6 +525,9 @@ pub type Asset_ConflictResolution = ::std::os::raw::c_uint; pub const Asset_ReturnType_ASSET_RETURN_ALL: Asset_ReturnType = 0; pub const Asset_ReturnType_ASSET_RETURN_ATTRIBUTES: Asset_ReturnType = 1; pub type Asset_ReturnType = ::std::os::raw::c_uint; +pub const Asset_OperationType_ASSET_NEED_SYNC: Asset_OperationType = 0; +pub const Asset_OperationType_ASSET_NEED_LOGOUT: Asset_OperationType = 1; +pub type Asset_OperationType = ::std::os::raw::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct Asset_Blob { diff --git a/tools/generate/build.rs b/tools/generate/build.rs index c21f4b4..63984f5 100644 --- a/tools/generate/build.rs +++ b/tools/generate/build.rs @@ -28,7 +28,7 @@ static CONFIG: Lazy> = Lazy::new(|| { }, SysConfig { name: "ohos-asset-sys", - headers: vec!["asset/asset_api.h","asset/asset_type.h"], + headers: vec!["asset/asset_api.h", "asset/asset_type.h"], }, ] });