af_sui_types/sui/
object.rs1use sui_sdk_types::{Object, Owner, Version};
2
3use crate::Address;
4
5pub trait ObjectHelpers {
10 #[cfg(feature = "hash")]
11 fn object_arg(&self, mutable: bool) -> crate::ObjectArg;
13}
14
15impl ObjectHelpers for Object {
16 #[cfg(feature = "hash")]
17 fn object_arg(&self, mutable: bool) -> crate::ObjectArg {
18 use Owner::*;
19 let id = self.object_id();
20 match self.owner() {
21 Address(_) | Object(_) | Immutable => {
22 crate::ObjectArg::ImmOrOwnedObject((id, self.version(), self.digest()))
23 }
24 Shared(initial_shared_version)
25 | ConsensusAddress {
26 start_version: initial_shared_version,
27 ..
28 } => crate::ObjectArg::SharedObject {
29 id,
30 initial_shared_version: *initial_shared_version,
31 mutable,
32 },
33 _ => panic!("unknown Owner variant"),
34 }
35 }
36}
37
38pub trait OwnerHelpers {
39 fn get_address_owner_address(&self) -> Option<Address>;
46
47 fn get_owner_address(&self) -> Option<Address>;
54
55 fn is_immutable(&self) -> bool;
56
57 fn is_address_owned(&self) -> bool;
58
59 fn is_child_object(&self) -> bool;
60
61 fn is_shared(&self) -> bool;
62
63 fn start_version(&self) -> Option<Version>;
69}
70
71impl OwnerHelpers for Owner {
72 fn get_address_owner_address(&self) -> Option<Address> {
73 match self {
74 Self::Address(address) => Some(*address),
75 Self::Shared { .. }
76 | Self::Immutable
77 | Self::Object(_)
78 | Self::ConsensusAddress { .. } => None,
79 _ => panic!("unknown Owner variant"),
80 }
81 }
82
83 fn get_owner_address(&self) -> Option<Address> {
84 match self {
85 Self::Address(address) => Some(*address),
86 Self::Object(id) => Some(*id),
87 Self::ConsensusAddress { owner, .. } => Some(*owner),
88 Self::Shared { .. } | Self::Immutable => None,
89 _ => panic!("unknown Owner variant"),
90 }
91 }
92
93 fn is_immutable(&self) -> bool {
94 matches!(self, Self::Immutable)
95 }
96
97 fn is_address_owned(&self) -> bool {
98 matches!(self, Self::Address(_))
99 }
100
101 fn is_child_object(&self) -> bool {
102 matches!(self, Self::Object(_))
103 }
104
105 fn is_shared(&self) -> bool {
106 matches!(self, Self::Shared { .. })
107 }
108
109 fn start_version(&self) -> Option<Version> {
110 match self {
111 Self::Immutable | Self::Object(_) | Self::Address(_) => None,
112 Self::Shared(version) => Some(*version),
113 Self::ConsensusAddress { start_version, .. } => Some(*start_version),
114 _ => panic!("unknown Owner variant"),
115 }
116 }
117}