#![cfg_attr(all(doc, not(doctest)), feature(doc_auto_cfg))]
pub use af_move_type::vector::MoveVec;
pub use af_move_type::{HasKey, MoveStruct, MoveType};
pub use af_sui_types::u256::U256;
pub use af_sui_types::{self, Address, TypeTag};
use tabled::grid::config::ColoredConfig;
use tabled::grid::dimension::CompleteDimensionVecRecords;
use tabled::grid::records::vec_records::{Text, VecRecords};
use tabled::settings::panel::Header;
use tabled::settings::style::Style;
use tabled::settings::{Rotate, Settings, TableOption};
pub use tabled::{self, Table, Tabled};
pub use {af_move_type, derive_new, serde};
#[macro_export]
macro_rules! sui_pkg_sdk {
($package:ident$(@$address:literal)? {
$($tt:tt)*
}) => {
$crate::sui_pkg_sdk!(@Modules $(@$address)? $($tt)*);
};
(@Modules @$address:literal
$(
$(#[$meta:meta])*
module $module:ident {
$($tt:tt)*
}
)*
) => {
$(
$crate::sui_pkg_sdk!(@Module $(#[$meta])* $address::$module
$($tt)*
);
)*
};
(@Modules
$(
$(#[$meta:meta])*
module $module:ident {
$($tt:tt)*
}
)*
) => {
$(
$crate::sui_pkg_sdk!(@Module $(#[$meta])* $module
$($tt)*
);
)*
};
(@Module $(#[$meta:meta])* $($address:literal::)?$module:ident
$($tt:tt)*
) => {
#[allow(clippy::too_many_arguments)]
$(#[$meta])*
pub mod $module {
#[allow(unused_imports)]
use super::*;
#[allow(non_camel_case_types, unused)]
type address = $crate::Address;
#[allow(non_camel_case_types, unused)]
type u256 = $crate::U256;
#[allow(non_camel_case_types, unused)]
type vector<T> = $crate::MoveVec<T>;
$crate::sui_pkg_sdk!(@ModuleMembers $($address::)?$module {
$($tt)*
});
}
};
(@ModuleMembers $($address:literal::)?$module:ident { }) => { };
(@ModuleMembers $($address:literal::)?$module:ident {
$(#[$meta:meta])* $(public $( ($_scope:ident) )? )? struct $Struct:ident
$(<$($(!$phantom:ident)? $T:ident$(: $_:ident $(+ $__:ident)*)?),*>)? $(has $($ability:ident),+)? {
$($struct_content:tt)+
}
$($rest:tt)*
}) => {
$crate::sui_pkg_sdk!(@Struct
#[derive($crate::derive_new::new)]
#[move_(module=$module)]
$(#[move_(address=$address)])?
$(#[$meta])*
$Struct$(<$($T),*>)? [$($($($phantom)? $T,)*)?] ($($struct_content)+) -> {}
);
$crate::sui_pkg_sdk!(@abilities
$Struct$(<$($T),*>)? [$($($ability,)+)?]
);
$crate::sui_pkg_sdk!(@ModuleMembers $($address::)?$module {
$($rest)*
});
};
(@ModuleMembers $($address:literal::)?$module:ident {
$(#[$meta:meta])* $(public $( ($_scope:ident) )? )? struct $Struct:ident
$(<$($(!$phantom:ident)? $T:ident$(: $_:ident $(+ $__:ident)*)?),*>)? ($( $struct_content:tt )+) $(has $($ability:ident),+)?;
$($rest:tt)*
}) => {
$crate::sui_pkg_sdk!(@Struct
#[derive($crate::derive_new::new)]
#[move_(module=$module)]
$(#[move_(address=$address)])?
$(#[$meta])*
$Struct$(<$($T),*>)?
[$($($($phantom)? $T,)*)?]
($($struct_content)+)
-> ()
);
$crate::sui_pkg_sdk!(@abilities
$Struct$(<$($T),*>)? [$($($ability,)+)?]
);
$crate::sui_pkg_sdk!(@ModuleMembers $($address::)?$module {
$($rest)*
});
};
(@ModuleMembers $($address:literal::)?$module:ident {
$(#[$meta:meta])* $(public $( ($_scope:ident) )? )? struct $Struct:ident$(<$(!phantom $T:ident),*>)?
$(has $($ability:ident),+)? {}
$($rest:tt)*
}) => {
$crate::sui_pkg_sdk!(@Struct
#[move_(module=$module)]
$(#[move_(address=$address)])?
$(#[$meta])*
$Struct$(<$($T),*>)?
[$($(phantom $T,)*)?]
()
-> { dummy_field: bool, }
);
impl$(<$($T: $crate::MoveType),*>)? $Struct$(<$($T),*>)? {
pub fn new() -> Self {
Self {
dummy_field: false,
$($( $T: ::std::marker::PhantomData::<$T>,
)*)?
}
}
}
impl$(<$($T: $crate::MoveType),*>)? ::std::default::Default for $Struct$(<$($T),*>)? {
fn default() -> Self {
Self::new()
}
}
$crate::sui_pkg_sdk!(@ModuleMembers $($address::)?$module {
$($rest)*
});
};
(@ModuleMembers $($address:literal::)?$module:ident {
$(#[$meta:meta])* $(public $( ($_scope:ident) )? )? struct $Struct:ident$(<$(!phantom $T:ident),*>)?
()
$(has $($ability:ident),+)? ;
$($rest:tt)*
}) => {
$crate::sui_pkg_sdk!(@Struct
#[move_(module=$module)]
$(#[move_(address=$address)])?
$(#[$meta])*
$Struct$(<$($T),*>)?
[$($(phantom $T,)*)?]
()
-> ( bool, )
);
impl$(<$($T: $crate::MoveType),*>)? $Struct$(<$($T),*>)? {
pub fn new() -> Self {
Self(
false,
$( $( ::std::marker::PhantomData::<$T>,
)* )?
)
}
}
impl$(<$($T: $crate::MoveType),*>)? ::std::default::Default for $Struct$(<$($T),*>)? {
fn default() -> Self {
Self::new()
}
}
$crate::sui_pkg_sdk!(@ModuleMembers $($address::)?$module {
$($rest)*
});
};
(@Struct
$(#[$meta:meta])*
$Struct:ident$(<$($G:ident),*>)?
[]
()
-> { $($result:tt)* }
) => {
#[derive(
$crate::MoveStruct,
$crate::serde::Deserialize,
$crate::serde::Serialize,
$crate::Tabled,
Clone,
Debug,
PartialEq,
Eq,
Hash,
)]
$(#[$meta])*
#[move_(crate = ::af_sui_pkg_sdk::af_move_type)]
#[serde(crate = "::af_sui_pkg_sdk::serde")]
#[serde(bound(deserialize = ""))]
#[tabled(crate = "::af_sui_pkg_sdk::tabled")]
#[allow(non_snake_case)]
pub struct $Struct$(<$($G: $crate::MoveType),*>)? {
$($result)*
}
impl$(<$($G: $crate::MoveType),*>)? ::std::fmt::Display for $Struct$(<$($G),*>)? {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
let settings = $crate::move_struct_table_option(stringify!($Struct));
let mut table = $crate::Table::new([self]);
table.with(settings);
write!(f, "{}", table)
}
}
};
(@Struct
$(#[$meta:meta])*
$Struct:ident$(<$($G:ident),*>)?
[phantom $T:ident, $($rest:tt)*]
($($fields:tt)*)
-> { $($result:tt)* }
) => {
$crate::sui_pkg_sdk!(@Struct
$(#[$meta])*
$Struct$(<$($G),*>)?
[$($rest)*]
($($fields)*)
-> {
$($result)*
#[tabled(skip)]
#[serde(skip_deserializing, skip_serializing, default)]
$T: ::std::marker::PhantomData<$T>,
}
);
};
(@Struct
$(#[$meta:meta])*
$Struct:ident$(<$($G:ident),*>)?
[$_T:ident, $($rest:tt)*]
($($fields:tt)*)
-> { $($result:tt)* }
) => {
$crate::sui_pkg_sdk!(@Struct
$(#[$meta])*
$Struct$(<$($G),*>)?
[$($rest)*]
($($fields)*)
-> { $($result)* }
);
};
(@Struct
$(#[$meta:meta])*
$Struct:ident$(<$($G:ident),*>)?
[]
($(#[$fmeta:meta])* $field:ident: Option<$type:ty> $(, $($rest:tt)*)?)
-> { $($result:tt)* }
) => {
$crate::sui_pkg_sdk!(@Struct
$(#[$meta])*
$Struct$(<$($G),*>)?
[]
($($($rest)*)?)
-> {
$($result)*
$(#[$fmeta])*
#[tabled(format(
"{}",
self.$field
.as_ref()
.map(ToString::to_string)
.unwrap_or_else(|| "None".into())
))]
pub $field: Option<$type>,
}
);
};
(@Struct
$(#[$meta:meta])*
$Struct:ident$(<$($G:ident),*>)?
[]
($(#[$fmeta:meta])* $field:ident: $type:ty $(, $($rest:tt)*)?)
-> { $($result:tt)* }
) => {
$crate::sui_pkg_sdk!(@Struct
$(#[$meta])*
$Struct$(<$($G),*>)?
[]
($($($rest)*)?)
-> {
$($result)*
$(#[$fmeta])* pub $field: $type,
}
);
};
(@Struct
$(#[$meta:meta])*
$Struct:ident$(<$($G:ident),*>)?
[]
()
-> ( $($result:tt)* )
) => {
#[derive(
$crate::MoveStruct,
$crate::serde::Deserialize,
$crate::serde::Serialize,
$crate::Tabled,
Clone,
Debug,
PartialEq,
Eq,
Hash,
)]
$(#[$meta])*
#[move_(crate = ::af_sui_pkg_sdk::af_move_type)]
#[serde(crate = "::af_sui_pkg_sdk::serde")]
#[serde(bound(deserialize = ""))]
#[tabled(crate = "::af_sui_pkg_sdk::tabled")]
#[allow(non_snake_case)]
pub struct $Struct$(<$($G: $crate::MoveType),*>)? (
$($result)*
);
impl$(<$($G: $crate::MoveType),*>)? ::std::fmt::Display for $Struct$(<$($G),*>)? {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
let settings = $crate::move_struct_table_option(stringify!($Struct));
let mut table = $crate::Table::new([self]);
table.with(settings);
write!(f, "{}", table)
}
}
};
(@Struct
$(#[$meta:meta])*
$Struct:ident$(<$($G:ident),*>)?
[phantom $T:ident, $($rest:tt)*]
($($fields:tt)*)
-> ( $($result:tt)* )
) => {
$crate::sui_pkg_sdk!(@Struct
$(#[$meta])*
$Struct$(<$($G),*>)?
[$($rest)*]
($($fields)*)
-> (
$($result)*
#[tabled(skip)]
#[serde(skip_deserializing, skip_serializing, default)]
::std::marker::PhantomData<$T>,
)
);
};
(@Struct
$(#[$meta:meta])*
$Struct:ident$(<$($G:ident),*>)?
[$_T:ident, $($rest:tt)*]
($($fields:tt)*)
-> ( $($result:tt)* )
) => {
$crate::sui_pkg_sdk!(@Struct
$(#[$meta])*
$Struct$(<$($G),*>)?
[$($rest)*]
($($fields)*)
-> ( $($result)* )
);
};
(@Struct
$(#[$meta:meta])*
$Struct:ident$(<$($G:ident),*>)?
[]
($(#[$fmeta:meta])* $type:ty $(, $($rest:tt)*)?)
-> ( $($result:tt)* )
) => {
$crate::sui_pkg_sdk!(@Struct
$(#[$meta])*
$Struct$(<$($G),*>)?
[]
($($($rest)*)?)
-> (
$($result)*
$(#[$fmeta])* pub $type,
)
);
};
(@abilities
$Struct:ident$(<$($G:ident),*>)?
[]
) => {};
(@abilities
$Struct:ident$(<$($G:ident),*>)?
[key, $($rest:tt)*]
) => {
impl$(<$($G: $crate::MoveType),*>)? $crate::HasKey for $Struct$(<$($G),*>)? {
fn object_id(&self) -> $crate::Address {
self.id.id.bytes
}
}
$crate::sui_pkg_sdk!(@abilities
$Struct$(<$($G),*>)? [$($rest)*]
);
};
(@abilities
$Struct:ident$(<$($G:ident),*>)?
[$other:ident, $($rest:tt)*]
) => {
$crate::sui_pkg_sdk!(@abilities
$Struct$(<$($G),*>)? [$($rest)*]
);
};
}
pub fn move_struct_table_option<S: AsRef<str>>(
name: S,
) -> impl for<'a> TableOption<VecRecords<Text<String>>, ColoredConfig, CompleteDimensionVecRecords<'a>>
{
Settings::default()
.with(Rotate::Left)
.with(Rotate::Top)
.with(Style::rounded())
.with(Header::new(name))
}