I want to make the attribute #[repr] optional, if no #[repr(type)] is provided, the default #[repr(u8)] will be used
if no #[repr] attribute is provided, the default attribute #[repr(u8)] should be used
i tired #[repr(if $repr { $repr } else { u8 })]
but it doesn't seem to work
#[macro_export]
macro_rules! int_enum {
(
#[derive($($path:path),+)]
$(#[$attr:meta])*
$(#[repr($repr:ty)])?
$vis:vis enum $Name:ident {
$(
$(#[$field_attr:meta])*
$variant:ident = $index:tt,
)*
}
) => {
$crate::macros::paste::paste! {
#[derive(Debug,Clone,Copy,$crate::Serialize_repr,$crate::Deserialize_repr,enum_sql::IntEnumFromSql,enum_sql::IntEnumToSql , $($path),*)]
$(#[$attr])*
#[serde(try_from="i16", into="i16")]
#[repr(if $repr ? {$repr}: {u8})]
$vis enum $Name {
$(
$(#[$field_attr])*
$variant = $index,
)*
}
impl $Name {
pub fn as_i16(&self) -> i16 {
use $Name::*;
match self {
$($variant => $index,)*
}
}
}
impl From<$Name> for i16 {
fn from(status: $Name) -> i16 {
status.as_i16()
}
}
impl TryFrom<i16> for $Name {
type Error = $crate::Error;
fn try_from(code: i16) -> $crate::Result<Self> {
use $Name::*;
match code {
$($index => Ok($variant),)*
_ => Err($crate::Error::invalid("invalid_status_code").set_message(format!("unexpected status: {}", code)))
}
}
}
}
};
(
$(#[$attr:meta])*
$vis:vis enum $Name:ident {
$(
$(#[$field_attr:meta])*
$variant:ident = $index:tt,
)*
}
) => {
$crate::macros::paste::paste! {
#[derive(Debug,Clone,Copy,$crate::Serialize_repr,$crate::Deserialize_repr,enum_sql::IntEnumFromSql,enum_sql::IntEnumToSql)]
$(#[$attr])*
#[repr(u8)]
$vis enum $Name {
$(
$(#[$field_attr])*
$variant = $index,
)*
}
impl $Name {
pub fn as_i16(&self) -> i16 {
use $Name::*;
match self {
$($variant => $index,)*
}
}
}
impl From<$Name> for i16 {
fn from(status: $Name) -> i16 {
status.as_i16()
}
}
impl TryFrom<i16> for $Name {
type Error = $crate::Error;
fn try_from(code: i16) -> $crate::Result<Self> {
use $Name::*;
match code {
$($index => Ok($variant),)*
_ => Err($crate::Error::invalid("invalid_status_code").set_message(format!("unexpected status: {}", code)))
}
}
}
}
}
}