Skip to content

Commit

Permalink
feat: add impl add
Browse files Browse the repository at this point in the history
  • Loading branch information
richerfu committed Jun 25, 2024
1 parent e5e3118 commit 17c77a1
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
8 changes: 4 additions & 4 deletions crates/asset/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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}
ohos-asset-sys = { workspace = true }
16 changes: 16 additions & 0 deletions crates/asset/src/data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::mem::ManuallyDrop;

use crate::AssetTag;

pub type AssetBlob = Vec<u8>;

pub enum AssetValue {
Boolean(bool),
U32IntT(u32),
Blob(ManuallyDrop<AssetBlob>),
}

pub struct AssetAttr {
pub tag: AssetTag,
pub value: AssetValue,
}
39 changes: 38 additions & 1 deletion crates/asset/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
mod r#type;
mod data;
mod r#type;

pub use data::*;
pub use r#type::*;

/// add
pub fn asset_add(attrs: Vec<AssetAttr>) {
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::<Vec<_>>();
unsafe { ohos_asset_sys::OH_Asset_Add(real_attrs.as_ptr(), count) };
}
142 changes: 140 additions & 2 deletions crates/asset/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -157,11 +158,148 @@ impl From<AssetSyncType> 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
}
}
}
}

#[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<AssetTag> 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<AssetTagType> 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<AssetOperationType> 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
}
}
}
}
6 changes: 3 additions & 3 deletions sys/ohos-asset-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
10 changes: 10 additions & 0 deletions sys/ohos-asset-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -511,13 +517,17 @@ 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;
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 {
Expand Down
2 changes: 1 addition & 1 deletion tools/generate/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static CONFIG: Lazy<Vec<SysConfig>> = 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"],
},
]
});
Expand Down

0 comments on commit 17c77a1

Please sign in to comment.