Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions crates/iota-sdk-ffi/src/types/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
types::{
address::Address,
digest::{ObjectDigest, TransactionDigest},
struct_tag::StructTag,
},
};

Expand Down Expand Up @@ -177,6 +178,49 @@ impl Object {
#[derive(Clone, Debug, derive_more::From, uniffi::Object)]
pub struct ObjectData(pub iota_types::ObjectData);

#[uniffi::export]
impl ObjectData {
/// Create an `ObjectData` from a `MoveStruct`
#[uniffi::constructor]
pub fn from_move_struct(move_struct: &MoveStruct) -> Self {
Self(iota_types::ObjectData::Struct(move_struct.0.clone()))
}

/// Create an `ObjectData` from `MovePackage`
#[uniffi::constructor]
pub fn from_move_package(move_package: &MovePackage) -> Self {
Self(iota_types::ObjectData::Package(move_package.0.clone()))
}

/// Return whether this object is a `MoveStruct`
pub fn is_struct(&self) -> bool {
self.0.is_struct()
}

/// Return whether this object is a `MovePackage`
pub fn is_package(&self) -> bool {
self.0.is_package()
}

/// Try to interpret this object as a `MoveStruct`
pub fn try_as_struct(&self) -> Option<Arc<MoveStruct>> {
self.0
.as_struct_opt()
.cloned()
.map(Into::into)
.map(Arc::new)
}

/// Try to interpret this object as a `MovePackage`
pub fn try_as_package(&self) -> Option<Arc<MovePackage>> {
self.0
.as_package_opt()
.cloned()
.map(Into::into)
.map(Arc::new)
}
}

#[derive(Clone, Debug, derive_more::From, uniffi::Object)]
pub struct MovePackage(pub iota_types::MovePackage);

Expand All @@ -186,5 +230,93 @@ pub struct MoveStruct(pub iota_types::MoveStruct);
#[derive(Copy, Clone, Debug, derive_more::From, derive_more::Deref, uniffi::Object)]
pub struct Owner(pub iota_types::Owner);

#[uniffi::export]
impl Owner {
#[uniffi::constructor]
pub fn new_address(address: &Address) -> Self {
Self(iota_types::Owner::Address(address.0))
}

#[uniffi::constructor]
pub fn new_object(id: &ObjectId) -> Self {
Self(iota_types::Owner::Object(id.0))
}

#[uniffi::constructor]
pub fn new_shared(version: Version) -> Self {
Self(iota_types::Owner::Shared(version))
}

#[uniffi::constructor]
pub fn new_immutable() -> Self {
Self(iota_types::Owner::Immutable)
}

pub fn is_address(&self) -> bool {
self.0.is_address()
}

pub fn is_object(&self) -> bool {
self.0.is_object()
}

pub fn is_shared(&self) -> bool {
self.0.is_shared()
}

pub fn is_immutable(&self) -> bool {
self.0.is_immutable()
}

pub fn try_as_address(&self) -> Option<Arc<Address>> {
self.0
.as_address_opt()
.cloned()
.map(Into::into)
.map(Arc::new)
}

pub fn try_as_object(&self) -> Option<Arc<ObjectId>> {
self.0
.as_object_opt()
.cloned()
.map(Into::into)
.map(Arc::new)
}

pub fn try_as_shared(&self) -> Option<Version> {
self.0.as_shared_opt().copied()
}
}

#[derive(Clone, Debug, derive_more::From, uniffi::Object)]
pub struct ObjectType(pub iota_types::ObjectType);

#[uniffi::export]
impl ObjectType {
#[uniffi::constructor]
pub fn new_package() -> Self {
Self(iota_types::ObjectType::Package)
}

#[uniffi::constructor]
pub fn new_struct(struct_tag: &StructTag) -> Self {
Self(iota_types::ObjectType::Struct(struct_tag.0.clone()))
}

pub fn is_package(&self) -> bool {
self.0.is_package()
}

pub fn is_struct(&self) -> bool {
self.0.is_struct()
}

pub fn try_as_struct(&self) -> Option<Arc<StructTag>> {
self.0
.as_struct_opt()
.cloned()
.map(Into::into)
.map(Arc::new)
}
}
6 changes: 6 additions & 0 deletions crates/iota-sdk-types/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ pub enum Owner {
Immutable,
}

impl Owner {
crate::def_is!(Immutable);

crate::def_is_as_into_opt!(Address => Address, Object => ObjectId, Shared => Version);
}

/// Object data, either a package or struct
///
/// # BCS
Expand Down