#[macro_export]
macro_rules! define_model {
(
$(#[$attr:meta])*
$vis:vis struct $name:ident {
$(
$(#[$field_attr:meta])*
$field_vis:vis $field_name:ident: $field_type:ty
),* $(,)?
}
primary_key: $primary_field:ident -> $primary_type:ty,
) => {
$(#[$attr])*
$vis struct $name {
$(
$(#[$field_attr])*
$field_vis $field_name: $field_type,
)*
}
impl $crate::traits::Model for $name {
type PrimaryKey = $primary_type;
type SecondaryKey = ();
fn get_primary_key(&self) -> Self::PrimaryKey {
self.$primary_field.clone()
}
fn get_secondary_key(&self) -> Option<Self::SecondaryKey> {
None
}
fn model_name() -> &'static str {
stringify!($name)
}
}
impl ic_stable_structures::Storable for $name {
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
std::borrow::Cow::Owned(candid::Encode!(self).expect("Failed to encode model for storage"))
}
fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
candid::Decode!(bytes.as_ref(), Self).expect("Failed to decode model from storage")
}
const BOUND: ic_stable_structures::storable::Bound =
ic_stable_structures::storable::Bound::Unbounded;
}
};
(
$(#[$attr:meta])*
$vis:vis struct $name:ident {
$(
$(#[$field_attr:meta])*
$field_vis:vis $field_name:ident: $field_type:ty
),* $(,)?
}
primary_key: $primary_field:ident -> $primary_type:ty,
secondary_key: $secondary_field:ident -> $secondary_type:ty,
) => {
$(#[$attr])*
$vis struct $name {
$(
$(#[$field_attr])*
$field_vis $field_name: $field_type,
)*
}
impl $crate::traits::Model for $name {
type PrimaryKey = $primary_type;
type SecondaryKey = $secondary_type;
fn get_primary_key(&self) -> Self::PrimaryKey {
self.$primary_field.clone()
}
fn get_secondary_key(&self) -> Option<Self::SecondaryKey> {
Some(self.$secondary_field.clone())
}
fn model_name() -> &'static str {
stringify!($name)
}
}
impl ic_stable_structures::Storable for $name {
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
std::borrow::Cow::Owned(candid::Encode!(self).expect("Failed to encode model for storage"))
}
fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
candid::Decode!(bytes.as_ref(), Self).expect("Failed to decode model from storage")
}
const BOUND: ic_stable_structures::storable::Bound =
ic_stable_structures::storable::Bound::Unbounded;
}
};
}
pub use define_model;