use serde::{Deserialize, Serialize};
use sui_sdk_types::{Input, Mutability, ObjectReference, SharedInput, Version};
use crate::{Address, ObjectRef};
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Serialize, Deserialize)]
pub enum ObjectArg {
ImmOrOwnedObject(ObjectRef),
SharedObject {
id: Address,
initial_shared_version: Version,
mutable: bool,
},
Receiving(ObjectRef),
}
impl From<ObjectArg> for Input {
fn from(value: ObjectArg) -> Self {
match value {
ObjectArg::ImmOrOwnedObject((i, v, d)) => {
Self::ImmutableOrOwned(ObjectReference::new(i, v, d))
}
ObjectArg::SharedObject {
id,
initial_shared_version,
mutable,
} => Self::Shared(SharedInput::new(
id,
initial_shared_version,
Mutability::from(mutable),
)),
ObjectArg::Receiving((i, v, d)) => Self::Receiving(ObjectReference::new(i, v, d)),
}
}
}
impl ObjectArg {
pub const CLOCK_IMM: Self = Self::SharedObject {
id: Address::from_static("0x6"),
initial_shared_version: 1,
mutable: false,
};
pub const SYSTEM_STATE_IMM: Self = Self::SharedObject {
id: Address::from_static("0x5"),
initial_shared_version: 1,
mutable: false,
};
pub const SYSTEM_STATE_MUT: Self = Self::SharedObject {
id: Address::from_static("0x5"),
initial_shared_version: 1,
mutable: true,
};
pub const fn id(&self) -> Address {
match self {
Self::ImmOrOwnedObject((id, ..)) => *id,
Self::SharedObject { id, .. } => *id,
Self::Receiving((id, ..)) => *id,
}
}
pub const fn id_borrowed(&self) -> &Address {
match self {
Self::ImmOrOwnedObject((id, ..)) => id,
Self::SharedObject { id, .. } => id,
Self::Receiving((id, ..)) => id,
}
}
#[expect(
clippy::missing_const_for_fn,
reason = "Not changing the public API right now"
)]
pub fn set_mutable(&mut self, mutable_: bool) -> Result<(), ImmOwnedOrReceivingError> {
match self {
Self::SharedObject { mutable, .. } => {
*mutable = mutable_;
Ok(())
}
_ => Err(ImmOwnedOrReceivingError),
}
}
}
#[derive(thiserror::Error, Debug)]
#[error("Only Shared ObjectArg's have a mutable flag")]
pub struct ImmOwnedOrReceivingError;